Michael,
Interesting question.
There are probably several approaches to implementing multilingual support.
I hope you get many responses to this question. I would really like to see what
others have to say.
>From me - just a few simple comments:
I see two problems.
1) to load a particular file, unknown at compile time.
2) to "export" a variable into the main program
The first you have solved with require ("do" would also be fine).
But you don't have to load a package, you can just load a file.
The second: define your variable(s) with "our".
#!/usr/bin/perl -wl
use strict;
our %msg;
my $lang = $ARGV[0] ||'en';
require "Mylang_$lang.pl";
print $msg{alert};
print $msg{success};
__END__
And Mylang_en.pl and friends looks like
use strict;
%main::msg = (
alert => 'ALERT',
success => 'OK',
);
__END__
A variation on this idea would be to define a certain scope, instead of main::
for the multilingual strings.
$LANG::this = ...
$LANG::that = ...
But is it actually anything wrong with your original idea of using no strict
'refs' for that part of the code building up the loading of a module?
I don't know.
Doing a little google-ing a found this article quite interesting:
http://www.drdobbsjournal.net/web-development/184416008
It is from The Perl Journal (May 2003), Autrijus Tang writing about Web
Localization and Perl. In that article he is demonstrating the use of
Locale::Maketext::Lexicon
Probably too demanding for smaller projects, but still interesting.
There is always a balance between how much work to spend on solving a problem,
how flexible and scalable it has to be, if we like avoiding installing extra
modules etc etc...
Leif
======================================
Leif Andersson, Systems Librarian
Stockholm University Library
SE-106 91 Stockholm
SWEDEN
Phone : +46 8 162769
Mobile: +46 70 6904281
-----Ursprungligt meddelande-----
Från: Doran, Michael D [mailto:[EMAIL PROTECTED]
Skickat: den 26 april 2008 02:46
Till: Perl4lib
Ämne: Importing Perl package variables into a Perl script with "require"
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/