[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: Perl realted..?



On Jan 11, 2008 4:01 AM, pedxing <pedxing@gmail.com> wrote:
> #!/usr/bin/perl
> # this just re-implements tail -1
> #
> # usage:
> # /this/file < /some/text/file.txt
> #
>
> my $last;
>
> while( <> ) {
> $last = $_;
> }
>
> print $last;
> -----------------------------
>
> Now, try with the "my $last;" *inside* the while(). That last print
> line won't have a clue what $last is.

That doesn't sound right to me.  Scope doesn't change in a loop.  All
of this code is in the same scope, so the print statement will work
fine.

Try again.

----8<----
$ perl -le 'my $last = 1; while (true) { my $last = 2; last; } print $last;'
1
----8<----

--
Chris Howie
http://www.chrishowie.com
http://en.wikipedia.org/wiki/User:Crazycomputers
Reply to: