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

Re: Perl realted..?



On Jan 11, 4:20 am, "s. keeling" <keel...@nucleus.com> wrote:
> ISHWAR RATTAN <rat...@cps.cmich.edu>:
>
> >  I am coming back to perl after a long time.
>
> >  The sample code these days also uses variable attribute my as:
>
> >     my $inst = Extutils::Installed->new();
> >     my @modules = $inst->modules();
>
> >  Can any demistify 'my' for me??
>
>       -----------------------------
>            #!/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;
}

Actually, this is a better example, because it will actually work
#;-).  But the point is that scope doesn't change in a loop, but it
does in a subroutine.

-pedxing


Reply to: