Reputation: 17
I have the following scenario -> 3 files
-------------------Module.pm------------------
use strict;
use warnings;
Package Foo;
our %hash = ( NAME => "NONE" , SSN => "NONE");
----------------------a.pl-------------------
use strict;
use warnings;
use Module;
my $name = "Bill"
my $SSN = "123456789";
# update name and SSN
$Foo::hash{NAME} = $name;
$Foo::hash{SSN} = $SSN;
----------------------b.pl--------------------
use strict;
use warnings;
use Module;
## print the updated values of name and SSN
print "\nUpdated values -> NAME = $Foo::hash{'NAME'} SSN = $Foo::hash{SSN}";
I execute a.pl first and b.pl later. But a.pl gives the updated output but b.pl still gives the old "NONE" output for both fields. I even tried to print the addresses of both has values in a.pl and b.pl and they're different.
Any ideas how can I access the values updated in a.pl into b.pl?
Upvotes: 0
Views: 222
Reputation: 386561
Your are conflating source code (text to be executed) and the data structures that text creates when executed.
Executing Module.pm
(e.g. by loading it) creates a hash in the current process. (The current interpreter, to be more specific.) a.pl
changes that hash.
b.pl
does not access anything in that process or interpreter, neither of which is likely to even exist anymore. b.pl
executes the code in Module.pm
, and nothing is even attempting to change that file.
If you want to transfer data from one process to another, you're going to have to store it somewhere both can access. (Disk, database, pipe, shared memory, etc)
# To store
use Storable qw( lock_nstore );
lock_nstore(\%Foo::hash, 'file');
# To recover
use Storable qw( lock_retrieve );
%Foo::hash = %{ lock_retrieve('file') };
Upvotes: 2