How do I use constants from a Perl module?
Asked Answered
M

6

46

If I define a constant in a Perl module, how do I use that constant in my main program? (Or how do I call that constant in the main program?)

Monocotyledon answered 10/10, 2008 at 21:6 Comment(2)
Best to avoid the "constant" package. earino.wordpress.com/2013/02/27/… Alternatives here: neilb.org/reviews/constants.htmlLewd
@Lewd Just because one guy hates them clashing with Perl bareword behavior in hash initializers? Thanks, but no, thanks. Tons of core modules use it without problem. Encode, Compress::Raw::Bzip2/Zlib, Data::Dumper, File::Spec, etc.Welladvised
G
52

Constants can be exported just like other package symbols. Using the standard Exporter module, you can export constants from a package like this:

package Foo;
use strict;
use warnings;

use base 'Exporter';

use constant CONST => 42;

our @EXPORT_OK = ('CONST');

1;

Then, in a client script (or other module)

use Foo 'CONST';
print CONST;

You can use the %EXPORT_TAGS hash (see the Exporter documentation) to define groups of constants that can be exported with a single import argument.

Update: Here's an example of how to use the %EXPORT_TAGS feature if you have multiple constants.

use constant LARRY => 42;
use constant CURLY => 43;
use constant MOE   => 44;

our @EXPORT_OK = ('LARRY', 'CURLY', 'MOE');
our %EXPORT_TAGS = ( stooges => [ 'LARRY', 'CURLY', 'MOE' ] );

Then you can say

use Foo ':stooges';
print "$_\n" for LARRY, CURLY, MOE;
Gudrunguelderrose answered 10/10, 2008 at 21:10 Comment(6)
use Exporter 'import'; is better, but will require upgrading Exporter on perl's earlier than 5.8.3.Almanac
please explain why is using 'import' better?Brittanybritte
Best to avoid the "constant" package. earino.wordpress.com/2013/02/27/… Alternatives here: neilb.org/reviews/constants.htmlLewd
@nslntmnx use constant benchmarks pretty well, generally. It's true that one should read the documentation for it.Calla
See, you go blowing through an example too quickly and you mess things up -- Copy & Paste FTW! I just spent 5 minutes trying to get our %EXPORT_OK = ( word => [ 'CONST1', 'CONST2' ] ); to work. :)Orten
That doesn't seem to work if I write use constant ( LARRY => 42, CURLY => 43, MOE => 44 ); Why?Hepcat
M
27

Constants are just subs with empty prototype, so they can be exported like any other sub.

# file Foo.pm
package Foo;
use constant BAR => 123;
use Exporter qw(import);
our @EXPORT_OK = qw(BAR);


# file main.pl:
use Foo qw(BAR);
print BAR;
Mirna answered 10/10, 2008 at 21:11 Comment(0)
S
22

To expand on the earlier answers, since constants are really just subs, you can also call them directly:

use Foo;
print Foo::BAR;
Scratch answered 10/10, 2008 at 21:21 Comment(1)
Don't refer to answers as being above, because if your answer gets voted up enough, it may end up above them.Counterstatement
R
17

You might want to consider using Readonly instead of constant.

Roulade answered 11/10, 2008 at 10:22 Comment(3)
You can't do this with it: print "constant is $constant"; or this: print $hash{constant};Linkboy
Since 'use constant' creates a subroutine, there are unexpected issues => no string interpolation, difficulty in using as a hash key. See the docs for Readonly [ metacpan.org/module/Readonly#COMPARISON-WITH-use-constant ] . Readonly use the perl internals to directly mark a variable as read-only. Readonly is slow (unless using Readonly::XS) and unmaintained. See also Data::Lock and Const::Fast for different implementations.Gnarl
Actually you can interpolate constant in a string. Just use the same syntax you would use with any other function: use constant COLOR => 'red'; print "The color is @{[COLOR]}!";Upchurch
S
8
package Foo;
use Readonly;
Readonly my  $C1 => 'const1';
Readonly our $C2 => 'const2';
sub get_c1 { return $C1 }
1;

perl -MFoo -e 'print "$_\n" for Foo->get_c1, $Foo::C2'
Sartain answered 12/10, 2008 at 20:4 Comment(0)
C
7

To add to the bag of tricks, since a constant is just a subroutine you can even call it as a class method.

package Foo;
use constant PI => 3.14;

print Foo->PI;

If you have lots of constants it's a nice way to get at the occasional one without having to export them all. However, unlike Foo::PI or exporting PI, Perl will not compile out Foo->PI so you incur the cost of a method call (which probably doesn't matter).

Crustacean answered 18/10, 2008 at 9:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.