Search code examples
perlnotation

Notation in Perl - what does the ||= operator do?


I happen to meet a perl code with the following syntax.

sub new{
my ($class, $value)=@_;
$lobby ||= bless{
e=>undef;},$class
}

what does the syntax ||= mean?

I failed to google it as a key word, and I could not find similar syntax in the perldoc.


Solution

  • You'll find the meaning of operators in perlop.

    Now what it does: $lhs ||= $rhs is equivalent to $lhs = $lhs || $rhs. This means that $rhs is assigned to $lhs if $lhs is false in the Perlish sense. This can be if $lhs is undef, if it is an empty string, a number that is 0.