Re: More questions on the "-pe" one-liner flag: in conjunction with s/// and tr///

2020-05-06 Thread William Michels via perl6-users
Thank you Laurent!

Are there any other "operators that modify their operands" in
Raku/Perl6 that don't require an initializing "." (dot)?

I checked the "subst" command and it requires an initial ".=" when
used with the "-pe" one-liner flag:

mbook:~ homedir$ perl6 -pe '.=subst(/love|like/, "admire"); ' demo1.txt
this is a test,
I admire Unix,
I admire Linux too,
mbook:~ homedir$

Thanks in advance, Bill.


On Wed, May 6, 2020 at 3:18 PM Laurent Rosenfeld
 wrote:
>
> The s/// substitution operator is not a method (and tr/// also not). They 
> both modify their operands, so there is no need anyway for a '.=' syntax, 
> since they do already what the '.=' syntax is aimed at.
>
> Cheers,
> Laurent.
>
> Garanti sans virus. www.avast.com
>
> Le mer. 6 mai 2020 à 23:11, William Michels  a écrit :
>>
>> Hello,
>>
>> Can anyone answer why--in a one-liner using the "-pe" flag--the s///
>> and tr/// functions do not require a "." (dot) preceding the function
>> call? Clearly adding a "." (dot) before either one results in an error
>> (code below). Also, adding a ".=" (dot-equals) sign before the either
>> the s/// or the tr/// function results in an error (code below).
>> Additionally, I would like to know if this is a related-or-different
>> issue compared to the question I posted earlier this week.
>>
>> Any explanation or assistance appreciated,
>>
>> Thank you, Bill.
>>
>> mbook:~ homedir$ cat demo1.txt
>> this is a test,
>> I love Unix,
>> I like Linux too,
>> mbook:~ homedir$
>>
>> mbook:~ homedir$ perl6 -pe 's/love|like/admire/; ' demo1.txt
>> this is a test,
>> I admire Unix,
>> I admire Linux too,
>> mbook:~ homedir$ perl6 -pe '.s/love|like/admire/; ' demo1.txt
>> ===SORRY!=== Error while compiling -e
>> Missing required term after infix
>> at -e:1
>> --> .s/love|like/admire/;
>> expecting any of:
>> prefix
>> term
>> mbook:~ homedir$ perl6 -pe '.=s/love|like/admire/; ' demo1.txt
>> ===SORRY!=== Error while compiling -e
>> Missing required term after infix
>> at -e:1
>> --> .=s/love|like/admire/;
>> expecting any of:
>> prefix
>> term
>> mbook:~ homedir$
>>
>> mbook:~ homedir$ perl6 -pe 'tr/aeiou/12345/;' demo1.txt
>> th3s 3s 1 t2st,
>> I l4v2 Un3x,
>> I l3k2 L3n5x t44,
>> mbook:~ homedir$ perl6 -pe '.tr/aeiou/12345/;' demo1.txt
>> ===SORRY!=== Error while compiling -e
>> Missing required term after infix
>> at -e:1
>> --> .tr/aeiou/12345/;
>> expecting any of:
>> prefix
>> term
>> mbook:~ homedir$ perl6 -pe '.=tr/aeiou/12345/;' demo1.txt
>> ===SORRY!=== Error while compiling -e
>> Missing required term after infix
>> at -e:1
>> --> .=tr/aeiou/12345/;
>> expecting any of:
>> prefix
>> term
>> mbook:~ homedir$


Re: More questions on the "-pe" one-liner flag: in conjunction with s/// and tr///

2020-05-06 Thread Laurent Rosenfeld via perl6-users
The s/// substitution operator is not a method (and tr/// also not). They
both modify their operands, so there is no need anyway for a '.=' syntax,
since they do already what the '.=' syntax is aimed at.

Cheers,
Laurent.


Garanti
sans virus. www.avast.com

<#DAB4FAD8-2DD7-40BB-A1B8-4E2AA1F9FDF2>

Le mer. 6 mai 2020 à 23:11, William Michels  a
écrit :

