如何将运算符传递给Perl子例程?

时间:2012-05-03 10:35:49

标签: perl operators subroutine

我有一个名为“lookup”的子命令,它在给定值的哈希中执行查找。 我意识到如果我可以要求它看不到给定的值,而是小于作为参数传递的值,那将会更强大。

我可以制作lookupbigger,lookupsmall等,但我相信还有更好的方法。

# lookup id according to the search criteria
sub lookup {
  my( $data, $lk, $lv ) = ( @_ );
  my @res;

  foreach my $key (keys $data) {
    my $value = $$data{$key};
    next unless( defined $$value{$lk} );
    # this is the line where I want to replace eq with another operator
    push(@res, $key) if( $$value{$lk} eq $lv );
  }

  return \@res;
}

3 个答案:

答案 0 :(得分:6)

您可以将标准函数传递给查找函数:

#!/usr/bin/env perl

use strict; use warnings;
use YAML;

my %hash = qw(a 1 b 2 c 3 d 4 e 5);

# find all keys with odd values

print Dump lookup_keys_by_value(\%hash, sub {
        return unless @_;
        my $v = shift;
        return $v % 2;
    },
);

sub lookup_keys_by_value {
    my ($hash, $criterion) = @_;

    my @keys;

    while (my ($k, $v) = each %$hash) {
        push @keys, $k if $criterion->($v);
    }

    return \@keys;
}

答案 1 :(得分:2)

这是一个想法(也许太“聪明”):

use strict;
use warnings;

{   no strict 'refs';
    # When called like __PACKAGE__->$op( ... ),  __PACKAGE__ is $_[0]
    *{'>'}  = sub { return $_[1] > $_[2]; }; 
    *{'<'}  = sub { return $_[1] < $_[2]; };
    *{'=='} = sub { return $_[1] == $_[2]; };
}

sub determine {
    my ( $first_arg, $op, $second_arg ) 
        = map { s/^\s+//; s/\s+$//; $_ }
          split( /\s*([<>]|==)\s*/, @_ == 1 ? shift : "@_" )
        ;
    say "$first_arg $op $second_arg => " 
      . (  __PACKAGE__->$op( $first_arg, $second_arg ) ? 'TRUE' : 'FALSE' )
      ;
}

determine( qw( 1 < 2 ) );
determine( qw( 2 < 1 ) );
determine( qw( 1 > 2 ) );
determine( qw( 2 > 1 ) );
determine( qw( 1 == 2 ) );
determine( qw( 1 == 1 ) );
determine( qw( 2 == 2 ) );
determine( ' 1 < 2 ' );
determine( ' 2 < 1 ' );
determine( ' 1 > 2 ' );
determine( ' 2 > 1 ' );
determine( ' 1 == 2 ' );
determine( ' 1 == 1 ' );
determine( ' 2 == 2 ' );

答案 2 :(得分:1)

您可以尝试使用重载

use overload (
'>' => 'compareBigger',
'<' => 'compareSmaller',
'==' => 'equals'
 )