develooper Front page | perl.perl5.porters | Postings from January 2004

RE: [perl #24793] Warning for logical short-circuit at compile time

Thread Previous | Thread Next
Avis, Ed
January 5, 2004 02:34
RE: [perl #24793] Warning for logical short-circuit at compile time
Message ID:
Elizabeth Mattijsen via RT writes:

>>...because normally you don't write logical tests where the
>>result is known at compile time.
>I would disagree with that.  I could envision an if depending on two 
>compile time constants:
>   use constant FOO => 1, BAR => 0;
>   if (FOO and BAR) {
>   }

I mentioned later in the message that compile-time constants are used,
and so the warning could be restricted to cases where the LHS is a

>>     my $fh = new FileHandle ("< $fileName" || die);
>>optimized at compile time to
>>     my $fh = new FileHandle "< $fileName";
>I'm not that an experienced opcode reader, but at least in 5.8.2, the 
>-die- is _not_ optimized away:

Sorry - I gave the wrong example.  What I meant to say was

    my $fh = new FileHandle "< filename" || die;

In this case the 'die' is optimized out, and this is the case I thought
could have a warning - where the LHS of the logical expression is a
constant string.  Programmers are unlikely to write that unless
something a bit fishy is happening.

(Hmm, you could also argue that since "X $foo" is always true no matter
what the value of $foo, the optimizer could be a bit cleverer and spot
that.  This would normally be a fairly pointless optimization, but if it
enabled a warning to be issued for some common mistakes it'd be handy.)

>If anything, I think a warning for using indirect object 
>syntax (which I think is the root cause of this problem) would be 
>more appropriate.

A first step towards that would be purging the indirect object syntax
from the perl core documentation and from the source code of the core
modules.  In the meantime, it's much too easy for the novice programmer
to look at

    open(FH, 'file') || die;

    my $fh = new FileHandle '<file';

and combine the two to get

    my $fh = new FileHandle '<file' || die;

A warning for short-circuit where LHS is a constant string (or an
always-true string) would catch quite a lot of similar precedence bugs.
I can make a patch for it, but I won't if the majority opinion here is
that the warning isn't needed.

Ed Avis <>

Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About