Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (4)
As of 2024-04-18 18:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found