http://www.perlmonks.org?node_id=818760


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;