How to export Readonly variables using mod_perl?

I am trying to simplify the implementation of some Perl Best Practices by creating a module Constantsthat exports several scalars used throughout the book. In particular, $EMPTY_STRINGI can use almost every Perl script that I write. I would like to automatically export these scalars so that 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 );

Usage example:

#!perl
use Example::Constants;
print $EMPTY_STRING . 'foo' . $EMPTY_STRING;

Using the above code results in an error:

Global symbol "$EMPTY_STRING" requires explicit package name

If I changed the declaration Readonlyto:

Readonly our $EMPTY_STRING => q{}; # 'our' instead of 'my'

The error will be:

Attempt to reassign a readonly scalar

Is this not possible with mod_perl?

+3
source share
3 answers

Readonly. Readonly mod_perl, - .

, , ... , : -)

- Eric

+3

4 :

  • strict warnings pragmas
  • base ( @ISA)
  • (.. our).

.

package Example::Constants;

use strict;
use warnings;
use base 'Exporter';
use Readonly;

Readonly our $EMPTY_STRING => q{};
our @EXPORT = qw( $EMPTY_STRING );

1;

, , , . , mod_perl . , .

+2

mod_perl , . , .

Scalar::Util::readonly, , .

#!perl
package Example::Constants;

use Exporter qw( import );
use Readonly;
use Scalar::Util qw(readonly);

our $EMPTY_STRING;
our @EXPORT = qw( $EMPTY_STRING );

if ( !readonly( $EMPTY_STRING ) ) {
    Readonly $EMPTY_STRING => q{};
}

use vars:

#!perl
package Example::Constants;

use Exporter qw( import );
use Readonly;
use vars qw( $EMPTY_STRING );

Readonly $EMPTY_STRING => q{};
our @EXPORT = qw( $EMPTY_STRING );

typeglob:

#!perl
package Example::Constants;

use Exporter qw( import );
use Readonly;

our $EMPTY_STRING;
*EMPTY_STRING = \q{};
our @EXPORT = qw( $EMPTY_STRING );

typeglob , ( ) .

0

Source: https://habr.com/ru/post/1706216/


All Articles