[Developers] Perl modules and global variables

Carsten Schneemann carsten.schneemann at aei.mpg.de
Tue Feb 6 18:19:13 CST 2007


Hi!

Erik Schnetter wrote:
> I've tried to introduce a "use strict" to the file
> interface_parser.pl.  One problem is that this file accesses the
> global variable $cctk_home, which is defined in CST before it requires
> interface_parser.pl.
>
> How do I propagate the variable $cctk_home from CST to
> interface_parser.pl?
>
My Perl knowledge is limited, but AFAIK unless you the required file
contains a "package" (which is roughly similar to a Fortran module),
which interface_parser.pl doesn't, the global variables from CST *are*
in fact visible in / propagated to interface_parser.pl. The problem here
is rather that with "use strict" you must either explicitly declare
$cctk_home and friends as global in CST (via "our $cctk_home") *or* you
have to use the fully qualified name $main::cctk_home when referencing
them (instead of the "lazy" form $cctk_home).

So one solution would be to declare all global variables in CST as
global or replace all references to global variables in
interface_parser.pl by their fully qualified names. Since both options
are somewhat messy you could try to define a lexical variable with the
same name as an alias in interface_parser.pl, e.g.
my $cctk_home = $main::cctk_home
and similarly for each global variable it uses.

There might also be more sophisticated solutions...

Bye,

Carsten.



More information about the Developers mailing list