perl5ever has asked for the wisdom of the Perl Monks concerning the following question:
Here's an example:
The idea is that I could write create_selector like this:sub create_selector { my ($spec) = @_; my $selector; if ($spec =~ m/^=(.*)/) { $selector = sub { $_[0]->version eq $1 } } elsif ($spec =~ m/!=(.*)/) { $selector = sub { $_[0]->version ne $1 } } elsif ($spec =~ m/>(.*)/) { $selector = sub { $_[0]->version gt $1 } } $selector; } my $selector = create_selector("=6.2"); ... my @selected_dists = grep { $selector->{$_} } @list_of_dists;
That is, I want to be able to perform the translation of $op to a method name in a way which will work for whatever object I'll be applying it to.sub create_selector { my ($spec) = @_; my $selector; if ($spec =~ m/^(=|!=|<|>)(.*)/) { my $op = $1; my $method = ...translate $op to a method name... $selector = sub { $_[0]->version->$method($2) } } }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: calling overloaded comparison operators as method calls?
by morgon (Priest) on Jul 05, 2009 at 20:32 UTC | |
by perl5ever (Pilgrim) on Jul 05, 2009 at 20:47 UTC | |
by morgon (Priest) on Jul 06, 2009 at 09:56 UTC |