I am trying to pass an inner array to a function in Perl. Here is my Perl program.
#!/usr/bin/perl
use strict;
use warnings;
my %data = (
'a' => (
x => 'Hello',
y => 'World'
),
'b' => (
x => 'Foo',
y => 'Bar'
)
);
#prototype
sub p(\%);
{ #main
p(%data{'a'}); # should print "Hello Wolrd".
}
sub p(\%) {
print "$_[0]{x} $_[0]{y}\n";
}
Instead, I get the following error: Type of arg 1 to main::p must be hash (not key/value hash slice)
.
This works:
#!/usr/bin/perl
use strict;
use warnings;
#prototype
sub p(\%);
{ #main
my %a = (
x => 'Hello',
y => 'World'
);
p(%a);
}
sub p(\%) {
print "$_[0]{x} $_[0]{y}\n";
}
So there must be something wrong with the method call. But what? The content of a is a hash, so the first character after p(
must be %
(I tried p($data{'a'});
too, but it leaves me with another error (which seems logical, since the content of a isn’t a scalar). I do not have to manually create a reference to the hash and dereference because I declare the function prototype. What am I missing?