Re: How to export constants to a module?
- From: Fabian Pilkowski <pilkowsk@xxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Tue, 19 Apr 2005 11:52:12 +0200
* 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
.
- References:
- How to export constants to a module?
- From: Harris More
- How to export constants to a module?
- Prev by Date: Re: First Line of Code - perl via Windows
- Next by Date: Re: double quote to single quote
- Previous by thread: Re: How to export constants to a module?
- Next by thread: Re: How to export constants to a module?
- Index(es):
Relevant Pages
|