[plug] [OT] Perl Sub Modify Global Variable

bwarff bwarff at obsidian.com.au
Wed Feb 9 16:38:54 WST 2005


you have really put yourself into the deep end with this strategy, 
you will need to learn about locking, interprocess communication,
and all the gotchas associated with the above.

personally id use a 'user_database' which already has all this functionality,
such as mysql or postgres ...



On Wed, 09 Feb 2005 15:31:14 +0800
Timothy White <weirdo at tigris.org> wrote:

> Ok. Still no luck.
> It appears that because I am forking I can't share between forks. This
> is a real bummer.
> It is especially annoying because I am writing a forking server to
> handle requests and I want the user_database to be shared between them all.
> 
> Any ideas?
> 
> Tim
> NB: Sorry anyone who pays per kb, I know I should just post the code on
> a website.
> p.s. I tried a small test script[2] to see if I could get the global and
> forking working. below are the results[1].
> Basically I have a fork which runs 2 subs. One of the subs sits there
> monitoring the variable and changing it to a preset value each time. The
> other sub calls 2 subs, one after the other, each one checks the
> variable and then updates it.
> [1]
> Loading user db...      127.0.0.1
> Monitoring user...      127.0.0.1       Done... 192.168.0.1
> Updating user...        127.0.0.1       Done... 192.168.0.2
> Checking user...        192.168.0.2     Done... 192.168.0.3
> Monitoring user...      192.168.0.1     Done... 192.168.0.1
> Updating user...        192.168.0.3     Done... 192.168.0.2
> Checking user...        192.168.0.2     Done... 192.168.0.3
> Monitoring user...      192.168.0.1     Done... 192.168.0.1
> Monitoring user...      192.168.0.1     Done... 192.168.0.1
> Updating user...        192.168.0.3     Done... 192.168.0.2
> Checking user...        192.168.0.2     Done... 192.168.0.3
> Monitoring user...      192.168.0.1     Done... 192.168.0.1
> Monitoring user...      192.168.0.1     Done... 192.168.0.1
> Updating user...        192.168.0.3     Done... 192.168.0.2
> Checking user...        192.168.0.2     Done... 192.168.0.3
> Monitoring user...      192.168.0.1     Done... 192.168.0.1
> 
> 
> [2]
> use strict vars;
> our %userdb;
> 
> load_user_database(); # This appears to work for the inital loading into
> $userdb
> 
> my $pid = fork();
> if ($pid) {
>     modify();
> }
> else {
>     user_monitor();
> }
> 
> sub modify {
>     while () {
>         my $user = 'tim';
>         my $ip   = '192.168.0.2';    # Normally get this from other areas
>         user_update( $user, $ip );
>         user_check($user);
>         sleep 1;
>     }
> }
> 
> sub user_update {
>     my $user = shift(@_);
>     my $ip   = shift(@_);
>     print "Updating user...\t"
>       . $userdb{$user}
>       ->{'ip'};    #Prints out 127.0.0.1 first time, then 192.168.0.3
>     $userdb{$user}->{'ip'} = $ip;
>     print "\tDone... "
>       . $userdb{$user}->{'ip'}
>       . "\n";      #Prints out 192.168.0.2 every time
>     return 1;
> }
> 
> sub user_monitor {
>     while () {
>         my $user = 'tim';
>         print "Monitoring user...\t"
>           . $userdb{$user}
>           ->{'ip'};    #Prints out 127.0.0.1 first time, then 192.168.0.1
>         $userdb{$user}->{'ip'} = "192.168.0.1";
>         print "\tDone... "
>           . $userdb{$user}->{'ip'}
>           . "\n";      #Prints out 192.168.0.1 every time
>         sleep 1;
>     }
> }
> 
> sub load_user_database {
>     $userdb{'tim'}->{'ip'} = "127.0.0.1";
>     print "Loading user db...\t" . $userdb{'tim'}->{'ip'} . "\n";
> }
> 
> sub user_check {
>     my $user = 'tim';
>     print "Checking user...\t"
>       . $userdb{$user}
>       ->{'ip'};    #Prints out 127.0.0.1 first time, then 192.168.0.2
>     $userdb{$user}->{'ip'} = "192.168.0.3";
>     print "\tDone... "
>       . $userdb{$user}->{'ip'}
>       . "\n";      #Prints out 192.168.0.1 every time
>     sleep 1;
> }
> 
> --
> Tim White - Use the Fox, Luke!
> PGP/GPG id: 602E944D, Pub Key Serv: subkeys.pgp.net
> Fingerprint: 04C2 9682 B7B2 3006 009D  A9F3 067E EDCD 602E 944D
> Hi! I'm a .signature virus! Copy me into your ~/.signature to help me spread!
> --
> 
> 



More information about the plug mailing list