in reply to check for existing value, else set to default

Checking if a scalar has a defined value (and assigning a default value if it hasn't) is quite common. Unfortunately, the constructs
$a = $a ? $a : "default_value";
or
$a ||= "default_value";
don't work if the value of $a is something that returns false in a boolean context, like 0 or "". For now,
$a = defined $a ? $a : "default_value";
is the best way to write this, and since it's such a common idiom, Perl 6 will allow you to say
$a //= "default_value";
whenever Perl6 will come out.

Replies are listed 'Best First'.
//= is coming in perl 5.10.0
by PodMaster (Abbot) on Apr 26, 2004 at 06:37 UTC
    but its available right now as a patch => defined or: // and //=

    MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
    I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
    ** The third rule of perl club is a statement of fact: pod is sexy.