Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

Re^2: &&= vivify keys?

by ikegami (Patriarch)
on Apr 18, 2012 at 21:01 UTC ( #965801=note: print w/replies, xml ) Need Help??

in reply to Re: &&= vivify keys?
in thread &&= vivify keys?

It's because what Eliya said isn't completely true. «$x{a} &&= 1;» isn't exactly equivalent to «$x{a} = $x{a} && 1;». Specifically, «$x{a}» is never assigned to itself. What really happens:

  1. The RHS of the assignment (1) is evaluated.
  2. The LHS of the assignment ($x{a}) is evaluated in lvalue context (which vivifies the hash element). [FETCH]
  3. If the scalar from the LHS is false,
    1. The scalar from the RHS is assigned to the scalar returned by the LHS. [STORE]

Notice how the store is conditional? The autovivification actually occurs on fetch.

The reason your tie code doesn't behave like the non-tie code is that you didn't autovivify the inner hash on fetch like the non-tie code does (step 2). But that's excusable -- I don't know if you can even tell you were called in lvalue context.

Replies are listed 'Best First'.
Re^3: &&= vivify keys?
by Eliya (Vicar) on Apr 18, 2012 at 22:03 UTC

    Thanks for the clarification.

    As for

    3. If the scalar from the LHS is false, ...

    I think that should be

        3. If the scalar from the LHS is true, ...

    How else could a true value in the scalar ever change?

    $ perl -E'$x=1; $x &&= 0; say $x' 0

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (2)
As of 2022-05-28 17:44 GMT
Find Nodes?
    Voting Booth?
    Do you prefer to work remotely?

    Results (99 votes). Check out past polls.