AngryKumquat has asked for the wisdom of the Perl Monks concerning the following question:

Is there an easy way to hold an operator in a scalar/array element and then use the scalar/array element in an expression. For example I want to pass a method an array of file test operators and use the array elements as the actual file test operator. So instead of "if ( -e $file )" I want to use "if ( $permissions[0] $file )" Is there a way to indicate that the array element should be handled as an operator. Apologies if I'm asking a dumb question.

Replies are listed 'Best First'.
Re: Operator held in scalar/array element
by ikegami (Patriarch) on May 06, 2009 at 22:11 UTC
    No, not an operator, but you can hold a reference to a sub.
    my @permissions = ( sub { -e $_[0] }, ); if ($permissions[0]->($file))
      Thank you this also works perfectly. Can I just clarify why in the method/subroutine I need the index to $_. I tried it without and it doesn't work. "sub { -e $_ }" instead of "sub { -e $_[0] }". Why does the file parameter need to be referenced as index[0]?
        sub { -e $_[0] }
        is short for
        sub { my ($file) = @_; -e $file }

        I hope that clarifies things.

        Sorry. Don't worry about that secondary question. My brain has gone to bed before my body. Thanks for the help.
Re: Operator held in scalar/array element
by FunkyMonk (Bishop) on May 06, 2009 at 22:16 UTC
    If you're sure your input can be trusted, there's string eval:
    if ( eval "$permissions[0] \$file" ) { ... }

      Thank you that works perfectly.