to eval or not to eval?

Discussion in 'Perl Misc' started by Marc Girod, Apr 19, 2011.

  1. Marc Girod

    Marc Girod Guest

    Hello,

    I am trying to eval metacode in a context, and cannot hide the
    context.

    -8<-------
    #!/usr/bin/perl -w

    use strict;
    my $fubar = q(my $foo = 'bar';);

    print eval "$fubar\n";
    while (<DATA>) {
    print eval "${fubar}qq($_)";
    }
    print "End\n";
    __DATA__
    Hello ${foo}!
    -8<-------

    This gives me:

    ~> ./tmp/evtst
    barHello bar!
    End

    The first 'bar' there is what annoys me... Go away!
    But why is it there?
    Yes: it is returned by the assignment to/initialization of $foo...
    Why is it part of the return of 'eval'?

    Thanks
    Marc
    Marc Girod, Apr 19, 2011
    #1
    1. Advertising

  2. Marc Girod

    Marc Girod Guest

    On Apr 19, 1:46 pm, Tad McClellan <> wrote:

    >     the value returned is the value of the last expression evaluated


    That's what I thought.
    So, what I miss is what is 'last expression evaluated' in: 1;2;
    I expected it would be: 2

    ~> perl -le 'print eval "1;2;"'
    2

    Why does eval return the result of the *first* expression as well?
    This is (still) my question.
    Thanks,
    Marc
    Marc Girod, Apr 19, 2011
    #2
    1. Advertising

  3. Marc Girod

    Marc Girod Guest

    On Apr 19, 1:46 pm, Tad McClellan <> wrote:

    > > print eval "$fubar\n";


    Oops... What does this do there?

    I thought I had removed that line!
    Thanks for pointing it.
    Sorry.

    Marc
    Marc Girod, Apr 19, 2011
    #3
    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. Eric Newton
    Replies:
    3
    Views:
    9,391
    Brock Allen
    Apr 4, 2005
  2. Replies:
    0
    Views:
    471
  3. DataBinder.Eval and Eval.

    , Jun 16, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    542
    Karl Seguin [MVP]
    Jun 16, 2006
  4. Alex van der Spek

    eval('07') works, eval('08') fails, why?

    Alex van der Spek, Jan 8, 2009, in forum: Python
    Replies:
    6
    Views:
    1,435
    Bruno Desthuilliers
    Jan 8, 2009
  5. Liang Wang
    Replies:
    8
    Views:
    129
    Ben Morrow
    Feb 2, 2008
Loading...

Share This Page