1

Can someone explain me what does this if is checking and when it is passed?

if ( $rate_minimum || 0 ) > ( (4 * $rate_max_min) // 120  ):

sorry for not being a perl developer

simbabque
  • 53,749
  • 8
  • 73
  • 136
PHA
  • 1,588
  • 5
  • 18
  • 37
  • 3
    http://perldoc.perl.org/perlop.html has a nice summary – Fredrik Pihl Sep 25 '15 at 12:50
  • That is a syntax error. First off, you are missing an enclosing parenthesis. Second, the line is ended with a colon `:`, which does not belong there. And I guess third, the code is pretty pointless as written, and the main point of it would be to avoid undefined warnings (which it doesn't), which could be handled in a better way than this. – TLP Sep 26 '15 at 10:22

2 Answers2

6

|| is the boolean or operator. It will return $rate_minimum if $rate_minimum is true, and 0 otherwise. The false values are primarily 0, '' and undef.

// is very similar, but only tests defined-ness. (And is only available since Perl 5.10). This means a value of 0 still counts, and so if $rate_max_min is zero, it won't get replaced with 120. (Where it would if || had been used)

So $rate_miniumum || 0 will return $rate_minumum unless it is either: 0, an empty string or undefined. In which case the || will kick in, and it'll be zero instead.

The second part tests if $rate_max_min is defined and if it isn't, replace that value with 120. (Which allows it to be zero)

See perlop for more detail.

As a related point - you can also do ||= and //= to conditionally assign.

E.g.

my $value = undef;
$value //= 42;
print $value,"\n";
# 42 

$value = 0; 
$value //= 42;
print $value,"\n";
# 0

$value = 0;
$value ||=  42;
print $value,"\n";
# 42

Edit: As noted by melpomene

As written, (4 * $rate_min_max) // 120 is useless because the result of * is never undef.

That conditional should probably be:

4 * ( $rate_min_max // 30 )

instead.

e.g.:

my $rate_min_max = 0;
print 4 * ( $rate_min_max // 30 ),"\n";
$rate_min_max = undef;
print 4 * ( $rate_min_max // 30 ),"\n";
Community
  • 1
  • 1
Sobrique
  • 52,974
  • 7
  • 60
  • 101
  • 2
    As written, `(4 * $rate_min_max) // 120` is useless because the result of `*` is never `undef`. – melpomene Sep 25 '15 at 12:53
  • Good point. Will amend. I assume they were _trying_ to trap a `$rate_min_max` of zero, but failing. – Sobrique Sep 25 '15 at 12:54
  • 1
    Maybe they accidentally put an extra `/` in their division? – RobEarl Sep 25 '15 at 12:56
  • That's a possiblity too. – Sobrique Sep 25 '15 at 12:57
  • 1
    @RobEarl Then `* 4 / 120` could be simplified to `/ 30`. – melpomene Sep 25 '15 at 12:57
  • Hey @Sobrique, I just want to point out that defined-or (`//`) was first introduced in perl v5.10 (not 5.12). This is particularly important if one is writing a CPAN module, as 5.10 would have to be specified as the minimum version of perl that can be used for the module. http://search.cpan.org/dist/perl-5.10.0/pod/perl5100delta.pod#Defined-or_operator – stevieb Sep 25 '15 at 13:47
  • Edited the post. Thought it was 5.12, but I probably skipped 5.10 completely :) – Sobrique Sep 25 '15 at 13:49
  • 1
    Yeah, your post is great, but as soon as I saw the "IIRC", I thought I'd speak up as I was just working on one of my modules this week and had to do research on a few of the operators, and which version of perl they were brought to life in :) – stevieb Sep 25 '15 at 13:50
1

Following explanation will help you :

See || is an OR logical operator and // is 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.

Amar Singh
  • 5,464
  • 2
  • 26
  • 55