# function to return its argument literally

Discussion in 'Perl Misc' started by Eric Smith, Nov 15, 2003.

1. ### Eric SmithGuest

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

Eric

Eric Smith, Nov 15, 2003

2. ### Jürgen ExnerGuest

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

Jürgen Exner, Nov 15, 2003

3. ### Eric SmithGuest

Re: function to return its argument literally - possible solution

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\$>, Eric Smith wrote:
> How do I get a function foo() below to return both `1+1' and `2' ?
> foo( 1+1 );
>
> Eric

Eric Smith, Nov 15, 2003
4. ### Malte UblGuest

Re: function to return its argument literally - possible solution

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

Malte Ubl, Nov 17, 2003