in reply to Object Matching, speed issue

Well, revdiablo has already given you one way to do it using string sub references. (Note that in a "use strict" environment, you may need to put "no strict subs;" somewhere in that code to have it work)

Here's another way, which is something to consider in general if you find yourself running eval inside a big loop. Basically, build up a string that will eval to a sub reference, and then use the sub reference in the loop. So STEP 2 becomes:

my $subtext = "sub {\n"; $subtext .= join (" and ", map { '$_->get_'.$_.'() eq "'.quotemeta($hashref->{ $_ }).'"' } @key +s); $subtext .= ";\n}"; my $selectsub = eval($subtext); my @ret = grep(&$selectsub, @{ $self->get_data() });
And that's it. Note that use of a sub reference also lets you easily use grep and not have to build up a return list yourself.
-- @/=map{[/./g]}qw/.h_nJ Xapou cets krht ele_ r_ra/; map{y/X_/\n /;print}map{pop@$_}@/for@/

Replies are listed 'Best First'.
Re^2: Object Matching, speed issue
by revdiablo (Prior) on Aug 26, 2004 at 17:40 UTC
    Note that in a "use strict" environment, you may need to put "no strict subs;" somewhere in that code to have it work

    Actually, no, you don't. strict doesn't care about softrefs for method calls.