use strict;
use vars qw($HELLO);
$HELLO = 'Hello';
sub new_message {
$HELLO = 'Nihao';
};
| [reply] [d/l] |
No reason not to use a lexical.
use strict;
my $HELLO = 'Hello';
sub new_message {
$HELLO = 'Nihao';
}
| [reply] [d/l] |
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. For including a file, see do. | [reply] |
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
| [reply] [d/l] |
This also will work in a forked environment, since the global 'our' variable is in the address space of the parent.
Could you elaborate what you mean by that?
Almost sounds like you are saying the child process could modify the variable in the parent process. If so, I'd like to see a demo.
| [reply] |
You don't "modifiers" when using strict, and you don't need strict to use "modifiers".
One way of doing what you want is to declare you shared variables as lexical variables in the most inner scope that is shared by your subroutines. In many cases, that's the file:
#!/usr/bin/perl
use 5.010;
my $global;
sub set_global {$global = shift}
sub get_global {$global}
sub say_global {say $global}
If you want to import variables from a different file, the usual way of doing that is with the help of Exporter:
package Blabla;
our @ISA = qw[Exporter];
use Exporter();
our @EXPORT = qw[$global1 $global2 $global3];
Then in any package that wants to use any of the variables, just do:
use Blabla;
| [reply] [d/l] [select] |