Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Global Variable

by abithajames (Initiate)
on Sep 22, 2008 at 12:50 UTC ( #713006=perlquestion: print w/ replies, xml ) Need Help??
abithajames has asked for the wisdom of the Perl Monks concerning the following question:


There should be a variable declared in one file say $error in "file1.pl".

I need to change the value of the variable $error in another file say "file2.pl".

This change should get reflected in "file1.pl".

How can I achieve this?

for eg: file1.pl

$error = 10; #Assigning value 10 to $error.

print "before $error \n"; # This will print 10.

system("file2.pl");

print "after $error \n"; # This should print 20. How can I acheive this?

file2.pl

$error = 20;

You valuable suggestions will be of great help. Basically I want the value of a variable to be changed in a file and the changed value should get reflected in the file from which the former file is executed.

Thanks,

Abitha.

Comment on Global Variable
Re: Global Variable
by Corion (Pope) on Sep 22, 2008 at 12:52 UTC

    That's not how system works. Read it, and understand the concept of separate memory spaces. Maybe you want do or require to load a configuration. Or you want one of the Config modules instead?

Re: Global Variable
by lamp (Chaplain) on Sep 22, 2008 at 14:39 UTC
    The following is the sample code for achieving the above mentioned scenario using require method.
    file1.pl
    #!/usr/bin/perl -w use strict; use warnings; our $error; $error = 10; print "before $error \n"; require 'file2.pl'; # you can use 'do' also print "after $error \n";
    file2.pl
    #!/usr/bin/perl -w use strict; our $error = 20;
    output
    before 10 after 20
Re: Global Variable
by oko1 (Deacon) on Sep 22, 2008 at 14:46 UTC

    Ignoring, for the moment, your misuse of "system" (please do follow Corion's advice - it's dead on target), you could do it this way (a.k.a. "crude but effective"):

    #!/usr/bin/perl -w use strict; # This is file1 my $error = 10; print "$error.\n";
    #!/usr/bin/perl -w use strict; # This is file2 print "Before: "; # Run the first script do "file1"; my $new_error_value = 20; @ARGV = "file1"; { local $^I = ""; # Enable in-place editing while (<>){ s/(\$error\s*=\s*)\d+/$1$new_error_value/; print; } } print "After: "; # Run the first script do "file1";

    Or, if you really insist on using "system":

    #!/usr/bin/perl -w use strict; # This is file2 print "Before: "; # Run the first script do "file1"; system "/usr/bin/perl", qw{-i -wpe s/(\$error\s*=\s*)\d+/${1}20/ file1 +}; print "After: "; # Run the first script do "file1";

    Update: Tweaked regex to remove unnecessary parens (just because I'm picky like that. ;)


    --
    "Language shapes the way we think, and determines what we can think about."
    -- B. L. Whorf

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://713006]
Approved by wfsp
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (5)
As of 2014-09-18 22:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (126 votes), past polls