packetstormer has asked for the wisdom of the Perl Monks concerning the following question:

Struggling again.....two questions really.

I am writing an app the will have a lot of user changeable strings (button names, customer names, error messages etc.). I thought the best way to do this is to create a variable.pl file, list all my variables in here and then call them from my other files. Obviously the whole, local/global variable issue arises when using strict pragma. But I can't think of a Perl way to do it.

I have been looking at Exporter but it seems I need to specify each variable to export, which could be literally 100's. Is there any way to Export a whole file from a package? I have read the perldoc Exporter page but there doesn't seem to be anyway to use the whole file instead of specifying individual variables or functions etc.
I am thinking I might be able to do this by creating a hash of all my variables and just exporting that, but I am not sure if this is acceptable?

And, is this bad Perl practice?

Replies are listed 'Best First'.
Re: Exporting Variables
by mr_mischief (Monsignor) on Feb 07, 2011 at 21:35 UTC

    I think the most commonly accepted way to do this is probably to use an actual configuration file and use JSON, YAML, or Windows-style INI file modules to read the settings. The second most common way I think is to use an actual Perl module that contains the settings. Instead of using Exporter, you could use the values from the specified namespace or as an object.

    Whichever method you choose for the configuration file, you can instantiate a configuration object that has all the settings as object data accessible through getter methods. If that doesn't work for you, you should still not make the variables global. You can access variables that are in a module's namespace by prepending the name of the module. Well, actually it's a package namespace but often enough the two are the same scope. See perlmod for more information about modules and scope issues with them.

Re: Exporting Variables
by Anonymous Monk on Feb 07, 2011 at 21:39 UTC

      Regarding exporting without naming things

      That imports without naming things. The OP was asking about avoiding to have to name all the things to export.

      $ perl -e' BEGIN { package Foo; use Exporter qw( import ); sub f {} $INC{"Foo.pm"} = __FILE__; } use Foo "/./"; f(); ' Undefined subroutine &main::f called at -e line 10.