in reply to Sharing a variable across multiple files

The answers above are more informative than this one will be. But your post inspired me to play with IPC::Shareable. It allows you to put variables into shared memory and access them from separate scripts. This is just a toy example and I don't have the experience required to recommend this as a solution. But it may be instructive.

It consists of two scripts, adult.pl which will run child.pl in the background. After they're both running they each update a shared array.

adult.pl:
use IPC::Shareable; use Data::Dump qw(ddx); $| = 1; # Setup shared array reference "my_var" my $id = 'my_special_name'; my %options = (create => 'yes', exclusive => 0, mode => 0644, destroy +=> 'yes'); my $my_var; tie $my_var, 'IPC::Shareable', $id, { %options } or die "tie failed: $ +!\n"; $my_var = [0..10]; system('./child.pl &'); until ($my_var->[-1] == 999) { my $num = int(rand()*100); push @$my_var, $num; ddx [ @$my_var ]; sleep 2; }; print "Leaving adult.pl..\n";
child.pl:
use IPC::Shareable; use Data::Dump qw(ddx); $| = 1; my $id = 'my_special_name'; my %options = (create => 0, exclusive => 0, mode => 0644, destroy => 0 +); my $my_var; tie $my_var, 'IPC::Shareable', $id, { %options } or die "tie failed: $ +!\n"; for (0..3) { my $num = int(rand()*100); push @$my_var, $num; ddx [ @$my_var ]; sleep 2; } push @$my_var, 999; print "Leaving child.pl..\n";
Output:
# adult.pl:20: [0 .. 10, 16] # child.pl:16: [0 .. 10, 16, 72] # adult.pl:20: [0 .. 10, 16, 72, 52] # child.pl:16: [0 .. 10, 16, 72, 52, 17] # adult.pl:20: [0 .. 10, 16, 72, 52, 17, 66] # child.pl:16: [0 .. 10, 16, 72, 52, 17, 66, 96] # adult.pl:20: [0 .. 10, 16, 72, 52, 17, 66, 96, 92] # child.pl:16: [0 .. 10, 16, 72, 52, 17, 66, 96, 92, 88] # adult.pl:20: [0 .. 10, 16, 72, 52, 17, 66, 96, 92, 88, 56] Leaving child.pl.. Leaving adult.pl..

* Note that they share data based on a named id, which means their connection can be established without the child script being started by the adult.