develooper Front page | perl.perl5.porters | Postings from September 2012

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

Thread Previous | Thread Next
From:
James E Keenan via RT
Date:
September 29, 2012 18:53
Subject:
[perl #24793] Warning for logical short-circuit at compile time
Message ID:
rt-3.6.HEAD-11172-1348970004-73.24793-15-0@perl.org
On Mon Jan 05 02:36:52 2004, avised@kbcfp.com wrote:
> 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
> string.
> 
> >>     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 <avised@kbcfp.com>

This ticket has not gotten any attention in eight years.  Do people feel
that a patch to the current documentation is needed?

Thank you very much.
Jim Keenan


---
via perlbug:  queue: perl5 status: open
https://rt.perl.org:443/rt3/Ticket/Display.html?id=24793

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About