Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what does the double forward slash mean here?

Tags:

perl

I'm new to Perl and came across this piece of code at work, I search for a while but did not find the answer. Can anyone help to explain its function in plain english? thanks.

my $abc = delete $args{ 'abc' } // croak 'some information!'; 
like image 911
photosynthesis Avatar asked May 26 '14 15:05

photosynthesis


People also ask

What does a double forward slash mean?

The double slashes allow you to comment your action scripts.

What do two slashes mean in text?

The double slash '//' means: this "leads to" or, is "followed by".... try out "?//.. x..."

What does forward slash mean in an email?

Make sure to remember the following: The backslash (\) is mostly used in computing and isn't a punctuation mark. The forward slash (/) can be used in place of “or” in less formal writing. It's also used to write dates, fractions, abbreviations, and URLs.


2 Answers

From this page here: http://perldoc.perl.org/perlop.html#Logical-Defined-Or

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 of EXPR1 if it's defined, otherwise, the value of EXPR2 is returned. (EXPR1 is evaluated in scalar context, EXPR2 in the context of // itself). Usually, this is the same result as defined(EXPR1) ? EXPR1 : EXPR2 (except that the ternary-operator form can be used as a lvalue, while EXPR1 // EXPR2 cannot, and EXPR1 will only be evaluated once). This is very useful for providing default values for variables. If you actually want to test if at least one of $a and $b is defined, use defined($a // $b).

like image 70
AntonH Avatar answered Sep 28 '22 02:09

AntonH


Check for Logical Defined-Or in perlop, it is similar to || but it checks for undef value (not false one).

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.

So in short,

my $abc = delete $args{ 'abc' } // croak 'some information!'; 

will croak when $args{ 'abc' } returns undef value.

like image 20
mpapec Avatar answered Sep 28 '22 01:09

mpapec