Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re^2: gotchas with hash element autovivification (dry)

by tye (Sage)
on Apr 03, 2012 at 01:28 UTC ( [id://963159]=note: print w/replies, xml ) Need Help??


in reply to Re: gotchas with hash element autovivification
in thread gotchas with hash element autovivification

if ($rec{NOTE­} && exists $rec{NOTE}­{Nested}) {

More-DRY versions:

if( exists ${ $rec{NOTE­} || {} }{Nested} ) { # or if( ( $rec{NOTE­} || {} )->{Nested} ) { # or if( exist +( $rec{NOTE­} || {} )->{Nested} ) {

Though, I find that I use those less often to avoid auto-viv and more often to prevent a potential fatal "Can't use undef as HASH ref" (something that I think should at least be turned into just a warning -- since sometimes it is fatal and sometimes it is a silent success, and I'm not even sure what distinguishes between those two behaviors).

- tye        

Replies are listed 'Best First'.
Re^3: gotchas with hash element autovivification (dry)
by JavaFan (Canon) on Apr 03, 2012 at 06:22 UTC
    It's a matter of opinion, but I prefer the simplicity of my suggestion, even if there's repetition. Even though I sometimes use your technique (typically in cases like keys %{$hash{foo} || {}}), I still had to look twice.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (6)
As of 2024-04-19 13:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found