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

Re: What's wrong with this local() ?

by papidave (Pilgrim)
on Jan 30, 2008 at 22:47 UTC ( [id://665235]=note: print w/replies, xml ) Need Help??


in reply to What's wrong with this local() ?

Based on the question, I think you were expecting the localized version to be initialized to the external value. I ran a quick test under Perl v5.8.5, and found that

local $A::variable = $A::variable;

gives the desired behavior. It's nice, but it doesn't have to be that way, because this behavior implies that the "local" was executed after the RHS of the assignment was evaluated.

Having said that, I don't think that precidence relationship is clearly defined in the language, and you probably shouldn't trust it for production code. Unless someone older and wiser than I knows for sure that it was planned that way?

Replies are listed 'Best First'.
Re^2: What's wrong with this local() ?
by Anonymous Monk on Jan 31, 2008 at 17:52 UTC
    I think it may have been designed that way. When I look at the operator precedence table, I notice that the '=' is two spots higher in the table than list operators when viewed from the right (local). I also see that '=' evaluates it's right side 1st. So this seems to be a case of behavior matching documentation.

      When I look at the operator precedence table, I notice that the '=' is two spots higher in the table

      Precedence is irrelevant here. And local has higher precedence.

      It wouldn't work otherwise

      Give local higher precedence >perl -le"$x=2; { (local $x)=$x; print $x; $x=3; print $x } print $x" 2 3 2 Give assignment higher precedence >perl -le"$x=2; { local ($x=$x); print $x; $x=3; print $x } print $x" 2 3 3 <-- XXX

      I also see that '=' evaluates it's right side 1st.

      Where did you see this? Operand evaluation order determines which operand is evaluated first. The operand evaluation order for = and other Perl binary operators is not documented anywhere and should probably be considered undefined.

      Perhaps you looked up the associativity of the = operator. Associativity doesn't define which operand is evaluated first, it determines which operator is evaluated first when two operators of the same precedence are chained.

      However, you are right that this feature of my and local is implemented by choosing a right to left operand evaluation order for assignment operators.

      >perl -le"local(${print('a'), 'x'}) = ${print('b'), 'x'}; b a

      Updated: Updated code into "precedence is irrelevant" reply.

Re^2: What's wrong with this local() ?
by ikegami (Patriarch) on Jan 31, 2008 at 18:02 UTC

    By design, variables declared with my are not introduced (not visible) until after the current statement. For my, this is documented in perlsub along with the rest of my's documentation.

    Similar behaviour exists for local, although I can't find it documented.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (7)
As of 2024-04-24 11:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found