Re: fishy conditional expression
- From: "Paul Lalli" <mritty@xxxxxxxxx>
- Date: 29 Jan 2007 12:05:18 -0800
On Jan 29, 12:13 am, kloro2...@xxxxxxxxx (Tom Arnall) wrote:
the following script:
if (@f = `cat nonExistentFile` ) {
print 1;
}
if (@f = `cat nonExistentFile` && (1==1)) {
print 2;
}
gets:
cat: nonExistentFile: No such file or directory
cat: nonExistentFile: No such file or directory
2
seems to me both print statements should be skipped. what am i missing?
The fact that you changed the contexts. In the first, the backticks
are evaluated in a list context. They therefore return the empty
list. @f therefore contains the empty list. @f is then evaluated in
a boolean (ie, a scalar) context. An empty array in scalar context is
0, which is false, so the if block is skipped.
In the second, the backticks are part of the && conjunction. && takes
scalars on either side. So the backticks return the empty string.
The equality returns 1. ('' && 1) returns the false, represented as
the empty string. @f therefore contains one element, the empty
string. @f is then evaluated in a scalar context, and returns 1. 1
is true, so the if block is executed.
Hope that helps,
Paul Lalli
.
- References:
- libgdbm
- From: Daniel D Jones
- fishy conditional expression
- From: Tom Arnall
- libgdbm
- Prev by Date: Re: fishy conditional expression
- Next by Date: I don't know where to start with this one.
- Previous by thread: Re: fishy conditional expression
- Next by thread: selecting the next line of a matching
- Index(es):
Relevant Pages
|