in reply to Re^2: searching for an object reference
in thread searching for an object reference
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;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: searching for an object reference
by simonm (Vicar) on Jul 09, 2005 at 00:24 UTC |