Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: Re: Re: Re: I need a simple explantion of the difference between my() and local()

by buckaduck (Chaplain)
on May 02, 2001 at 22:28 UTC ( [id://77455]=note: print w/replies, xml ) Need Help??


in reply to Re: Re: Re: I need a simple explantion of the difference between my() and local()
in thread I need a simple explantion of the difference between my() and local()

Update: I added an example for an if() conditional, which was the original poster's actual intent.

I think that his implication was just a clarification of a subtlety that escapes many Perl novices.

The point is that you don't have to use the my() declaration between curly braces to define its scope within a block of code. You can use my() within the condition of an if/else block:

if ( (my $name = GetName()) =~ /^\w+$/) ) { print $name; }
And the variable $name has the scope of that conditional.

You can also use the my() declaration in a loop control statement:

while (my $line = <>) { print $line; } foreach my $name (@people) { print $name; }
And the variable's scope is that of the loop. It was certainly a revelation for me, once upon a time...

buckaduck

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://77455]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (5)
As of 2024-04-25 10:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found