Is it good practice to export variables in Perl?

Posted by gvkv on Stack Overflow See other posts from Stack Overflow or by gvkv
Published on 2010-04-07T15:30:28Z Indexed on 2010/04/07 15:33 UTC
Read the original article Hit count: 144

Filed under:
|
|

I'm finding it very convenient to pass configuration and other data that is read or calculated once but then used many times throughout a program by using Perl's use mechanism. I'm doing this by exporting a hash into the caller's namespace. For example:

package Myconfiguration;

my %config;

sub import {
    my $callpkg = caller(0);
    my $expsym = $_[1];

    configure() unless %config;

    *{"$callpkg\::$expsym"} = \%config;
}

and then in other modules:

use MyConfiguration (loc_config_sym);

if ( $loc_config_sym{paramater} ) {
    # ... do stuff ...
}

However, I'm not sure about this as a best practice. Is it better to add a method that returns a hash ref with the data? Something else?

© Stack Overflow or respective owner

Related posts about perl

Related posts about variables