Jim Turner (jimpossible84) wrote in perl,
Jim Turner
jimpossible84
perl

Including global variables

Greetings,

I have inherited hundreds of scripts which have some stuff hardcoded (which now need changing - lol). Anyway, I'd like to make a simple require file to put some globals in. Many of the scripts have package statements in them and most have "use strict" set. So I need a very general and reasonably fool-proof way to handle this. Here's what I came up with and wanted to bounce this off someone before actually implementing it:

include file:
==================================

use strict;

BEGIN {
	$main::__SAVEPACKAGE = __PACKAGE__;
}

package GLOBAL;

our $GLOBALVAR1 = "value1";
our $GLOBALVAR2 = "value2";
  ...

eval "package $main::__SAVEPACKAGE; ";

1;

==================================

Then using:


require 'my-globals.pl';
  ...
print $GLOBAL::GLOBALVAR1
  • Post a new comment

    Error

    default userpic
  • 7 comments