How to import constants into several modules in Perl?

I am writing a Perl application with several modules. I want to write some global constants that will be visible everywhere, for example:

#Constants.pm
$h0 = 0;
$scale = 20;

And then use them without qualification with main::or Constants::in several modules. However, if I write use Constants;in more than one module, they are imported into only one namespace. Is there any way around this?

I am using the latest ActivePerl.

+5
source share
5 answers

You can put this at the beginning Constants.pm:

package main;

In this case, all the variables you specify will be in the namespace main:

$main::x

:

package;

:

$::x

, package , -, Perl. . .


man perlfunc:

       package NAMESPACE
       package Declares the compilation unit as being in the given
               namespace.  The scope of the package declaration is
               from the declaration itself through the end of the
               enclosing block, file, or eval (the same as the "my"
               operator).  All further unqualified dynamic identifiers
               will be in this namespace.  A package statement affects
               only dynamic variables--including those you've used
               "local" on--but not lexical variables, which are cre?
               ated with "my".  Typically it would be the first decla?
               ration in a file to be included by the "require" or
               "use" operator.  You can switch into a package in more
               than one place; it merely influences which symbol table
               is used by the compiler for the rest of that block.
               You can refer to variables and filehandles in other
               packages by prefixing the identifier with the package
               name and a double colon:  $Package::Variable.  If the
               package name is null, the "main" package as assumed.
               That is, $::sail is equivalent to $main::sail (as well
               as to $main'sail, still seen in older code).

               If NAMESPACE is omitted, then there is no current pack?
               age, and all identifiers must be fully qualified or
               lexicals.  However, you are strongly advised not to
               make use of this feature. Its use can cause unexpected
               behaviour, even crashing some versions of Perl. It is
               deprecated, and will be removed from a future release.


: : Perl?

+2

Exporter perlmod.

+7

, . kudos lkundrak.

package Constants;

use base qw/Exporter/;

use constant BOB => 666;
use constant ALICE => 555;

sub import {
    no strict "refs";

    ${[caller]->[0].'::'}{$_} = ${__PACKAGE__."::"}{$_}
        foreach grep { not /^(ISA|isa|BEGIN|import|Dumper)$/ } 
            keys %{__PACKAGE__."::"};
}
+7

, , Perl . , , , :

{ package Foo;
our $global = 42; }

{ package Bar;
say "global is $global"; }

, $global $Foo::global. , , , "" , @INC, %ENV, $_ .. , , main.

, . "" main. , - :

{ package Constants;
  $_{PI} = 3.141592; }

{ package Foo;
  say "pi is $_{PI}"; }

.

( $ENV, &INC ..)

- , , - :) , , - .

+5

Exporter :

Constants.pm:

#Constants.pm
require Exporter;
@ISA = qw(Exporter);
@EXPORT = qw($h0 $scale);
@EXPORT_OK = qw(myfunc);

$h0 = 0;
$scale = 20;
sub myfunc {...}

:
* & &myfunc @EXPORT , . * $h0 $scale , & myfunc (. , , )

And then in the module that imports Constants.pm and wants to use $h0, $scaleor &myfunc, you add the following to import all the characters found @EXPORTin Constants.pm.

#MyModule.pm
use Constants qw(;

If you want to import only some of the characters, use:

#MyModule.pm
use Constants qw($h0);

And finally, if you do not want to import any Constant.pm characters, use:

#MyModule.pm
use Constants ();
+2
source

All Articles