"too many arguments" passing hash reference to subroutine

D

Dean

Running the code below I see the error:

Too many arguments for main::print_rHoHoH at ./usenet.pl line 21, near
"$xml)"



#!/usr/bin/perl -w
use XML::Simple;

sub print_rHoHoH(){
$rHoHoH=$_[0];
for my $k1 ( sort keys %$rHoHoH ) {
print "$k1\n";

for my $k2 ( sort keys %{$rHoHoH->{ $k1 }} ) {
print "\t$k2\n";

for my $k3 ( sort keys %{$rHoHoH->{ $k1 }->{ $k2 }} ) {
print "\t\t$k3 => $rHoHoH->{ $k1 }->{ $k2 }->{ $k3 }\n";
}
}
}
}

$xml=XMLin($ARGV[0]);
print_rHoHoH($xml);



I don't understand why passing the reference to the hash in the call to the
subroutine causes the error.

If I change the last line to
do print_rHoHoH($xml);
it works, but with a warning about 'do' being deprecated.

Any insight would be much appreciated.
Thanks.
 
P

Paul Lalli

Dean said:
Running the code below I see the error:

Too many arguments for main::print_rHoHoH at ./usenet.pl line 21, near
"$xml)"

#!/usr/bin/perl -w
use XML::Simple;

sub print_rHoHoH(){

You have unknowingly prototyped your subroutine to take zero arguments.
The parentheses do not belong there.

Paul Lalli
 
K

kens

Dean said:
Running the code below I see the error:

Too many arguments for main::print_rHoHoH at ./usenet.pl line 21, near
"$xml)"

Whenever you see this message it means that the function/subroutine has
been
prototyped - it has a defined parameter list.
#!/usr/bin/perl -w
use XML::Simple;

sub print_rHoHoH(){

Here you defined the prototype (unintentionally I presume). The empty
parenthese indicate that this function takes no parameters.
Omit the parentheses and see how that works.
$rHoHoH=$_[0];
for my $k1 ( sort keys %$rHoHoH ) {
print "$k1\n";

for my $k2 ( sort keys %{$rHoHoH->{ $k1 }} ) {
print "\t$k2\n";

for my $k3 ( sort keys %{$rHoHoH->{ $k1 }->{ $k2 }} ) {
print "\t\t$k3 => $rHoHoH->{ $k1 }->{ $k2 }->{ $k3 }\n";
}
}
}
}

$xml=XMLin($ARGV[0]);
print_rHoHoH($xml);



I don't understand why passing the reference to the hash in the call to the
subroutine causes the error.

If I change the last line to
do print_rHoHoH($xml);
it works, but with a warning about 'do' being deprecated.

Any insight would be much appreciated.
Thanks.

HTH,
Ken
 
D

Dean

Running the code below I see the error:

Too many arguments for main::print_rHoHoH at ./usenet.pl line 21, near
"$xml)"

Whenever you see this message it means that the function/subroutine has
been
prototyped - it has a defined parameter list.
#!/usr/bin/perl -w
use XML::Simple;

sub print_rHoHoH(){

Here you defined the prototype (unintentionally I presume). The empty
parenthese indicate that this function takes no parameters.
Omit the parentheses and see how that works.
$rHoHoH=$_[0];
for my $k1 ( sort keys %$rHoHoH ) {
print "$k1\n";

for my $k2 ( sort keys %{$rHoHoH->{ $k1 }} ) {
print "\t$k2\n";

for my $k3 ( sort keys %{$rHoHoH->{ $k1 }->{ $k2 }} ) {
print "\t\t$k3 => $rHoHoH->{ $k1 }->{ $k2 }->{ $k3 }\n";
}
}
}
}

$xml=XMLin($ARGV[0]);
print_rHoHoH($xml);



I don't understand why passing the reference to the hash in the call to the
subroutine causes the error.

If I change the last line to
do print_rHoHoH($xml);
it works, but with a warning about 'do' being deprecated.

Any insight would be much appreciated.
Thanks.

HTH,
Ken

Many Thanks. All I can say is I'd been doing some php prior to this and
completely overlooked it :-/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,015
Latest member
AmbrosePal

Latest Threads

Top