Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Using Constants in Perl

by qazwart (Scribe)
on Feb 28, 2006 at 15:24 UTC ( [id://533373]=perlquestion: print w/replies, xml ) Need Help??

qazwart has asked for the wisdom of the Perl Monks concerning the following question:

A long time ago, when I started writing Perl, I learned to make constants like this:
*FOO = \"bar";
I know that there is now a constant pragma which is used like this:
use constant FOO => "bar";
But, I never really liked it because you cannot easily interpolate constants in strings using this method:
*FOO = \"BAR"; print qq(\$FOO is set to "$FOO"\n); use constant FOO => "BAR"; print qq(Constant "FOO" is set to ) . FOO . qq(\n);
Plus, I have a tendency to do this:
use constant FOO => "BAR"; print "The value of FOO is $FOO\n";
However, I know I am in the extremely tiny minority who still use the reference method of making constants, so there must be a major advantage of using the constant pragma that made everyone want to change over.

What are the advantages of the newer constant pragma vs. the older reference method of declaring a constant?

Replies are listed 'Best First'.
Re: Using Constants in Perl
by davorg (Chancellor) on Feb 28, 2006 at 15:47 UTC

    In PBP, Damian recommends Readonly instead of use constant.

    --
    <http://dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

      Just to elaborate a bit (I happened to read this chapter in PBP yesterday). He recommends Readonly because bareword constants (ala use constant) can't be interpolated. Moreover, Readonly allows lexically scoped variables to be created at runtime, but use constant creates package scoped variables at run time.

        A Better reason that I find for using Readonly is that is makes your code easier to understand. If you do not have Readonly with XS compiled then you take a performance hit but you generally only see that using a Readonly hash.

        Another Advantage Readonly has over use constant is that you can declare a hash, you cannot do that with use constant.

Re: Using Constants in Perl
by Fletch (Bishop) on Feb 28, 2006 at 15:25 UTC

    Well for one thing, like perldoc constant says . . .

    When a constant is used in an expression, perl replaces it with its value at compile time, and may then optimize the expression further. In particular, any code in an "if (CONSTANT)" block will be optimized away if the constant is false.
Re: Using Constants in Perl
by diotalevi (Canon) on Mar 01, 2006 at 01:30 UTC

    use constant because then the perl compiler can use the preferred method to constantify it. In newer versions of perl (like say... 5.10), constants are much smaller but only if you're actually using the pragma that's been provided to you. In general, people making constants manually will just lose out and not get the benefit of the improvement.

    So the point is, use the pragma and upgrades to perl can upgrade how your code runs.

    pragmas are written with your version of perl's implementation in mind. When you fake that out, you're deciding on a particular version's functionality. The version you're using now doesn't necessarily benefit from the method you used.

    ⠤⠤ ⠙⠊⠕⠞⠁⠇⠑⠧⠊

Re: Using Constants in Perl
by xdg (Monsignor) on Feb 28, 2006 at 21:48 UTC

    What might be nice is something which did both:

    use bimodal_constant FOO => "BAR"; print "\$FOO is $FOO\n" if FOO;

    That seems as simple as:

    package bimodal_constant; sub import { my ($class, $key, $value) = @_; my $caller = caller(); # constant scalar eval " *$caller\::$key = \\\"$value\" "; # constant sub eval " *$caller\::$key = sub () { \"$value\" } "; } 1;

    When used with the example at the top, it prints "$FOO is BAR" as expected.

    Of course, a shorter name would be helpful.

    Update: glancing at the source for constant, there is a lot of sanity checking so it's not quite that easy, but the sanity checks could be cut and paste. All the action is in the last few lines of code.

    -xdg

    Code written by xdg and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.

Re: Using Constants in Perl
by theshz (Sexton) on Feb 28, 2006 at 19:12 UTC
    Readonly and "use constant" aside, what's wrong with the plain old:
    # The following are constants my $FOO = "BAR"; my @BAR = qw/This is a constant array/; my %HASH = (1=>2, 2=>3);

      They're not constants (in that they're modifiable)?

      They won't be inlined during compilation?

      That if you don't see that one comment line (because you started editing waaaay deep in the file) you could introduce bugs in other code because you inadvertently modified said "constant"?

        I'm aware of that. The reason I'm asking this is whether this is a theoretic/purist concern or a practical concern. "use strict" is useful because it's very common to make the kind of mistakes it prevents; I don't remember the last time I had a bug because I modified a constant (e.g, something named $CONST_DATABSE_NAME)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://533373]
Approved by wfsp
Front-paged by traveler
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (6)
As of 2024-04-24 08:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found