Monday, March 9, 2009

Establishing a Default Value

# use $b if $b is true, else $c
$a = $b || $c;

# set $x to $y unless $x is already true
$x ||= $y

# use $b if $b is defined, else $c
$a = defined($b) ? $b : $c;

$foo = $bar || "DEFAULT VALUE";

$dir = shift(@ARGV) || "/tmp";

$dir = $ARGV[0] || "/tmp";

$dir = defined($ARGV[0]) ? shift(@ARGV) : "/tmp";

$dir = @ARGV ? $ARGV[0] : "/tmp";


# find the user name on Unix systems
$user = $ENV{USER}
|| $ENV{LOGNAME}
|| getlogin()
|| (getpwuid($<))[0]
|| "Unknown uid number $<";


@a = @b unless @a; # copy only if empty
@a = @b ? @b : @c; # assign @b if nonempty, else @c

No comments: