0

I have the following scenario -> 3 files

  1. module.pl
  2. a.pl
  3. b.pl

-------------------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?

7
  • 4
    these are two separate processes; how they should share variables? Commented Oct 2, 2013 at 16:39
  • So is there a way in which 2 processes can share/update/access the same global variable? Commented Oct 2, 2013 at 16:53
  • Lots of ways. Which one is best depends on your use case. Do the processes need to share data while both are running, or does only 1 run at a time? Commented Oct 2, 2013 at 16:54
  • 2
    That gives "Can't locate object method "Package" via package "Foo" [...]", not the behaviour your describe. Any other differences between what you posted and what you ran? Commented Oct 2, 2013 at 17:01
  • 1
    Note that the file name and the package name should match. Commented Oct 2, 2013 at 17:05

1 Answer 1

2

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') };
Sign up to request clarification or add additional context in comments.

1 Comment

This will definitely help. Thanks for the clarification! I got my answer. Thanks a lot

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.