SPUG: references, subroutines, fork

Benjamin Franks benjamin at golly.com
Thu Nov 29 14:25:40 CST 2001


I pass a subroutine a reference to a array.  The subroutine forks off a child and then makes use of the reference to the hash.  However, in the parent branch of the fork, I empty/delete the underlying array elements.  Even if I put the child to sleep until well after the parent has deleted the array, I can still access the data the reference use to point to.  I would have imagined if you destroy the underlying data the reference would reflect that change.  What's happening...how does perl handle this situation?

Here's some rough code as an example:
#!/usr/bin/perl
use strict;

push my @alpha, {
   'test' => ['a','b','c','d'],
   'test1' => 2
};

while (@alpha) {
   &child($alpha[$#alpha]);         
   @alpha=();
   print "PARENT: ",$#alpha,"\n";                 
}
exit;

sub child {         
   my $x=shift;
   warn ("can't fork: $!") unless defined (my $child=fork());
   if ($child) {
      print "PARENT: ",%{$x}->{test1},"\n";
   }
   else {
      sleep(10);                                    
      print "CHILD: ",%{$x}->{test1},"\n";
      exit;
   }
}

_____________________________________________________________


 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     POST TO: spug-list at pm.org       PROBLEMS: owner-spug-list at pm.org
      Subscriptions; Email to majordomo at pm.org:  ACTION  LIST  EMAIL
  Replace ACTION by subscribe or unsubscribe, EMAIL by your Email-address
 For daily traffic, use spug-list for LIST ;  for weekly, spug-list-digest
     Seattle Perl Users Group (SPUG) Home Page: http://zipcon.net/spug/





More information about the spug-list mailing list