function to return its argument literally

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

  1. Eric Smith

    Eric Smith Guest

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

    Eric
    Eric Smith, Nov 15, 2003
    #1
    1. Advertising

  2. 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
    #2
    1. Advertising

  3. Eric Smith

    Eric Smith Guest

    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
    #3
  4. Eric Smith

    Malte Ubl Guest

    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
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kentor
    Replies:
    47
    Views:
    1,221
    derbz
    Feb 16, 2007
  2. Replies:
    7
    Views:
    323
    CBFalconer
    Nov 5, 2006
  3. thunk
    Replies:
    1
    Views:
    292
    thunk
    Mar 30, 2010
  4. thunk
    Replies:
    0
    Views:
    457
    thunk
    Apr 1, 2010
  5. thunk
    Replies:
    14
    Views:
    602
    thunk
    Apr 3, 2010
Loading...

Share This Page