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

[OT] Re: Perl realted..?



pedxing <pedxing@gmail.com>:
>  On Jan 11, 4:20 am, "s. keeling" <keel...@nucleus.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.
> 
>  my $last;
>  my $bar;
>  while( <> ){
>    $last="foo";
>    $bar=different_scope($_);
> }
>  print $bar;
>  print $last;
> 
>  sub different_scope{
>    my $last=$_[0];
>    return $last;
> }

I haven't tested it.  Saved for posterity though.

>  Actually, this is a better example, because it will actually work

As does mine.

>  #;-).  But the point is that scope doesn't change in a loop, but it
>  does in a subroutine.

You should have tested my code.  :-)  Thanks to the others for their
informative comments.


-- 
Any technology distinguishable from magic is insufficiently advanced.
(*)    http://blinkynet.net/comp/uip5.html      Linux Counter #80292
- -    http://www.faqs.org/rfcs/rfc1855.html    Please, don't Cc: me.


Reply to: