Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

(lestrrat) Do use 'constant': Re(4): Am I missing something here?

by lestrrat (Deacon)
on Nov 08, 2001 at 23:09 UTC ( #124135=note: print w/ replies, xml ) Need Help??


in reply to Re: Re: Re: Am I missing something here?
in thread Am I missing something here?

I'm sure your way is more efficient compared to calling a sub all the time, but I would definitely push for "use constant" in this scenario, especially if you're concerned with efficiency

Suppose the following is in a file called "debug.pl":

*DEBUG = \1; use constant CONST_DEBUG => 1; $DEBUG && print "foo\n"; CONST_DEBUG && print "bar\n";

They look pretty much the same, but you should see the output of perl -MO=Deparse debug.pl

## Output of perl -MO=Deparse on my machine with perl 5.6.1: *DEBUG=\(1); sub CONST_DEBUG() { package constant; $scalar; } print "foo\n" if $DEBUG; # <- $DEBUG must be evaluated each time print "bar\n"; # <- there's no evaluation... just straight print()'ing

There you see that the second option gives you a literal print "bar\n" without needing to evaluate another variable.

This is because of a couple of reasons: 1 - the perl compiler is smart enough to optimize away "real" constants... and 2 - *DEBUG = \1 is NOT a "real" constant. (You can always assign on top of *DEBUG if you so choose to).


Comment on (lestrrat) Do use 'constant': Re(4): Am I missing something here?
Select or Download Code
Re: (lestrrat) Do use 'constant': Re(4): Am I missing something here?
by Biker (Priest) on Nov 09, 2001 at 01:18 UTC

    And Biker humbly bowes his head in respect. Amen.

    f--k the world!!!!
    /dev/world has reached maximal mount count, check forced.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (4)
As of 2014-09-19 02:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (129 votes), past polls