Re: fishy conditional expression



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

.



Relevant Pages

  • Re: In Australia, Id prefer if people vote on CO2 reductions.
    ... pack Amerrorica, people are trying to buy a hundred empty houses, of ... the joy of owning hundreds of thousands of empty homes. ... As the cat swings around it growls a bit and the young folks think ...
    (rec.audio.tubes)
  • Re: concatenate files
    ... some of those files end without newlines or with one or more newlines. ... cat only prints to std output and not to another file unless I know ... treat hold space as empty ... b H # branch to non-empty hold state ...
    (comp.unix.shell)
  • problem with perl + thread and extension
    ... struct MqS * const context, ... ret = ProcError (aTHX_ context, ... create an error: the argument stack is empty and: ...
    (comp.lang.java.programmer)
  • Re: Bettys little absence
    ... thigh, kneading a blanket, pawing at her cat dancer, *something*. ... There's an empty cat bed, an empty food dish, an ... I still miss my Feather but WooToo helps me remember him without ... not replace Feather and I did not replace my Mom in WooToo's life, ...
    (rec.pets.cats.anecdotes)
  • Re: Bettys little absence
    ... thigh, kneading a blanket, pawing at her cat dancer, *something*. ... There's an empty cat bed, an empty food dish, an ...
    (rec.pets.cats.anecdotes)