Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
引用在另一个Perl模块文件中创建哈希的Perl模块,该文件将哈希设置为该哈希_Perl_Hash_Package_Perl Module_Exporter - Fatal编程技术网

引用在另一个Perl模块文件中创建哈希的Perl模块,该文件将哈希设置为该哈希

引用在另一个Perl模块文件中创建哈希的Perl模块,该文件将哈希设置为该哈希,perl,hash,package,perl-module,exporter,Perl,Hash,Package,Perl Module,Exporter,我有2个perlmodule文件(.pm)。File_A.pm位于/some/dir/here/File_A.pm。我有一个文件位于/some/other/dir/File_B.pm 如果使用if(-r'/some/dir/here/File_A.pm/File_A.pm)可以读取文件_A.pm,则文件_B.pm将其my%machines哈希设置为/some/dir/here/File_A.pm的%machines,否则它将使用文件_B.pm中定义的标准哈希作为my%machines=() 我试

我有2个perlmodule文件(.pm)。File_A.pm位于/some/dir/here/File_A.pm。我有一个文件位于/some/other/dir/File_B.pm

如果使用if(-r'/some/dir/here/File_A.pm/File_A.pm)可以读取文件_A.pm,则文件_B.pm将其my%machines哈希设置为/some/dir/here/File_A.pm的%machines,否则它将使用文件_B.pm中定义的标准哈希作为my%machines=()

我试过下面的代码

然而,这对我不起作用

package some::other::dir::File_B;
use strict;
use vars qw(@ISA @EXPORT $VERSION);
use Cwd;
use some::dir::File_A;

use Exporter;
$VERSION = 1.0;
@ISA     = qw(Exporter);
@EXPORT =
  qw(getMachines printMachines getMachineAttributes printMachineAttributes);

if(-r '/some/dir/here/File_A.pm'){
    my %machines = do q{/some/dir/here/File_A.pm};
else{
 my %machines = (  
    "some.fqdn.com" => {
    role        => ["someRole"],
    environment => "test",
    location    => "USA",
    os          => "Ubuntu",},
    )
}
###################################
#I have getMachines, printMachines, getMachineAtrributes, and
#printMachineAttributes below here in my code
####################################

如果文件_A.pm my%machines散列是可读的,我希望逻辑使用该文件,如果文件_A.pm不可读,则使用备份我的%machines散列。

词汇变量的范围定义为从声明到封闭块结尾的范围。第一个
my%machines
在“then”块中无法生存,第二个在“else”块末尾消失


请注意,如果恶意用户可写入文件,则他们可以在其中插入任何代码。使用INI文件或JSON、YAML、XML或其他任何文件来填充哈希更安全。

Hmm这是我没有考虑过的角度。如果我在外部使用XML文件。我将如何引用它并将其解析为perl哈希?您也可以问一个新问题:-)好的,转到我的原始帖子。我的问题是,是否使用正确的方法将%machines哈希设置为外部pm的%machine哈希?