I was searching in a lot of Perl books but I can't find an answer. I have this code, what I suppose it does is assign param's ticket to $ticket iff it exists if not, assign 0.
my $ticket = $params->{ticket} // 0;
//
means defined-or. $ticket
is assigned $params->{ticket}
if it is defined, 0 otherwise.
Although it has no direct equivalent in C, Perl's
//
operator is related to its C-style or. In fact, it's exactly the same as||
, except that it tests the left hand side's definedness instead of its truth. Thus,EXPR1 // EXPR2
returns the value ofEXPR1
if it's defined, otherwise, the value ofEXPR2
is returned.
It was added in 5.10.
In the code above, $params->{ticket}
can still have garbage in it, so make sure the value conforms to the expected pattern before using it.
Perl documentation says:
"EXPR1 // EXPR2 returns the value of EXPR1 if it's defined, otherwise, the value of EXPR2 is returned."
It's similar to a logic or, but testing definedness.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With