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

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 "".

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

This change should get reflected in "".

How can I achieve this?

for eg:

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

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


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

$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.



Replies are listed 'Best First'.
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.
    #!/usr/bin/perl -w use strict; use warnings; our $error; $error = 10; print "before $error \n"; require ''; # you can use 'do' also print "after $error \n";
    #!/usr/bin/perl -w use strict; our $error = 20;
    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?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://713006]
Approved by wfsp
[stevieb]: I have got ready everything in a stable manner to go forward with a full-blown "Raspberry Pi with Perl" series, and at the same time "Automated testing locally for everything" series. A year it took. A year I've been waiting to do this.

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (6)
As of 2017-06-29 01:30 GMT
Find Nodes?
    Voting Booth?
    How many monitors do you use while coding?

    Results (653 votes). Check out past polls.