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.
So I am using the 'require' command, which executes at runtime. All's good so
far.
Problem:
When loading a module with 'require' AND ALSO using 'use strict' I can't figure
out how to utilize those package variables *without* also using an explicit
package name (e.g. $English::button_label). Doing so is pretty straightforward
if I had been able to use 'use English', but it's not straightforward (at least
to me) on how to do it using 'require English' [1].
At issue again, is not knowing what the appropriate package name will be until
runtime. I can brute force within the CGI program by doing it along these
lines (which also requires that I set "no strict 'refs'":
my ($var_1,$var_2,$var_3,$var_4);
{ no strict 'refs';
$var_1 = ${$lang_package . "::" . "var_1"};
$var_2 = ${$lang_package . "::" . "var_2"};
$var_3 = ${$lang_package . "::" . "var_3"};
$var_4 = ${$lang_package . "::" . "var_4"};
}
Below is what I want to do, reduced to the minimum (i.e. I want test.cgi to
print out the variable without specifying the package name):
test.cgi
========
#!/usr/local/bin/perl
use strict;
my $lang_package_file = "English.pm";
require $lang_package_file;
print "My package variable \$language value is $language" . "\n"; # This
doesn't work [2]
exit(0);
English.pm
==========
package English;
$language = "English";
1;
I know I can turn off "use strict" but I've tried to walk the straight and
narrow with recent programming efforts, and I hate to start backsliding.
There's got to be a better, more elegant way to do this and maintain 'use
strict' in the main CGI program, I'm just not getting it at the moment...
-- Michael
[1] I'm referring to using this stuff in the package, which works great with
'use English' but not so great (for me anyway) with 'require English'.
package English;
require Exporter;
@ISA = qw(Exporter);
@EXPORT = qw($language $etc);
[2] I get this when I run test.cgi:
Global symbol "$language" requires explicit package name at test.cgi line 5.
Execution of test.cgi aborted due to compilation errors.
# Michael Doran, Systems Librarian
# University of Texas at Arlington
# 817-272-5326 office
# 817-688-1926 mobile
# [EMAIL PROTECTED]
# http://rocky.uta.edu/doran/