0

Please help me resolve this issue

pre_package_config.pm

use strict;
our %pre_pkg_configs;

$pre_pkg_configs{locDbList}=['default','default_test'];

second.pl

#!/usr/bin/perl
use Expect;
use strict;

our %pre_pkg_configs;

my  $pre_pkg_file = './pre_package_config.pm';

eval {require $pre_pkg_file};

foreach my $db ( $pre_pkg_configs{locDbList} ){
    print $db;
}

Output:

ARRAY (0x10092ae88)

Should have been:

default default_test

1
  • 1
    When debugging, use the Data::Dumper module to print your debugging information. It will make things much easier for you. Commented Jan 18, 2013 at 11:54

1 Answer 1

3

$pre_pkg_configs{locDbList} is a single (scalar) value. Iterating over it would simply give you that one value (which happens to be a reference to an array). If you want to iterate over the contents of that array, you need to dereference:

for my $db (@{ $pre_pkg_configs{locDbList} }) {
    print $db;
}

Note that this will output defaultdefault_test, not default default_test. The easiest way to get the latter would be:

print join(" ", @{ $pre_pkg_configs{locDbList} }), "\n";

To learn more about references, see perldoc perlreftut.
(Also, you should use warnings; in every file in addition to use strict;.)

Sign up to request clarification or add additional context in comments.

3 Comments

but I have set the $pre_pkg_configs{locDbList} an array in previous file ? e.g $pre_pkg_configs{locDbList}=['default','default_test'];
What I should do to make $pre_pkg_configs{locDbList} an array over which I can iterate ?
@sakhunzai No, that's a reference to an array, not an array. Hashes can't contain arrays, they can only contain scalars. See perlreftut.

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.