in reply to Sharing a variable across multiple files

The problem is that with system("perl 2.pl"); you call a new instance of perl, which has no shared memory with the one that calls it. Those are two different processes.

What you probably want to do is do "2.pl", this way 2.pl will be run in the same context as 1.pl . Look do's documentation for information on that.

Replies are listed 'Best First'.
Re^2: Sharing a variable across multiple files
by bihuboliya (Acolyte) on Aug 07, 2013 at 10:56 UTC
    Hi Eily, thank you for your suggestion. I did run the second script as do "2.pl" and that solved the problem :)
      Dear Monks, I gained some additional wisdom about the function 'do' which I would like to share.

      I used the do function in the first script to execute the second script in the same context (shared memory without creating another process by using system(..)). Though, I had to pass some arguments to the second script. do "2.pl $arg1" does not work.

      Finally I came to know that if we want to pass argument to the second script, we can exploit the idea that @ARGV will be visible to the second script. I achieved the goal as below:

      use my_module; print "@my_var\n"; # prints 1..10 change_var(); print "@my_var\n"; # prints 20..30 { # using local to supress @ARGV local @ARGV = ($arg1, $arg2...); # $arg1, $arg2... are the argumen +ts to be passed do "2.pl"; } exit;
      Now, in 2.pl I can get these arguments using shift.

      Regards,

      --Anupam