in reply to Re: Defining global variable in a subroutine
in thread Defining global variable in a subroutine
choroba,
"You can not change the outer scope from within a sub. If you want to have a global variable, declare it in the topmost scope."
I believe you are referring to "use strict;", but if you run the following code with "# use strict;", you'll see that you can do what the OP wanted.
my $in = result( 3 ); print "my \$in: $in \t \$out: \$out\n"; sub result { our $out = shift; return( 2 ** $out ); }
This also will work in a forked environment, since the global 'our' variable is in the address space of the parent.
However, I would use "use strict;" and predefine the global variables as you stated.
Thank you
"Well done is better than well said." - Benjamin Franklin
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: Defining global variable in a subroutine
by Eliya (Vicar) on Feb 22, 2012 at 14:42 UTC | |
by flexvault (Monsignor) on Feb 22, 2012 at 15:09 UTC | |
by dave_the_m (Monsignor) on Feb 22, 2012 at 16:21 UTC | |
by Eliya (Vicar) on Feb 22, 2012 at 16:24 UTC | |
by flexvault (Monsignor) on Feb 23, 2012 at 10:14 UTC | |
by choroba (Cardinal) on Feb 23, 2012 at 10:23 UTC | |
| |
by zwon (Abbot) on Feb 23, 2012 at 11:35 UTC |