Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: Shortcut operator for $a->{'b'}=$b if $b;

by dragonchild (Archbishop)
on Sep 21, 2005 at 03:41 UTC ( #493649=note: print w/ replies, xml ) Need Help??


in reply to Shortcut operator for $a->{'b'}=$b if $b;

No-one's mentioned using tie.

package Tie::Hash::OnlyIf; sub STORE { my $self = shift; my ($key, $val) = @_; if ( $val ) { $self->{$key} = $val; } } package main; tie my %data, Tie::Hash::OnlyIf; $data{b} = $b;
The only problem with this is that the casual reader will expect that the key 'b' will exist. But, it is a solution.

Oh - and tie's a lot slower than normal hashes, but performance is almost always overvalued. Going from 0.01 to 0.08 seconds per execution isn't noticeable.


My criteria for good software:
  1. Does it work?
  2. Can someone else come in, make a change, and be reasonably certain no bugs were introduced?


Comment on Re: Shortcut operator for $a->{'b'}=$b if $b;
Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (4)
As of 2015-07-05 19:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (67 votes), past polls