Re: How to export constants to a module?



* Harris More schrieb:

> How do I export a constant to a pm module? Or maybe the question is, How
> do I make a constant in the caller routine visible to modules?

You want to export a constant *to* a module, not exporting one *from* a
module. Then, you could simply use the constant with its full name (ie.
putting the package name in front of the constant):


#!/usr/bin/perl
use strict;
use warnings;
{
package Foo;
sub func { main::FOO() }
}
use constant FOO => 42;
print Foo::func();
__END__
42


But since not only »Foo« is using the constants, it could be reasonable
to branch out all constants into a new package (remember, you're talking
about "a LOT of constants"). This way, you could decide for each package
whether the constants should be available (ie. polluting the namespace).
And then, you could use Exporter.pm to export your constants *from* this
new module as usual. Nevertheless, try to use »strict« and »warnings« in
your modules too. There's no reason to leave this out.


#!/usr/bin/perl
package Foo::Constants; # file: Foo/Constants.pm
use strict;
use warnings;
use base 'Exporter';
our @EXPORT = qw/FOO BAR/;
use constant FOO => 42;
use constant BAR => 43;
1;
__END__


#!/usr/bin/perl -w
use strict;
use warnings;
{
package Foo;
use Foo::Constants;
sub func { FOO }
}
use Foo::Constants qw( BAR ); # import BAR only, not FOO
print Foo::func(), BAR;
__END__
4243


regards,
fabian
.



Relevant Pages

  • Re: Strange behavior by regex with variable
    ... use strict; ... use warnings; ... I modified my test script as suggested by Paul and now it works fine, ... my $BAR = quotemeta q; ...
    (comp.lang.perl.misc)
  • Re: source a config file
    ... use strict; ... use warnings; ... my $bar = $}; ... Nasty, nasty stuff. ...
    (comp.lang.perl.misc)
  • Re: retrieving from opendbm
    ... use strict; ... use warnings; my %result_hash = qw(foo bar); dbmopen; ... statement clobbers whatever was previously in %result_hash. ...
    (perl.beginners)
  • Re: Non-menu menubuttons or something similar
    ... >I'm trying to build a dynamic menu bar in an application that can be ... use strict; ... use warnings; ... MjIogaC7MSgjGLqMoBoyKEFCgiQkFAiqMYJBQkIoJBQIuiGBJCQkSEKBoAsRSEJCgiQUCLqK ...
    (comp.lang.perl.tk)
  • Re: Abbreviated type declaration
    ... You're describing a namespace conflict. ... If warnings or even notes are given every time a _potential_ ... (bar x)) ... ;;; Compiling file foo2.cl ...
    (comp.lang.lisp)