For those curious, this is how I might do it using tie.
#!perl -w use strict; # You could write any kind of function you wanted... # I thought I'd go for multi-operand functions. my %funk; %funk = ( 'Add' => sub { return $_[0] if not defined $_[1]; unshift( @_, shift( @_ ) + shift( @_ ) ); &{$funk{Add}}}, 'Subtract' => sub { return $_[0] if not defined $_[1]; unshift( @_, shift( @_ ) - shift( @_ ) ); &{$funk{Subtract}}}, 'Multiply' => sub { return $_[0] if not defined $_[1]; unshift( @_, shift( @_ ) * shift( @_ ) ); &{$funk{Multiply}}}, 'Divide' => sub { return $_[0] if not defined $_[1]; unshift( @_, shift( @_ ) / shift( @_ ) ); &{$funk{Divide}}}, 'IsEqual' => sub { for ( 1 .. $#_ ) { return 0 if $_[0] != $_[$ +_] } 1 }, 'IsNotEqual' => sub { &{$funk{IsEqual}} ? 0 : 1 }, ); sub Funk::TIESCALAR { bless \$_[1], $_[0] } sub Funk::FETCH { ${$_[0]} } sub Funk::STORE { ${$_[0]}=$funk{$_[1]} } my $op; tie $op, 'Funk', $op; #Usage: # You now simply decide the operator you want and pass it the args: # $op = 'Add'; # my $two = $op->( 1, 1 ); # Test loop: my @testArgs = ( 3, 3 ); for ( sort keys %funk ) { $op = $_; my $answer = $op->( @testArgs ); # print the result: $^A = ""; formline "\@>>>>>>>>>>>>>>>>>>>>> = $answer\n", "$_( " . join( ', ', @testArgs ) . " )"; print $^A; } __END__
Additional code could be added to do argument checking and the like.

In reply to Re^2: Is it possible to store an arthimetric operator in a variable? by Adam
in thread Is it possible to store an arthimetric operator in a variable? by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.