Dear Laurent (and Gianni),

So if the following code does useless work:

perl6 -pe '.chop' demo1.txt

why doesn't it fail with an error, "Useless use of ... in sink context
(line 1)"?

Best, Bill.

On Tue, May 5, 2020 at 12:43 PM Laurent Rosenfeld
<laurent.rosenf...@googlemail.com> wrote:
>
> In:
> perl6 -ne 'put .chop' demo1.txt
>
> the script prints out the value returned by the chop method, because put acts 
> on this value.
>
> In:
> perl6 -pe '.chop' demo1.txt
> the value returned by chop is discarded and the script print $_ unaltered.
>
> Cheers,
> Laurent.
>
> Le mar. 5 mai 2020 à 21:07, William Michels via perl6-users 
> <perl6-us...@perl.org> a écrit :
>>
>> On Tue, May 5, 2020 at 8:01 AM Gianni Ceccarelli <dak...@thenautilus.net> 
>> wrote:
>> >
>> > On 2020-05-05 William Michels via perl6-users <perl6-us...@perl.org>
>> > wrote:
>> > > mbook:~ homedir$ perl6 -ne 'put .chop' demo1.txt
>> > > this is a test
>> > > I love Unix
>> > > I like Linux too
>> > > mbook:~ homedir$ perl6 -pe '.chop' demo1.txt
>> > > this is a test,
>> > > I love Unix,
>> > > I like Linux too,
>> >
>> > The ``.chop`` method does not mutate its argument, it only returns a
>> > chopped value. If you want to mutate, you need to say so::
>> >
>> >   raku -pe '.=chop' demo1.txt
>> >
>> > Notice that the ``.=`` operator is:
>> >
>> > * not specific to ``chop``
>> > * not even specific to calling methods
>> >
>> > In the same way that ``$a += 1`` is the same as ``$a = $a + 1``, so
>> > ``$a .= chop`` is the same as ``$a = $a.chop``.
>> >
>> > So, if you wanted, you could do::
>> >
>> >   raku -pe '.=uc' # print upper-case
>> >
>> > --
>>
>> I appreciate the reply, but your answer fails to explain one thing:
>> why does chop work without ".=" assignment using the "-ne" one-liner
>> flag, but not with the "-ne" one-liner flag"? According to the help
>> screen (running 'perl6 -help' at the bash command prompt), this is
>> what it says about the "-n" and the "-e" flags:
>>
>> -n                   run program once for each line of input
>> -p                   same as -n, but also print $_ at the end of lines
>>
>> So what strikes me from the definitions above is the part where "-p"
>> is the "same as -n... (with autoprinting of $_)." That leads people to
>> believe that they can write a short one-liner with the -ne flag ('put
>> .chop') and an even shorter one-liner with the -pe flag ('.chop').
>>
>> 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?
>>
>> Look, It's no big deal if I have to write 'perl6 -pe ".=chop" '
>> instead of  'perl6 -pe ".chop" ', I just want to resolve in my mind a
>> perceived inconsistency wherein there's no requirement to write
>> 'perl6 -ne "put .=chop" ' for the "-ne" case, but there IS a
>> requirement to write 'perl6 -pe ".=chop" ' for the "-pe" case.
>>
>> Best Regards, Bill.

Reply via email to