Re: Perl realted..?
On Fri, Jan 11, 2008 at 01:51:44AM -0800, pedxing wrote:
> But the point is that scope doesn't change in a loop, but it
> does in a subroutine.
You are incorrect. Every set of { braces } is its own (sub)scope,
regardless of whether they are separated out into a sub or not.
~$ perl -w -e ' { my $last = "foo"; } print "$last\n"; '
Name "main::last" used only once: possible typo at -e line 1.
Use of uninitialized value in concatenation (.) or string at -e line 1.
~$ perl -w -e ' my $last; { $last = "foo"; } print "$last\n"; '
foo
Or, if you like the code from the original question better, try
perl -w -e 'while (<>) { my $last = $_ } print "$last\n"; '
and it *will* complain about $last being undefined after the loop exits:
~$ perl -w -e 'while (<>) { my $last = $_ } print "$last\n"; ' < .bashrc
Name "main::last" used only once: possible typo at -e line 1.
Use of uninitialized value in concatenation (.) or string at -e line 1, <> line 51.
--
I reckon we are now the only monastry ever that had a dungeon stuffed with
sixteen thousand zombies.
- perlmonks.org
Reply to: