in reply to Scoping problems with recursive function
#!/usr/bin/perl use strict; use warnings; my %hash = ( 'one' => 1, 'two' => 2, 'three' => 3, 'four' => 4, 'five' + => 5); my %dependencies =( 'one' => [], 'two' => [], 'three' => ['four'], 'fo +ur' => ['one','two'], 'five' => ['three']); #sub my_del($){ sub my_del{ #while (my $key = each (%hash)){ foreach my $key (keys %hash){ print "KEY = $key\n"; foreach (@{ $dependencies{$key} }){ if( $_ eq $_[0]){ print"Going to call sub for $key\n"; my_del($key); } } print"\n"; } delete $hash{$_[0]}; print "Deleting $_[0]\n"; } my_del('one');
Note that I also removed the prototyping from your function declaration as well. It's unnecessary in Perl unless you are doing particular tricks and probably doesn't mean what you think it means. Note that it can't check the prototype until the function is defined, and hence you need cleverness to protoype a function you are calling recursively.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Scoping problems with recursive function
by AnomalousMonk (Archbishop) on Mar 19, 2010 at 18:54 UTC |