Velocity Reviews > Perl > function to return its argument literally

# function to return its argument literally

Eric Smith
Guest
Posts: n/a

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

Eric

Jürgen Exner
Guest
Posts: n/a

 11-15-2003
Eric Smith wrote:
> 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

Eric Smith
Guest
Posts: n/a

 11-15-2003
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]"))
};

In article <3fb690e5\$0\$88376\$(E-Mail Removed)>, Eric Smith wrote:
> How do I get a function foo() below to return both `1+1' and `2' ?
> foo( 1+1 );
>
> Eric

Malte Ubl
Guest
Posts: n/a

 11-17-2003
Eric Smith wrote:

> 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