Steve Bertrand wrote:
Shawn H. Corey wrote:
Steve Bertrand wrote:
Ok, I'm done with the philosophical stuff.
I forget where I read about the warning "...will not stay shared", so
I'm going for the quick answer here.
Add `use diagnostics;` to the program for a more complete error message.
Also, see:
http://www.perl.com/pub/a/2002/05/07/mod_perl.html
http://www.perlmonks.org/?node_id=551496
http://www.nntp.perl.org/group/perl.beginners/2008/04/msg99983.html
Try not putting a named sub inside another :)
Thanks Shawn!:
This problem can usually be solved by making the inner subroutine
anonymous, using the sub {} syntax. When inner anonymous subs that
reference variables in outer subroutines are called or referenced, they
are automatically rebound to the current values of such variables.
Steve
Here's an example of what they're talking about. Notice that when
CONFIG_VAR is called, a new $new is created and set to zero. For it's
second call and those that follow, the $num inside callback remains
unchanged.
sub CONFIG_VAR {
my $num = 0;
sub callback {
return ++$num;
}
print "CONFIG_VAR: \$num = $num\n";
}
print callback(), "\n";
print callback(), "\n";
print callback(), "\n";
CONFIG_VAR();
print callback(), "\n";
print callback(), "\n";
print callback(), "\n";
CONFIG_VAR();
print callback(), "\n";
print callback(), "\n";
print callback(), "\n";
__END__
Since this is the behaviour you want, the error message seems
superfluous but if you wanted the other behaviour, that is, $num to be
reset by calling CONFIG_VAR, it won't work. To do that, add another sub:
{
my $num = 0;
sub callback {
return ++$num;
}
sub set {
$num = shift @_ || 0;
}
}
--
Just my 0.00000002 million dollars worth,
Shawn
Programming is as much about organization and communication
as it is about coding.
Regardless of how small the crowd is, there is always one in
it who has to find out the hard way that the laws of physics
applies to them too.
--
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
http://learn.perl.org/