in reply to Reverse in scalar context

I ponder the meaning of eval reverse $b. Is that wise? when $b='bus' for example prints nothing (it's undef).

—John

Replies are listed 'Best First'.
Re: Reverse in scalar context
by Abigail (Deacon) on Jul 12, 2001 at 01:12 UTC
    What do you mean with "prints nothing"? What are you trying to print? Note that you are trying to eval the string sub which is a syntax error:
    $ perl -w $b = "bus"; eval reverse $b; print $@; __END__ syntax error at (eval 1) line 2, at EOF
    And if the code doesn't compile, eval will return undef.

    -- Abigail

      If you read the post I was replying to, you'd know that I was not trying to print anything! Rather, I was pointing out this very problem, albeit more subtly, in the original node. I'm quite aware of what it does--I crafted it specifically to do that, yet with a real word might not be immediatly obvious.
Re: Re: Reverse in scalar context
by tachyon (Chancellor) on Jul 12, 2001 at 01:44 UTC

    Interesting. Reverse of 'bus' is 'sub'. Hmm. Is it wise, or necessary? - no you have scalar at your disposal to force scalar context safely. Eval-ing strings (especially some strings ;-) reversed or not is potentially dangerous. It ended up there because I wondered what would happen if I put it there, it worked on the test case, so it stayed. Just simple curiosity. Never used it before and I doubt I'll use it again.

    $b = '}"n\!nhoJ olleH" tnirp{b bus'; print eval reverse $b; &b; $_ = 'web&lave'; print eval reverse; sub AUTOLOAD {print}

    cheers

    tachyon

    s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print