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

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

Thread Previous
From:
Elizabeth Mattijsen
Date:
January 3, 2004 04:21
Subject:
Re: [perl #24793] Warning for logical short-circuit at compile time
Message ID:
p05111b00bc1c6227df8f@[192.168.56.3]
At 08:48 +0000 1/2/04, avised@kbcfp.com (via RT) wrote:
>...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) {
   }


>For example, this is a mistake that caused some bother:
>
>     my $fh = new FileHandle "< $fileName" || die;
>
>The precedence of || means that the code is equivalent to
>
>     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:

$ perl -MO=Concise -e 'my $fh = new FileHandle "< $fileName" || die;'
f  <@> leave[1 ref] vKP/REFC ->(end)
1     <0> enter ->2
2     <;> nextstate(main 1 -e:1) v ->3
e     <2> sassign vKS/2 ->f
c        <1> entersub[t5] sKS/TARG ->d
3           <0> pushmark s ->4
4           <$> const(PV "FileHandle") sM/BARE ->5
-           <1> null lKM/1 ->b
8              <|> or(other->9) lKM/1 ->b
-                 <1> ex-stringify sK/1 ->8
-                    <0> ex-pushmark s ->5
7                    <2> concat[t2] sK/2 ->8
5                       <$> const(PV "< ") s ->6
-                       <1> ex-rv2sv sK/1 ->7
6                          <$> gvsv(*fileName) s ->7
a                 <@> die[t4] sK ->b
9                    <0> pushmark s ->a
b           <$> method_named(PVIV "new") s ->c
d        <0> padsv[$fh:1,2] sRM*/LVINTRO ->e


>It's unlikely the programmer intended to write an || expression which
>could be short-circuited in this way before the program runs.
>Therefore, it would be a good idea to add a warning.

I disagree.  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.  But that's not going to happen for Perl 5, I don't 
think.


Liz

Thread Previous


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