Re: making user definable variables really global

Derek Robert Price <[email protected]> Tue, 17 Jun 2003 10:34:28 -0400
Newsgroups gmane.comp.tex.texi2html.devel
Organization Ximbiot <http://ximbiot.com>
Message-ID <[email protected]>
Dumas Patrice wrote:

>>Again, how about sourcing the files into a Texi2HTML::Config package or 
>>the like?  I'm torn over whether the .init file should contain the 
>>"package Texi2HTML::Config" line or not, but it might not hurt.
>>
>>    
>>
>
>Do you mean something like ?
>
>package Texi2HTML::Config
>require file.init
>  
>

I don't remember the exact syntax.   Does that work?  At the least, I meant:

cat >file.init <<EOF
package Texi2HTML::Config;
$setting1="some data";
$setting2="some other data";
$function1=sub { some function };
EOF

This way, config vars are still global, really, but they are hidden in 
the Texi2HTML::Config namespace (accessible as 
$Texi2HTML::Config::setting1, $Texi2HTML::Config::setting2, etc...).  At 
the least their usage becomes a little clearer.

If we wanted to deglobalize them, I suppose we could instantiate a 
Texi2HTML::Config instance and pass the reference around.

Perhaps that is step 1 and then step 2?

The CPAN module distributed with Perl should have an example of this, 
but maybe it is too complex.  I did something like this for one program 
I wrote:

    # Load our config file.
    use lib "$ENV{'HOME'}/lib/perl5/myperl";
    use PageMe::Config;

Then in the config file (~/lib/perl5/myperl/PageMe/Config.pm):

    %PageMe::Config =
    (
            'PagerEmailAddress' => q/[email protected]/,
            'debug' => 0
    );

    1;
    __END__

Vars are used like:

    $PageMe::Config{'PagerEmailAddress'}

I just looked at my ~/.cpan/CPAN/MyConfig.pm.  It looks like it does 
much the same thing.  CPAN.pm has some wrapper cruft to join the config 
with the default config & handle missing and malformed files, I think.

Derek

-- 
                *8^)

Email: [email protected]

Get CVS support at <http://ximbiot.com>!
-- 
I will not bring sheep to class.
I will not bring sheep to class.
I will not bring sheep to class...

          - Bart Simpson on chalkboard, _The Simpsons_