[tpm] Stupid question time!

Uri Guttman uri at StemSystems.com
Fri Jan 29 20:53:03 PST 2010

>>>>> "SF" == Shaun Fryer <sfryer at sourcery.ca> writes:

  SF> The most common example would be creating a hash which I'm
  SF> populating with values, and don't want a given key to exist() if
  SF> the right hand side is undef.

  SF> my %foo;
  SF> $foo{bar} = $baz if $baz;

well, that doesn't check for undef, just truth. add defined in there if
you are checking for undef.

if you are populating a whole hash from another one (as in adding or
setting options with defaults) there are better ways than doing it one
at a time. i won't put code in here unless you ask about needing such a
beast. you can do several neat tricks with hash overrides, slices, map,

  SF> In this case...

  SF> $foo{bar} = $baz // undef;

  SF> ...while interesting, isn't appropriate.

your original if modifier is about the best you can do for a scalar


Uri Guttman  ------  uri at stemsystems.com  --------  http://www.sysarch.com --
-----  Perl Code Review , Architecture, Development, Training, Support ------
---------  Gourmet Hot Cocoa Mix  ----  http://bestfriendscocoa.com ---------

More information about the toronto-pm mailing list