Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re^2: Assignment to a value only if it is defined

by voj (Acolyte)
on Jan 21, 2010 at 16:48 UTC ( #818760=note: print w/ replies, xml ) Need Help??


in reply to Re: Assignment to a value only if it is defined
in thread Assignment to a value only if it is defined

Thanks! That perfectly fits to assign to a scalar:

sub setifdef { $_[0] = $_[1] if defined($_[1]) } setifdef $foo, $bar;

And in case of a list I'll use the (slightly less readable) 'for grep defined,' psuedo-inline operator. It only makes sense with list references (if you treat (undef) as valid list). Defined scalar values which are transformed to a list can also be handled:

@list = @{$_} for grep defined, $listref_or_undef; @list = split(",",$_) for grep defined, $splitme_if_defined;


Comment on Re^2: Assignment to a value only if it is defined
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (9)
As of 2015-07-07 10: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 (88 votes), past polls