Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: constants wont optimize

by Somni (Friar)
on Jul 10, 2011 at 00:13 UTC ( [id://913560]=note: print w/replies, xml ) Need Help??


in reply to constants wont optimize

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;

Replies are listed 'Best First'.
Re^2: constants wont optimize
by Marshall (Canon) on Jul 10, 2011 at 04:20 UTC
    on Windows as is, you'll have to remove the 2>/dev/null

    On Windows the equivalent of the Unix /dev/null device, the "bit bucket", is the "file" called NUL. Note one 'L'. 2>NUL is same as 2>/dev/null on Unix.

    The case of the name NUL can be anything, nUl, etc. This name is reserved by Windows in all directories - you cannot make a file called NUL. ">type NUL" prints nothing instead of printing "file not found".

      $ perl -MFile::Spec -le " print File::Spec->devnull" nul
      perlport - Writing portable Perl

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://913560]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (7)
As of 2024-04-23 19:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found