Search code examples
perlperl-module

Import Data from Module


I have given a file testconf.pm , with nothing else in it but

my $item = {

'default'   =>     {

    'General'       =>    { 'item1' => 'config1',
                            'item2' => 'config2'
                          }
                   }
           };

In a second file main.pl , i want to load the hash for next processing steps, I tried some stuff like

use testconfig;
use Data::Dumper;
my $config;
$config = testconfig::item;
print Dumper $config;

But i could not get to use the data from testconf. Unfortunately, i am not able to extend testconf.pm with an exporter or a packagedeclaration, using our instea of my and so on, as this file has to stay like this. How could i get the values out of item in main.pl (btw. i Need to Access the data, not only Dumping it )


Solution

  • You specifically told Perl to limit the scope of the variable (where it's seen) to the file, so you can't.

    If that's the entire file, you could rely on the fact that the assignment to $item is the last statement of the file by changing

    do("testconf.pm")
       or die($@ || $!);
    

    to

    my $item = do("testconf.pm")
       or die($@ || $!);