Is there a way to reliably call operators such as
eq,
ne, etc. on an object which you know has those operators overloaded? However, I don't want to make assumptions about the class of the object.
Here's an example:
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;
The idea is that I could write
create_selector like this:
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) }
}
}
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.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.