in reply to Assignment to a value only if it is defined

If you're using Perl 5.10, you have the defined-or operator //:

$foo //= $bar; # equivalent to $foo = $bar if defined $bar;

Replies are listed 'Best First'.
Re^2: Assignment to a value only if it is defined
by JavaFan (Canon) on Jan 20, 2010 at 10:56 UTC
    Wrong. That's equivalent to
    $foo = $bar unless defined $foo;
    What the OP wants is:
    $foo = $bar // $foo;
    which is equivalent to
    $foo = $bar if defined $bar;
    assuming the absence of ties and overloading.
Re^2: Assignment to a value only if it is defined
by almut (Canon) on Jan 20, 2010 at 10:48 UTC

    not quite — $foo //= $bar would test if $foo is defined, not $bar (it's equivalent to $foo = $bar unless defined $foo).