in reply to Re: Re: Lexical scoping like a fox
in thread Lexical scoping like a fox
Couple other questions/commentsuse strict; sub foo { print "\$a is ", defined($a) ? $a : 'undef', $/; print "\$b is ", defined($b) ? $b : 'undef', $/; } print "Just before start of scope\n"; { print "Just after start of scope\n"; my $a = 1; local $b = 1; print "a=$a, b=$b\n"; foo(); print "Just before end of scope\n"; } print "Just after end of scope\n"; print "\$a is ", defined($a) ? $a : 'undef', $/; print "\$b is ", defined($b) ? $b : 'undef', $/;
3)can someone explain further on "lexical variables are declared at compile-time, not initialised?[root@myserver tmp]# cat -n !$ cat -n ./per_begin.pl1 1 #!/usr/bin/perl -w 2 3 use strict; 4 5 6 my $x = "original state"; 7 8 sub foo { 9 print " \$x is: $x\n"; 10 } 11 12 { # beginning of lexical scope 13 14 local $x = "altered state"; 15 foo(); 16 17 } # end of lexical scope 18 print "\$x is: $x\n"; [root@myserver tmp]# ./!$ ././per_begin.pl1 Can't localize lexical variable $x at ././per_begin.pl1 line 14.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^4: Lexical scoping like a fox
by shmem (Chancellor) on Jan 13, 2008 at 19:07 UTC | |
Re^4: Lexical scoping like a fox
by graff (Chancellor) on Jan 13, 2008 at 17:58 UTC |