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

Re: Variable scope issue- effect of "my" on hash ref keys, values

by tobyink (Canon)
on Feb 26, 2020 at 13:16 UTC ( [id://11113436]=note: print w/replies, xml ) Need Help??


in reply to Variable scope issue- effect of "my" on hash ref keys, values
in thread Behaviour of parsed XML

Why would the "extra fun" bit be in the output? You declared two separate $x variables and they're each references to different hashes. If you put data into one hash, it shouldn't appear in the other one.

The only arguable improvement which could be made to Perl with regard to this, is that my $x->{a}->[0] is kinda weird and might be worth warning about. Really that's just a precedence thing though. my is higher precedence than the deref operator, so it just means (my $x)->{a}->[0].

Replies are listed 'Best First'.
Re^2: Variable scope issue- effect of "my" on hash ref keys, values
by parv (Parson) on Feb 26, 2020 at 13:24 UTC

    Ha! Right you are. I had missed the fact a spanking new variable was created inside the block (when assigning to the hash reference keys). No "extra fun" for me, it's obvious now.

    I was mistakenly thinking that my operator was affecting the existence of keys, values (in that {a} element was autovivified and only [0] element was localized to the block) but not the $x created earlier outside the block. Much thanks to you both for the clue bat.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (6)
As of 2024-04-19 04:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found