RE: Importing Perl package variables into a Perl script with "require"

[email protected] ("Doran, Michael D")
Newsgroups perl.perl4lib
Message-ID <[email protected]>
Hi Mike,

> You can use UNIVERSAL::require to do this:

Hmmm.  This is interesting.  I wasn't familiar with this module, so will be giving it a look.

Thanks!

-- Michael

# Michael Doran, Systems Librarian
# University of Texas at Arlington
# 817-272-5326 office
# 817-688-1926 cell
# [email protected]
# http://rocky.uta.edu/doran/



-----Original Message-----
From: Mike Rylander [mailto:[email protected]]
Sent: Mon 5/5/2008 8:57 PM
To: Doran, Michael D
Cc: Perl4lib
Subject: Re: Importing Perl package variables into a Perl script with "require"
 
On Fri, Apr 25, 2008 at 8:46 PM, Doran, Michael D <[email protected]> wrote:
> Back-story:
>
>  I have a Perl CGI program.  The CGI program needs to utilize variables in one of several separate configuration files (packages).  The different packages all contain the same variables, but with different values for those variables.  Each package represents a different language for a multilingual interface for the CGI program.  e.g. English.pm, French.pm, Spanish.pm.
>
>  The CGI program can't determine which language package is needed until it parses the form input and does a test based on the value for a query string name/value pair.  Based on the test, I assign a value to a package load command (i.e. 'use English' or 'require English').  Because of that, I can't load the package with "use Package" since "use" runs at compile time, before I can assign a value to it.
>

You can use UNIVERSAL::require to do this:


use UNIVERSAL::require;
use CGI;

my $cgi = new CGI();
my $package = 'Language::' . $cgi->param('lang'); # say, 'English'
$package->use;

my $thing = $package->new();


Hope that helps. FWIW, we use U::r pretty heavily inside Evergreen.

-- 
Mike Rylander
 | VP, Research and Design
 | Equinox Software, Inc. / The Evergreen Experts
 | phone: 1-877-OPEN-ILS (673-6457)
 | email: [email protected]
 | web: http://www.esilibrary.com
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.