Solved

Scalar input / convert to operator

Posted on 2000-04-16
4
179 Views
Last Modified: 2010-03-05

I have three arguments on the command line. One of which is a mathematical operator, such as +. I'm parsing the arguments to three scalars. How can I use the scalar as an math operator?
IE: $result = ($operand $operator $operand) ?  $result = ($operand + $operand).  Thanks!
davidt@t-online.de
0
Comment
Question by:kenai
  • 2
4 Comments
 
LVL 2

Accepted Solution

by:
azawawi earned 50 total points
Comment Utility
if( $operator eq "+" ) {
  .....
} else if( $operator eq "-" ) {
}

i sense that what you're asking is not available in a language like Perl but is found in functional languages such LISP (you could construct a list and since every list is actually a statement of the form (operator operand1 operand2 ... operandN) you could execute it....

I dont think this is available in Perl....

Hope this helps....
0
 
LVL 4

Expert Comment

by:adam923
Comment Utility
it could be done as an eval but it's much easier and safer just to make a separate case for each operator, as azawawi shows above
0
 
LVL 2

Expert Comment

by:azawawi
Comment Utility
playing with lists & functions is a game than LISP (and its dialects) do well....
0
 
LVL 84

Expert Comment

by:ozo
Comment Utility
#you could do
$result = eval "$operand $operator $operand";
#but that could be dangerous if you allow someone to set something like
$operator = ',system("delete *.*"),'

#it may be safer to use  something like
$result = &{${{
'+' => sub {$_[0] + $_[1]},
'-' => sub {$_[0] - $_[1]},
'*' => sub {$_[0] * $_[1]},
'/' => sub {$_[0] / $_[1]},
}}{$operand}||sub{undef}}($operand,$operand);
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now