in reply to what difference between eval and do ?

First of all, make the intent of your code clear.

grep { s/\d+/a/ } @dd;
should be
@dd = map { s/\d+/a/r } @dd;
or
for (@dd) { s/\d+/a/; }
or
s/\d+/a/ for @dd;

Unlike grep and do, eval makes a copy of the values returned like a sub call.

$ perl -E'@dd = 0..9; s/\d+/a/ for @dd; say @dd;' aaaaaaaaaa $ perl -E'@dd = 0..9; s/\d+/a/ for grep { 1 } @dd; say @dd;' aaaaaaaaaa $ perl -E'@dd = 0..9; s/\d+/a/ for do { @dd }; say @dd;' aaaaaaaaaa $ perl -E'@dd = 0..9; s/\d+/a/ for sub :lvalue { @dd }->(); say @dd;' aaaaaaaaaa $ perl -E'@dd = 0..9; s/\d+/a/ for sub { @dd }->(); say @dd;' 0123456789 $ perl -E'@dd = 0..9; s/\d+/a/ for map { $_ } @dd; say @dd;' 0123456789 $ perl -E'@dd = 0..9; s/\d+/a/ for eval { @dd }; say @dd;' 0123456789

Replies are listed 'Best First'.
Re^2: [OT]: what difference between eval and do ?
by AnomalousMonk (Archbishop) on Apr 19, 2018 at 04:38 UTC
    grep { s/\d+/a/ } @dd;

    This is another instance of a mystery of which I've only recently become aware: The (mis(mis))use of grep in place of the (mis)use of map in place of a for-loop. See this discussion. Just for the satisfaction of my own curiosity, if you or anyone can offer any insight into the rationale behind this strange map/grep usage, I'd be mighty obliged.


    Give a man a fish:  <%-{-{-{-<

      This is another instance of a mystery of which I've only recently become aware: The (mis(mis))use of grep in place of the (mis)use of map in place of a for-loop.
      Definitely agreed that it's a strange (mis|ab)?use of grep, but the map variant seems pretty canonical to me - it transforms the elements of a list and stores the resulting new list. The only thing that strikes me as questionable about it is that it overwrites the original list with the new one.

      This is just a case of "When all you have is a hammer, everything looks like a nail". Drives me nutty too...

        Update: indeed I was able to reproduce it.

        My JS add on to produce monastery html tags plays a role here because multiple clicks produce different messages.

        A nonce could solve this. ..

        Cheers Rolf
        (addicted to the Perl Programming Language and ☆☆☆☆ :)
        Wikisyntax for the Monastery

        I think these duplicates are caused by multiple clicks on send button because of a delayed response.

        Not sure if I have to blame my tablet's browser, my wifi router, my dsl provider or the monastery.

        probably all of them.

        Trying to reproduce it. ...

        Cheers Rolf
        (addicted to the Perl Programming Language and ☆☆☆☆ :)
        Wikisyntax for the Monastery

Re^2: what difference between eval and do ?
by Anonymous Monk on Apr 19, 2018 at 06:12 UTC
    Understood! eval is actually a anonymous sub call, but do is just a block! Thank you very much ikegami!
      eval is also Perl's mechanism for exception-trapping.

        I didn't take the title literally. The OP appears to asking why do BLOCK and eval BLOCK behave differently in the provided code. I did not set to list all the difference between eval BLOCK and do BLOCK, at least one of which hasn't been mentioned yet (do BLOCK's interaction with the while statement modifier).