> Hello,
>
> Can anyone answer why--in a one-liner using the "-pe" flag--the s///
> and tr/// functions do not require a "." (dot) preceding the function
> call? Clearly adding a "." (dot) before either one results in an error
> (code below). Also, adding a ".=" (dot-equals) sign before the either
> the s/// or the tr/// function results in an error (code below).
> Additionally, I would like to know if this is a related-or-different
> issue compared to the question I posted earlier this week.
>
> Any explanation or assistance appreciated,
>
> Thank you, Bill.
>
> mbook:~ homedir$ cat demo1.txt
> this is a test,
> I love Unix,
> I like Linux too,
> mbook:~ homedir$
>
> mbook:~ homedir$ perl6 -pe 's/love|like/admire/; ' demo1.txt
> this is a test,
> I admire Unix,
> I admire Linux too,
> mbook:~ homedir$ perl6 -pe '.s/love|like/admire/; ' demo1.txt
> ===SORRY!=== Error while compiling -e
> Missing required term after infix
> at -e:1
> --> .s/love|like/admire/;
> expecting any of:
> prefix
> term
> mbook:~ homedir$ perl6 -pe '.=s/love|like/admire/; ' demo1.txt
> ===SORRY!=== Error while compiling -e
> Missing required term after infix
> at -e:1
> --> .=s/love|like/admire/;
> expecting any of:
> prefix
> term
> mbook:~ homedir$
>
> mbook:~ homedir$ perl6 -pe 'tr/aeiou/12345/;' demo1.txt
> th3s 3s 1 t2st,
> I l4v2 Un3x,
> I l3k2 L3n5x t44,
> mbook:~ homedir$ perl6 -pe '.tr/aeiou/12345/;' demo1.txt
> ===SORRY!=== Error while compiling -e
> Missing required term after infix
> at -e:1
> --> .tr/aeiou/12345/;
> expecting any of:
> prefix
> term
> mbook:~ homedir$ perl6 -pe '.=tr/aeiou/12345/;' demo1.txt
> ===SORRY!=== Error while compiling -e
> Missing required term after infix
> at -e:1
> --> .=tr/aeiou/12345/;
> expecting any of:
> prefix
> term
> mbook:~ homedir$
>


Re: More questions on the "-pe" one-liner flag: in conjunction with s/// and tr///

2020-05-06 Thread Gianni Ceccarelli
On 2020-05-06 William Michels via perl6-users 
wrote:
> Can anyone answer why--in a one-liner using the "-pe" flag--the s///
> and tr/// functions do not require a "." (dot) preceding the function
> call?

Because they're not function calls, but *mutating* operators. As the
documentation says
https://docs.raku.org/language/operators#s///_in-place_substitution
``s///`` is an "in-place substitution":

s/// operates on the $_ topical variable, changing it in place

and ``tr///``
https://docs.raku.org/language/operators#tr///_in-place_transliteration
says the same:

tr/// operates on the $_ topical variable and changes it in place

And as usual, ``-p`` prints the value of ``$_`` at the end of each
loop, so you get the modified value.

-- 
Dakkar - 
GPG public key fingerprint = A071 E618 DD2C 5901 9574
 6FE2 40EA 9883 7519 3F88
key id = 0x75193F88


More questions on the "-pe" one-liner flag: in conjunction with s/// and tr///

2020-05-06 Thread William Michels via perl6-users
Hello,

Can anyone answer why--in a one-liner using the "-pe" flag--the s///
and tr/// functions do not require a "." (dot) preceding the function
call? Clearly adding a "." (dot) before either one results in an error
(code below). Also, adding a ".=" (dot-equals) sign before the either
the s/// or the tr/// function results in an error (code below).
Additionally, I would like to know if this is a related-or-different
issue compared to the question I posted earlier this week.

Any explanation or assistance appreciated,

Thank you, Bill.

mbook:~ homedir$ cat demo1.txt
this is a test,
I love Unix,
I like Linux too,
mbook:~ homedir$

mbook:~ homedir$ perl6 -pe 's/love|like/admire/; ' demo1.txt
this is a test,
I admire Unix,
I admire Linux too,
mbook:~ homedir$ perl6 -pe '.s/love|like/admire/; ' demo1.txt
===SORRY!=== Error while compiling -e
Missing required term after infix
at -e:1
--> .s/love|like/admire/;
expecting any of:
prefix
term
mbook:~ homedir$ perl6 -pe '.=s/love|like/admire/; ' demo1.txt
===SORRY!=== Error while compiling -e
Missing required term after infix
at -e:1
--> .=s/love|like/admire/;
expecting any of:
prefix
term
mbook:~ homedir$

