I'm a long-time C programmer starting on Perl. I always begin by
assuming things are C-ish and discover that sometimes they aren't.
Here's a statement I'm wondering about that seems to be all over the
place in Perl. Generically:
$a = b() or c();
Here there is a precedence issue between "=" (the assignment operator)
and 'or'. In Perl, 'or' is a lower-precedence syntactic sugar version
of the boolean 'or' operator, '||'. In this case, is it equivalent to
( $a = b() ) || c();
What you seem to be asking about, though, is the side-effect of evaluating
boolean expressions (which, AIUI, is done in C too). The above first
evaluates the expression "$a = b()", which will be the result of the
assignment of the return value of b() to $a, which will be whatever b()
returned. If that value is not true, then c() is called and it's return
value will be discarded. Either way, $a gets assigned to whatever b()
returned. If you had written
$a = b() || c();
where '||' has higher precedence than the assignment operator, then first
b() is called, if it's return value is true, then $a is assigned to that,
and c() isn't called. However, if it's return value is false, then c() is
called and whatever it returns is assigned to $a.
Does the "or" check an internal Perl program field, or the final value
of $a, or the return from b()? I think I'd understand it if someone
would tell me whether z() is executed in the following statements:
$a = 0 or z();
In that statement, z() will never be executed. If you were to write that as
$a = ( 0 or z() );
or
$a = 0 || z();
Then z() will be called every time.
Is that the same as:
if ( $a = 0 ) {
z();
}
Well, the '=' is the assignment operator, you are probably thinking
of the numeric comparison operatot, '==', or even the string comparison
operator, 'eq'. And in this case is is about evaulation, so it is closer
to:
if ( 0 ) {
$a = 0;
}
else {
$a = z();
}
...and how about the simple:
a() or z();
Or am I still missing the point?
The boolean operators are often used for their side effects as much as for
their meaning in a boolean context. Perl will evaluate the left side of
an expression containing '||' or '&&' first, and might evaluate the right
side depending on the result.
The point to remember is that for '||' (or 'or' in the appropriate context),
if the left side is true then the right side is not evaluated; whereas with
'&&' (or 'and' in the appropriate context) if the left side is false the
right side is not evaluated. You may see this effect used widely in code
that looks like
function( @args ) or die "Error: $!\n";
Here, function() is expected to return a true value, and in the case where
it does not, the program exits with an error. It would do so even if you
were capturing the return value:
my $return = function( @args ) or die "Error: $!\n";
Here, the assignment operator has higher precedence than 'or', like the
first example above. However, if the return value of function() is false,
even though it is assigned successfully to $return, the assignment operator
always returns the value assigned even if it is a false value, and in this
case that means the left hand side evaulates as false, so the right hand side,
die() is evaluated.
I recommend reading the perlop man page thoroughly, that is the place that
this material is covered in depth. If you're impatient, jump to the section
headed: "C-style Logical And".
--Damian