function to return its argument literally

E

Eric Smith

How do I get a function foo() below to return both `1+1' and `2' ?
foo( 1+1 );

Eric
 
J

Jürgen Exner

Eric said:
How do I get a function foo() below to return both `1+1' and `2' ?
foo( 1+1 );

You don't.
Your function foo() recieves only one argument which is the result of the
evaluation of the expression 1+1 which happens to be 2.
foo() has no way of knowing that there was anything else.

If you want foo() to know about the "literal" argument, then you need to
pass that as a text string:

foo ('1+1');

Or, depending on what your original problem is, maybe make it a
three-argument call:

foo( 1, '+', 1);

But I got a feeling that we are looking at an x-y problem. Meaning, you got
a problem x and you believe that y would be the best way to solve it,
therefore you are asking how to do y.
What is your x? Chances are there is a better way to do it then have
functions backtrack their arguments (which is not possible anyway).

jue
 
E

Eric Smith

Following up mu own post -

funny how posting a question makes you focus more clearly:
Maybe this is a solution

calling:
render( "1+1" );

.... and using eval in the sub:

sub render{
$_[0].
sprintf("%.2f", nearest(0.05, eval"$_[0]"))
};
 
M

Malte Ubl

Eric said:
Following up mu own post -

funny how posting a question makes you focus more clearly:
Maybe this is a solution

No. For lazy evaluation use closures. The example code you provided
would work perfectly with a regular function call, though.

malte
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top