in reply to Re^2: Passing anon sub as param
in thread Passing anon sub as param
By the way, the sub doesn't have to be anon:traverse($in, 0, 0, sub {print ' ' x $_->{dept},'-',$_->{comm},"\n";}) +; ... later in the code... traverse($in, 0, 0, sub {$sth->execute($dept, $comm)});
sub _print { print ' ' x $_->{dept},'-',$_->{comm},"\n"; } traverse($in, 0, 0, \&_print);
It's a feature, it has no side effects, so there's no reason to get rid of it, and there's every reason to keep it. If you don't want to explicitely specify it, create a wrapper:
sub print_tree { push(@_, 0, 0, sub {print ' ' x $_->{dept},'-',$_->{comm},"\n";}); &traverse; } print_tree($in);
|
|---|