从模块导入数据

che*_*ino 1 perl perl-module

我给了一个文件testconf.pm,但没有别的东西

my $item = {

'default'   =>     {

    'General'       =>    { 'item1' => 'config1',
                            'item2' => 'config2'
                          }
                   }
           };
Run Code Online (Sandbox Code Playgroud)

在第二个文件main.pl中,我想为下一个处理步骤加载哈希,我尝试了一些类似的东西

use testconfig;
use Data::Dumper;
my $config;
$config = testconfig::item;
print Dumper $config;
Run Code Online (Sandbox Code Playgroud)

但我无法使用testconf中的数据.不幸的是,我无法使用我的instea等扩展testconf.pm与导出器或packagedeclaration,因为这个文件必须保持这样.我如何从main.pl中的项目中获取值(顺便说一句.我需要访问数据,而不仅仅是转储数据)

ike*_*ami 7

你特意告诉Perl将变量的范围(它看到的地方)限制在文件中,所以你不能.

如果这是整个文件,您可以$item通过更改依赖于赋值为文件的最后一个语句的事实

do("testconf.pm")
   or die($@ || $!);
Run Code Online (Sandbox Code Playgroud)

my $item = do("testconf.pm")
   or die($@ || $!);
Run Code Online (Sandbox Code Playgroud)