I have 2 perlmodule files (.pm). File_A.pm
is located on /some/dir/here/File_A.pm
. I have a File_B.pm
located on /some/other/dir/File_B.pm
.
File_B.pm
will set its my %machines
hash equal to /some/dir/here/File_A.pm
's %machines
if File_A.pm
is readable using if (-r '/some/dir/here/File_A.pm')
else it will use a standard hash defined within File_B.pm
as my %machines = ()
.
I have tried the code below
However, this is not working for me.
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
####################################
I am expecting the logic to use the File_A.pm my %machines hash if it is readable and if not use the backup my %machines hash in case the File_A.pm somehow becomes unreadable.
The scope of a lexical variable defined with my spans from the declaration to the end of the enclosing block. The first my %machines
doesn't survive the "then" block, the second one disappears at the end of the "else" block.
Note that if File_A is writable by a malicious user, they can insert any code into it. It's safer to use an INI file, or JSON, YAML, XML, or whatever to populate the hash.