Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

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
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (7)
As of 2017-05-26 12:18 GMT
Find Nodes?
    Voting Booth?