On 2020-05-05 William Michels via perl6-users <perl6-us...@perl.org> wrote: > If the only difference between the "-n" and "-p" flags is really that > the second one autoprints $_, I would have expected the "-pe" code > above to work identically to the "-ne" case (except "-ne" requires a > print, put or say). Presumably 'perl6 -ne "put .chop" ' is the same > as 'perl6 -ne ".chop.put" ' , so if ".put" isn't returning $_ , > what is it returning then?
Ok, let's ask rakudo what the difference is between the two programs:: $ diff -uw <(rakudo --target=ast -ne '.chop') \ <(rakudo --target=ast -pe '.chop') --- /dev/fd/63 2020-05-05 20:36:57.262256451 +0100 +++ /dev/fd/62 2020-05-05 20:36:57.263256462 +0100 @@ -78,6 +78,7 @@ - QAST::Op(p6scalarfromdesc) - QAST::WVal(ContainerDescriptor::Untyped) - QAST::Var(local __lowered_param_decont__1) + - QAST::Stmts - QAST::Stmts <sunk> .chop - QAST::Stmt <sunk final> .chop - QAST::Want <sunk> @@ -90,6 +91,8 @@ - QAST::Op(hllize) <sunk> :statement_id<1> - QAST::Op(callmethod chop) chop - QAST::Var(lexical $_) <wanted> + - QAST::Op(call &say) + - QAST::Var(lexical $_) - QAST::Stmts - QAST::Op(bind) - QAST::Var(local ctxsave :decl(var)) Which tells us that the only difference is (as documented) that ``-p`` adds a ``say $_`` at the end of the body of the loop. So, ``-ne 'put .chop'`` is roughly equivalent to:: for $*ARGFILES.lines { put .chop } Here ``.chop`` (or its fully explicit version, ``$_.chop``) returns the chopped line, which gets passed to ``put``, which prints it. On the other had, ``-pe '.chop'`` is roughly equivalent to:: for $*ARGFILES.lines { .chop; say $_ } Here, ``.chop`` still returns the chopped line, but that value is thrown away. Then, independently of that, we print the value of ``$_``. In both cases, nothing is changing the value of ``$_`` inside each iteration of the loop. The two programs behave differently because they are not the same program! Two pairs of equivalent programs would be:: raku -pe '.chop' raku -ne '.chop; .say' raku -pe '.=chop' raku -ne '.=chop; .say' Does this help? -- Dakkar - <Mobilis in mobile> GPG public key fingerprint = A071 E618 DD2C 5901 9574 6FE2 40EA 9883 7519 3F88 key id = 0x75193F88