I'm trying to make it easier to follow some Perl Best Practices by creating a Constants
module that exports several of the scalars used throughout the book. One in particular, $EMPTY_STRING
, I can use in just about every Perl script I write. What I'd like is to automatically export these scalars so I can use them without defining them explicitly in each script.
#!perl
package Example::Constants;
use Exporter qw( import );
use Readonly;
Readonly my $EMPTY_STRING => q{};
our @EXPORT = qw( $EMPTY_STRING );
An example usage:
#!perl
use Example::Constants;
print $EMPTY_STRING . 'foo' . $EMPTY_STRING;
Using the above code produces an error:
Global symbol "$EMPTY_STRING" requires explicit package name
If I change the Readonly
declaration to:
Readonly our $EMPTY_STRING => q{}; # 'our' instead of 'my'
The error becomes:
Attempt to reassign a readonly scalar
Is this just not possible with mod_perl?