Re: equality as a variable
- From: anno4000@xxxxxxxxxxxxxxxxxxxxxxx (Anno Siegel)
- Date: 30 Sep 2005 19:44:50 GMT
A. Sinan Unur <1usa@xxxxxxxxxxxxxxxxxxx> wrote in comp.lang.perl.misc:
> anno4000@xxxxxxxxxxxxxxxxxxxxxxx (Anno Siegel) wrote in
> news:dhjufn$801$1@xxxxxxxxxxxxxxxxxxxxxxxxx:
>
> > A. Sinan Unur <1usa@xxxxxxxxxxxxxxxxxxx> wrote in comp.lang.perl.misc:
> >> Brian Wakem <no@xxxxxxxxx> wrote in
> >> news:3q58ibFd6ikpU1@xxxxxxxxxxxxxx:
> >>
> >> > Shiraz wrote:
> >> >
> >> >> i need to put the equality in an if statement as a variable
> ...
>
> >> Hmmm ... Sorry, I am going to go with hash based solution:
>
> ...
>
> > I'd use the hash *and* eval:
> >
> > my %handlers = map { $_ => eval "sub { \$_[0] $_ \$_[ 1] }" }
> > qw( > < >= <= == != <=>),
> > # qw( gt lt ge le eq ne cmp),
> > ;
> >
> > That string-eval is perfectly safe, everything comes from inside the
> > source.
>
> I always type too much :))
It isn't just the laziness aspect, in fact the fewer lines of code may
take longer to write (and debug) than straight-forward typing of the
obvious. There is also reliability.
Basically, in the explicit definition of the hash you specify the same
thing twice which implies a possibility[1] of getting it wrong. Even
if the probability is minute, if you do this (programming) all day, it
is bound to happen once in a while, especially in maintenance. You're
safe from that when you specify each operator only once. The cost is
a possible loss in clarity.
Later you have more duplicate specifications:
# from memory
my %examples = (
'5 == 6' => [ 5, '==', 6],
'5 <= 6' => [ 5, '<=', 6],
# ...
);
In a similar vein, I wouldn't even use a hash for that, but specify a list
of strings:
my @examples = (
'5 == 5',
'5 <= 6',
'5 <=> 6',
'"E" eq "F"',
'"E" cmp "F"',
);
and run them as
print "$_: ", do_compare_op( split), "\n" for @examples;
with do_compare_op unchanged. That also shows them in the sequence
they are specified.
Anno
[1] initially mistyped "pissibility", was tempted to let it stand
--
If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers.
.
- References:
- equality as a variable
- From: Shiraz
- Re: equality as a variable
- From: A. Sinan Unur
- Re: equality as a variable
- From: Anno Siegel
- Re: equality as a variable
- From: A. Sinan Unur
- equality as a variable
- Prev by Date: Re: equality as a variable
- Next by Date: Re: website development and maintenance, using Perl. Please help!
- Previous by thread: Re: equality as a variable
- Next by thread: Re: equality as a variable
- Index(es):