Ah, Juerd's comment about return explains why I was unable to replicate your problem with any version of perl I have. I automatically corrected it when writing my test code.

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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.