Also, for general purpose use, UNIVERSAL is probably better than the stringification/regex approach, as it plays better with string overloading.
A further speed improvement can be gained by using list processing to avoid recursive calling:
use strict; use UNIVERSAL 'isa'; sub deep_grep (&@) { my $test = shift; my ( @results, %seen ); while ( scalar @_ ) { my $target = shift @_; push @results, grep $test->( $_ ), $target; unshift @_, $seen{$target} ++ ? () : isa($target, 'HASH') ? values %$target : isa($target, 'ARRAY') ? @$target : isa($target, 'REF') ? $$target : isa($target, 'SCALAR') ? $$target : (); } return @results; }
The prototype allows you to call it blockwise, like the builtin grep, but isn't needed:
my @loggers = deep_grep( sub { isa( $_, 'Log' ) }, $target ); my @loggers = deep_grep { isa( $_, 'Log' ) } $target;
In reply to Re^3: searching for an object reference
by simonm
in thread searching for an object reference
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |