in reply to 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()

if you use my() inside an if .. else conditional, it is also only visible in that scope.
I think that's just a special case of what Dominus said at Re: I need a simple explantion of the difference between my() and local():
The variable is only available inside the block in which you declared it.
Or were you implying something extra special there?

-- Randal L. Schwartz, Perl hacker

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

Replies are listed 'Best First'.
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
    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

Re: Re: Re: Re: I need a simple explantion of the difference between my() and local()
by $code or die (Deacon) on May 03, 2001 at 07:16 UTC
    It seems I have err'd

    I replied to Dominus instead of mothra - which was my original intent - mothra was talking about lexical variables in subroutines. I wanted to point out that it applies to conditional blocks as well.

    $ perldoc perldoc

    Update: Nope - unless I am 'very' short-sighted - I think I did reply to mothra.