Linux Archive

Linux Archive (http://www.linux-archive.org/)
-   Debian User (http://www.linux-archive.org/debian-user/)
-   -   Perl realted..? (http://www.linux-archive.org/debian-user/34092-perl-realted.html)

ISHWAR RATTAN 01-10-2008 05:11 PM

Perl realted..?
 
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??

-ishwar


--
To UNSUBSCRIBE, email to debian-user-REQUEST@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org

"Chris Howie" 01-10-2008 05:23 PM

Perl realted..?
 
On Jan 10, 2008 1:11 PM, ISHWAR RATTAN <rattan@cps.cmich.edu> wrote:

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??


-ishwar

"my" declares a variable that exists within the current scope and is unreachable from outside the scope.* So a "my" declaration in one package cannot be reached from another package.* "our" is identical to "my" except that you *can* reach that variable from other packages.* A "my" declaration inside of a function just limits the scope and lifetime of the variable to that function.

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

Arthur Furlan 01-10-2008 05:32 PM

Perl realted..?
 
ISHWAR RATTAN escreveu:

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??


http://perldoc.perl.org/functions/my.html

--
Atenciosamente,

Arthur Furlan
arthur@mpsinf.com.br




-ishwar



--
To UNSUBSCRIBE, email to debian-user-REQUEST@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org

"s. keeling" 01-11-2008 02:09 AM

Perl realted..?
 
ISHWAR RATTAN <rattan@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.


--
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.


--
To UNSUBSCRIBE, email to debian-user-REQUEST@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org

pedxing 01-11-2008 08:01 AM

Perl realted..?
 
On Jan 11, 4:20*am, "s. keeling" <keel...@nucleus.com> wrote:
> ISHWAR RATTAN <rat...@cps.cmich.edu>:
>
> > *I am coming back toperlafter 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.
>

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.

If you move the loop to a subroutine, then the two $last variables
will be independant due to separate scopes:

my $last="foo";
$bar=scan_loop();
print $bar;
print $last;
sub scan_loop{
my $last;
while( <> ){
$last=$_;
return $last;
}
}

This will print out the last line and then "foo" because there are two
$last values, the one in the original scope and the one in the
subroutine scope.

-pedxing


All times are GMT. The time now is 12:23 AM.

VBulletin, Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.