Reputation: 4777
This Perl code is part of the variable declaration at the beginning of a piece of code. What does it mean?
my $EXPLICIT_YEAR = $ALL_PAGES ? 0 : ($store{year} || $current_year);
Upvotes: 0
Views: 171
Reputation:
I know 0 perl but a lot of C, so taking a guess here:
Set variable EXPLICIT_YEAR to
( if ALL_PAGES == true then 0
else ( (get store indexed at year) or current_year ))
The or operation is
false or false = false
false or true = true
true or false = true
true or true = true
Upvotes: 0
Reputation: 67900
my $EXPLICIT_YEAR = $ALL_PAGES ? 0 : ($store{year} || $current_year);
This expression is using the Ternary "?:" operator, combined with a subexpression using the ||
C-style logical OR. See perldoc perlop.
$ALL_PAGES ?
The expression before the ?
- the condition - is evaluated as a boolean expression. A true value meaning any value that is not zero, the empty string or undefined (not declared).
0 : ( $store{year} || $current_year )
The values on either side of :
are the values to be returned, depending on the return value of the condition. If the condition evaluates true, return the leftmost value, otherwise the rightmost. The leftmost value is simply zero 0
.
$store{year} || $current_year
The rightmost value is an expression itself, using the C-style logical OR operator. It will return the leftmost value, if it evaluates true (and ignore the rightmost). Otherwise it will return the rightmost value. So:
Upvotes: 1
Reputation: 1464
It's equivalent to this:
my $EXPLICIT_YEAR;
if ($ALL_PAGES) {
$EXPLICIT_YEAR = 0;
}
else {
# $EXPLICIT_YEAR = $store{year} || $current_year;
if ($store{year}) {
$EXPLICIT_YEAR = $store{year};
}
else {
$EXPLICIT_YEAR = $current_year;
}
}
The $conditional ? $true : $false
portion is a ternary if.
The $store{year} || $current_year
portion uses the fact that the || operator returns the first value that evaluates to true, allowing $current_year to be used if $store{year} is "false" (zero, empty string, etc.)
Upvotes: 4
Reputation: 651
i think thats a way of doing a if/then/else see here: http://www.tutorialspoint.com/perl/perl_conditions.htm
Upvotes: 0
Reputation: 1159
I'm not a Perl developer, but I'm 99% sure (this holds in most other languages) that it equates to this: If the variable $ALL_PAGES
is true (or 1), then 0 is evaluated, and if not, ($store{year} || $current_year)
is evaluated.
Upvotes: 0