0

I've got an hash array in my code, imported from the Config::General module that contains multiple levels of child hash arrays. I'm trying to access the variables within the hash array's child, yet it continually complains about variables being undefined.

If I try to define them as hash arrays in advance it complains that the contents are undefined. I'm clearly doing something stupid, are you able to point out what?

my $key;
my $val;
# allDevices should be a hash array istelf
my $allDevices = $cfgfile{'remote_hosts'}{'category:switch'}{'subcategory:brocade'};
while (($key, $val) = each $allDevices)
{
    # This proves that the $val is a hash array also
    INFO(" - $key => $val\n");
    # This bit works ok
    my @devDetails = split(/:/, $key);
    my $thisDevice = $val;
    my $thisDeviceType = $devDetails[0];
    my $thisDeviceName = $devDetails[1];
    INFO(" - Device Name: $thisDeviceName\n");
    # This is where it complains - %thisDevice requires explicit package name
    my $thisDeviceConnectProto = $thisDevice{'remote_device_connect_protocol'};
    my $thisDeviceConnectIP = $thisDevice{'remote_device_connect_ipaddress'};
    my $thisDeviceConnectUser = $thisDevice{'remote_device_connect_username'};
    my $thisDeviceConnectPass = $thisDevice{'remote_device_connect_password'};
    #########################################################################
    # For each command specified in cli file                                #
    #########################################################################
    # CLI "for" loop
}

Thanks in advance

2
  • 3
    use strict; use warnings; will tell you what you did wrong. Commented Aug 8, 2014 at 12:40
  • Also, there is no such thing as a "hash array". I think you mean hash of arrays, HoA. Commented Aug 8, 2014 at 12:41

2 Answers 2

3

Don't forget to use strict and use warnings in your code. They helps you a lot in the compiling phase. For example:

# file: test.pl
use strict;
use warnings;

my $hash = {'one'=>1}; #hash reference

print $hash{'one'}, "\n"; #error: this refers to %hash, and it doesn't exists
print $hash->{'one'}, "\n"; #ok

The perl interpreter shows this error message:

Global symbol "%hash" requires explicit package name at test.pl line 7  
Sign up to request clarification or add additional context in comments.

1 Comment

He already has strict, from the question: # This is where it complains - %thisDevice requires explicit package name
2

You didn't declare %thisDevice hash and $thisDevice hashref is completely another variable.

Change

$thisDevice{'remote_device_connect_protocol'};

to

$thisDevice->{'remote_device_connect_protocol'};

2 Comments

Perfect, thanks for the lighting response! This has fixed it.
@user3691037 The alternate to -> notation is by dereferencing the reference. As you are expecting a scalar value just add a $ sigil. $$thisDevice{'remote_device_connect_protocol'}; will work too, though arrow looks pretty.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.