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

2020-05-07 Thread Laurent Rosenfeld via perl6-users
Hi William,

I guess that you now have answers to your questions. I would suggest,
however, that you take a look as Andrew Shitov's book *Raku One-Liners*,
available on-line (
https://andrewshitov.com/wp-content/uploads/2020/01/Raku-One-Liners.pdf).

There are (around page 12) some explanations on your original question.

Consider especially the following equivalent one-liner programs:

$ raku-npe'.=flip' data.txt

and

$ raku-ne'.flip.say' data.txt

and the related explanations.

Cheers,
Laurent.




Le jeu. 7 mai 2020 à 10:12, Gianni Ceccarelli  a
écrit :

> On 2020-05-06 William Michels via perl6-users 
> wrote:
> > Are there any other "operators that modify their operands" in
> > Raku/Perl6 that don't require an initializing "." (dot)?
>
> The dot is used to call a method on an object.
>
> > I checked the "subst" command and it requires an initial ".=" when
> > used with the "-pe" one-liner flag:
>
> ``subst`` is a method https://docs.raku.org/type/Str#method_subst
>
> To summarise:
>
> * ``-e`` tells raku "execute the next argument as a program"
>
> * both the ``-p`` and ``-n`` command-line switches wrap the program in
>   a loop like::
>
> for $*ARGFILES.lines -> {
>
> }
>
>   ``-p`` additionally puts a ``say $_`` at the end of the block
>
> * inside that loop, the variable ``$_`` will have its value set to
>   each line of the input, one at a time
>
> * you can do whatever you want inside the loop
>
> * the dot calls a method, and if you don't specify an object to call
>   the method on (on the left hand side of the dot), ``$_`` is used
>
> * some operators (like ``s///`` or ``tr///``) operate on ``$_`` unless
>   told otherwise
>
> * assignment (``=``) can be combined with other operators (``+=``,
>   ``/=``, ``.=``, ``~=``, &c)
>
> If, having seen all that, the behaviour of all your examples is still
> not clear, maybe you should start writing your programs without
> shortcuts and implicit terms: the languages in the Perl family are
> very good at letting you write compact code, but sometimes this comes
> at the expense of clarity. While learning, code that's clear,
> explicit, and straightforward is often more helpful.
>
> --
> Dakkar - 
> GPG public key fingerprint = A071 E618 DD2C 5901 9574
>  6FE2 40EA 9883 7519 3F88
> key id = 0x75193F88
>


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

2020-05-07 Thread Gianni Ceccarelli
On 2020-05-06 William Michels via perl6-users 
wrote:
> Are there any other "operators that modify their operands" in
> Raku/Perl6 that don't require an initializing "." (dot)?

The dot is used to call a method on an object.

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

``subst`` is a method https://docs.raku.org/type/Str#method_subst

To summarise:

* ``-e`` tells raku "execute the next argument as a program"

* both the ``-p`` and ``-n`` command-line switches wrap the program in
  a loop like::

for $*ARGFILES.lines -> {

}

  ``-p`` additionally puts a ``say $_`` at the end of the block

* inside that loop, the variable ``$_`` will have its value set to
  each line of the input, one at a time

* you can do whatever you want inside the loop

* the dot calls a method, and if you don't specify an object to call
  the method on (on the left hand side of the dot), ``$_`` is used

* some operators (like ``s///`` or ``tr///``) operate on ``$_`` unless
  told otherwise

* assignment (``=``) can be combined with other operators (``+=``,
  ``/=``, ``.=``, ``~=``, &c)

If, having seen all that, the behaviour of all your examples is still
not clear, maybe you should start writing your programs without
shortcuts and implicit terms: the languages in the Perl family are
very good at letting you write compact code, but sometimes this comes
at the expense of clarity. While learning, code that's clear,
explicit, and straightforward is often more helpful.

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


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$