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

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

Thread Previous
Elizabeth Mattijsen
October 1, 2012 12:35
Re: [perl #24793] Warning for logical short-circuit at compile time
Message ID:
On Sep 30, 2012, at 3:53 AM, James E Keenan via RT <> wrote:
> On Mon Jan 05 02:36:52 2004, 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 <>
> This ticket has not gotten any attention in eight years.  Do people feel
> that a patch to the current documentation is needed?

Afaic, please close.

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