meonkeys has asked for the wisdom of the Perl Monks concerning the following question:
#!/usr/bin/perl -w use Data::Dumper qw( Dumper ); use strict; sub mycode { my $var = $_[0]; defined($var) ? print qq|mycode() called with $var\n| : print qq|mycode() called with no operands.\n|; my $foo = "blah" if $var; # does $foo become static here? print "foo is $foo\n" if $foo; $foo ||= "FOO"; } mycode("test"); # "foo is blah" mycode(); # mycode(); # "foo is FOO" mycode(); # "foo is FOO" # print Dumper(\%main::); # make sure $foo is never in symbol table
but I would expect this output:mycode() called with test foo is blah mycode() called with no operands. mycode() called with no operands. foo is FOO mycode() called with no operands. foo is FOO
Somehow the variable $foo gets the value 'FOO' on the second call of mycode(). This seems to persist as seen in the third and fourth calls of mycode(). Can anyone explain this?mycode() called with test foo is blah mycode() called with no operands. mycode() called with no operands. mycode() called with no operands.
|
|---|