Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

defined or

  • //
  • defined-or

How do you set a default value of a scalar?

$x = defined $x ? $x : $DEFAULT;

$x ||= $DEFAULT;

$x //= $DEFAULT;
#!/usr/bin/perl
use strict;
use warnings;

my $val;
print defined $val ? "defined\n" : "not defined\n";   # not defined

$val //= 42;

print "$val\n";   # 42



$val = 0;

print "$val\n";   # 0

$val //= 42;

print "$val\n";   # 0