Anyways, in case someone is interested, here's the test code. I've modified it to show the problem with return; it won't work on Windows as is, you'll have to remove the 2>/dev/null.
#!/usr/bin/perl -w use strict; { my $const_return = 'BEGIN { eval "sub HAS () { return 1 }" }'; my $const_noreturn = 'BEGIN { eval "sub HAS () { 1 }" }'; my $const_use = 'use constant; BEGIN { constant->import( HAS +=> 1 ) }'; my $paren = 'print HAS;'; my $noparen = 'print HAS();'; print( 'return, with parens ', run($const_return, $paren ), +"\n", 'return, without parens ', run($const_return, $noparen), +"\n", 'noreturn, with parens ', run($const_noreturn, $paren ), +"\n", 'noreturn, without parens ', run($const_noreturn, $noparen), +"\n", 'use, with parens ', run($const_use, $paren ), +"\n", 'use, without parens ', run($const_use, $noparen), +"\n", ); } sub run { my($code) = join ' ', @_; my @normal = qx{perl -wle \Q$code\E }; my @deparse = qx{perl -MO=Deparse -wle \Q$code\E 2>/dev/null}; chomp(my $normal_output = $normal[-1]); chomp(my $deparse_output = $deparse[-1]); return join ' ', $normal_output, $deparse_output; }
And some example output, with 5.12.2:
> px 5.12.2 ./constant-folding.pl return, with parens 1 print HAS; return, without parens 1 print HAS; noreturn, with parens 1 print 1; noreturn, without parens 1 print 1; use, with parens 1 print 1; use, without parens 1 print 1;
In reply to Re: constants wont optimize
by Somni
in thread constants wont optimize
by patcat88
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |