Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: Will "$_[0]=shift" always resolve shift first?

by betterworld (Curate)
on Sep 08, 2008 at 23:41 UTC ( [id://709917]=note: print w/replies, xml ) Need Help??


in reply to Will "$_[0]=shift" always resolve shift first?

I've found the following code in Perl 5.10.0's testsuite in t/op/local.t:

@a = ('a', 'b', 'c'); { local($a[1]) = 'foo'; local($a[2]) = $a[2]; is($a[1], 'foo'); is($a[2], 'c'); undef @a; }

Now, I daresay (and I hope that my head is clearer than when I wrote my last node) that this only works if "=" evaluates its right-hand operand first.

The important line is the one with the second local. As a run-time operator, local() effectively sets its operand ($a[2] in this case) to an undefined value. But then this new value will be set to the value of $a[2] before local was executed. This would not work the other way round.

Apart from this extract, local.t contains more similar tests.

In a nutshell: Even though I could not find any hints in the documentation, the test suite suggests that kyle's coworker's code is reliable.

Replies are listed 'Best First'.
Re^2: Will "$_[0]=shift" always resolve shift first?
by ikegami (Patriarch) on Sep 09, 2008 at 00:18 UTC

    Even though I could not find any hints in the documentation,

    moritz pointed out that perltrap documents the operand evaluation order for assignment operators.

Log In?
Username:
Password:

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

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

    No recent polls found