Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the result of Perl's &&?

When I try this:

$a = 1;
$b = 2;
print ($a && $b) . "\n";

The result is 2. Why?

like image 644
korrawit Avatar asked Oct 24 '10 09:10

korrawit


People also ask

What is Perl's Prussian blue used for?

Perls's procedure may be used to identify excess iron deposits such as hemosiderin deposits (hemosiderosis) and in conditions such as hereditary hemochromatosis. Perls Prussian blue is commonly used on bone marrow aspirates to indicate levels of iron storage and may provide reliable evidence of iron deficiency.

What is Perl's stain mention its use?

Perls stain is used in pathology to detect any ferric overload. The iron, fixed in the hemosiderin, is colored blue by Prussian blue, the nuclei are highlighted by the nuclear red.

What is the Counterstain of Perl's Prussian blue?

Counterstain in 0.5% aqueous methylene blue for 2 minutes. Rinse well in distilled water.

What is the principle of Perls staining?

The principle of Pearl's Prussian blue reaction is that potassium ferrocyanide will form ferric ferrocyanide (Prussian blue) with reactive ferric salts in an acid solution. Dilute hydrochloric acid liberates loosely bound ferric iron from protein.


2 Answers

Quote perlop:

The "||", "//" and "&&" operators return the last value evaluated (unlike C's "||" and "&&", which return 0 or 1).

The resulting 2 is considered true by Perl, so that when you use the && operator in a logical condition, everything works as expected. The added bonus is that you can use the logical operators in other contexts as well:

sub say_something {
    say shift || 'default';
}

say_something('foo'); # prints 'foo'
say_something(); # prints 'default'

Or even as flow modifiers:

my $param = shift || die "Need param!";
-f $file && say "File exists.";

In the last two examples it’s good to realize that they could not work if the && and || operators did not short-circuit. If you shift a true value in on first line, there is no point evaluating the right side (die…), since the whole expression is true anyway. And if the file test fails on the second line, you don’t need to evaluate the right side again, since the overall result is false. If the logical operators insisted on evaluating the whole expression anyway, we could not use them this way.

like image 138
zoul Avatar answered Oct 01 '22 04:10

zoul


That's how the && operator works: if the left-hand argument evaluates as true, the value of the expression is that of the value of the right-hand argument. This is covered on the perlop page.

However, you've also let yourself open to a much more subtle problem. You'll find that the newline doesn't get printed. This is because if you put an expression in brackets after print (or any other function name) the arguments passed to print are just those in the brackets. To get notice of this, make sure you switch on warnings. Put these lines at the top of each program:

#!/usr/bin/perl -w

use strict;

until you understand them enough to decide for yourself whether to continue with them. :-)

In your case, you'll get this:

print (...) interpreted as function at p line 7.
Useless use of concatenation (.) or string in void context at p line 7.
like image 36
Tim Avatar answered Oct 01 '22 02:10

Tim