in reply to interpolating a variable into an operator

I am trying to store operators in a variable and then use the variable as the operator

Lisp can do this; Perl cannot. It can, however, store an anonymous *subroutine* in a variable. This is syntactically different from an operator, but it can accomplish the same effect:

$op = sub { $_[0] eq $_[1] }; if($op->($somvar, "some text")){ #do stuff }

Resist the urge to use string eval for this.


;$;=sub{$/};@;=map{my($a,$b)=($_,$;);$;=sub{$a.$b->()}} split//,".rekcah lreP rehtona tsuJ";$\=$;[-1]->();print

Replies are listed 'Best First'.
Re^2: interpolating a variable into an operator
by diotalevi (Canon) on Jul 20, 2004 at 19:16 UTC
    Bah. We just need more people to treat perl like lisp. This isn't impossible - it is just difficult.

        Perl is best treated like lisp by being lisp-like. Go after the optree directly and both cases look like multiply( EXPR1, EXPR2 ) and repeat( EXPR1, EXPR2 ). I don't know how you thought to solve this where * is overridable but x isn't. Care to share?