mbook:~ homedir$ perl6 -pe 'tr/aeiou/12345/;' demo1.txt
th3s 3s 1 t2st,
I l4v2 Un3x,
I l3k2 L3n5x t44,
mbook:~ homedir$ perl6 -pe '.tr/aeiou/12345/;' demo1.txt
===SORRY!=== Error while compiling -e
Missing required term after infix
at -e:1
--> .tr/aeiou/12345/;
expecting any of:
prefix
term
mbook:~ homedir$ perl6 -pe '.=tr/aeiou/12345/;' demo1.txt
===SORRY!=== Error while compiling -e
Missing required term after infix
at -e:1
--> .=tr/aeiou/12345/;
expecting any of:
prefix
term
mbook:~ homedir$


Re: Inconsistency between one-liner flags (-ne, -pe) using chop example?

2020-05-06 Thread William Michels via perl6-users
Thank you, Gianni (and Laurent). I really appreciate you both taking
the time to explain the intricacies of Raku/Perl6. Sincerely, Bill.


On Wed, May 6, 2020 at 3:46 AM Gianni Ceccarelli  wrote:
>
> On 2020-05-06 William Michels via perl6-users 
> wrote:
> > 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)"?
>
> That's a very good question!
>
> My best attempt at an answer:
>
> * subroutines that are "pure functions" (i.e. have no side effects)
>   can be marked with the ``pure`` trait (e.g. ``sub sqrt($) is pure``
>   is in the standard library)
> * the parser or the optimiser (whoever notices first) will indeed emit
>   the warning "useless use of ..." when they see that the result of
>   calling a pure function is sunk
> * but we're not calling a function, we're calling a method!
> * and (currently) Rakudo can't infer the type of the object we're
>   using (it could, since the return value of ``IO::CatHandle::lines``
>   can be declared, and thus the type of ``$_`` in that loop could be
>   inferred, but it's hard and hasn't been done yet)
> * so Rakudo can't currently know what code that ``.chop`` will end up
>   calling, and if it has side-effects or not
> * so it can't safely warn (it would be like warning that you're not
>   checking the return value of ``say``)
> * yes, there is a sub version of ``chop`` (you can write ``chop($_)``
>   instead of ``$_.chop``), but it just delegates to the method, so
>   without full type inference and data-flow analysis we still can't
>   know if the method "is pure"
>
> Hope this helps!
>
> --
> Dakkar - 
> GPG public key fingerprint = A071 E618 DD2C 5901 9574
>  6FE2 40EA 9883 7519 3F88
> key id = 0x75193F88


Re: Inconsistency between one-liner flags (-ne, -pe) using chop example?

2020-05-06 Thread Gianni Ceccarelli
On 2020-05-06 William Michels via perl6-users 
wrote:
> 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)"?

That's a very good question!

My best attempt at an answer:

* subroutines that are "pure functions" (i.e. have no side effects)
  can be marked with the ``pure`` trait (e.g. ``sub sqrt($) is pure``
  is in the standard library)
* the parser or the optimiser (whoever notices first) will indeed emit
  the warning "useless use of ..." when they see that the result of
  calling a pure function is sunk
* but we're not calling a function, we're calling a method!
* and (currently) Rakudo can't infer the type of the object we're
  using (it could, since the return value of ``IO::CatHandle::lines``
  can be declared, and thus the type of ``$_`` in that loop could be
  inferred, but it's hard and hasn't been done yet)
* so Rakudo can't currently know what code that ``.chop`` will end up
  calling, and if it has side-effects or not
* so it can't safely warn (it would be like warning that you're not
  checking the return value of ``say``)
* yes, there is a sub version of ``chop`` (you can write ``chop($_)``
  instead of ``$_.chop``), but it just delegates to the method, so
  without full type inference and data-flow analysis we still can't
  know if the method "is pure"

Hope this helps!

-- 
Dakkar - 
GPG public key fingerprint = A071 E618 DD2C 5901 9574
 6FE2 40EA 9883 7519 3F88
key id = 0x75193F88


Re: Inconsistency between one-liner flags (-ne, -pe) using chop example?

2020-05-06 Thread William Michels via perl6-users
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
 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 
>  a écrit :
>>
>> On Tue, May 5, 2020 at 8:01 AM Gianni Ceccarelli  
>> wrote:
>> >
>> > On 2020-05-05 William Michels via perl6-users 
>> > 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.