develooper Front page | perl.perl6.users | Postings from December 2019

Precedence: assignment vs smartmatch? (...was Re: where is my maptypo)

Thread Next
From:
William Michels via perl6-users
Date:
December 7, 2019 04:34
Subject:
Precedence: assignment vs smartmatch? (...was Re: where is my maptypo)
Message ID:
CAA99HCzRVWumMN5Y3bkF-VQDxVtTpx5vc=u7d9LT0oCzedWrEA@mail.gmail.com
Hello All,

Todd put up some interesting code yesterday using the Raku/Perl6 REPL,
which I reproduced with no problem. Additionally I tried some
variations removing and/or moving parentheses to a different location,
and have numbered the relevant REPL lines 1 through 6:

mbook:~ homedir$ perl6
To exit type 'exit' or '^D'
1> my $x = Q[word</b><br>] ;
word</b><br>
2> (my $y = $x) ~~ s/ '<' .* //; say $/; say $x; say $y;
「</b><br>」
word</b><br>
word
3> my $a = Q[word</b><br>] ;
word</b><br>
4> my $b = ($a ~~ s/ '<' .* //); say $/; say $a; say $b;
「</b><br>」
word
「</b><br>」
> my $c = Q[word</b><br>] ;
word</b><br>
> my $d = $c ~~ s/ '<' .* //; say $/; say $c; say $d;
「</b><br>」
word
「</b><br>」
7> $*VM
moar (2019.07.1)

Working in groups of 2, lines 1 and 2 replicate the code Todd put up
(parenthesis surrounding everything to the left of the smartmatch
operator). I get the same result as Todd. What interests me are lines
3 through 6. Lines 3 and 4 are the virtually the same code but with
parentheses surrounding everything to the right hand side (RHS) of the
assignment operator (" = "). As people will note, lines 2 and lines 4
give different results. Removing parentheses entirely in line 6 gives
the same result as in line 4. Because the results in line 4 and line 6
are the same, this says that as far as parentheses are concerned, the
smartmatch operator "~~" takes precedence over the assignment operator
"=".

What's not clear to me in the code above (lines 4 and 6) is why
variables $b and $d get assigned to $/. I would have expected in line
4 that $a would have been matched against the smartmatch, and the
result ("word") would have been simply copied into variable $b. Have I
misunderstood?

Anyway, I'm just hoping to start a conversation on the topic of
precedence in general, and hopefully getting some feedback as to where
to look in the docs for further instruction.

Best Regards, Bill.


On Fri, Dec 6, 2019 at 12:15 AM ToddAndMargo via perl6-users
<perl6-users@perl.org> wrote:
>
> On 2019-12-05 23:19, ToddAndMargo via perl6-users wrote:
> > On 2019-12-05 03:09, William Michels via perl6-users wrote:
> >> What happens when you type "perl6" or "raku" at the bash command prompt?
> >
> > Hi William,
> >
> > On my shop machine, it jumps to the next line with an
> > empty flashing cursor
> >
> > On my office machine, it told me to install
> >      zef install Readline
> >
> > After that, I get:
> >
> > $ perl6
> > To exit type 'exit' or '^D'
> >  >
> >
> > and
> >
> >  > say "hello World"
> > hello World
> >  > say "B" ~ Q[:\] ~ " drive dismounted"
> > B:\ drive dismounted
> >  >
> >
> > and sticking an obvious booboo into it
> >
> >  > if 3 % 2 = 1 {say "odd"};
> > Cannot modify an immutable Int (1)
> >    in block <unit> at <unknown file> line 1
> >
> > Plus I can use the arrow keys to recall previous lines too.
> >
> > Time up update my Perl6 on my shop computer!
> >
> > No more hassling with `perl6 -e` !!!
> >
> > Dude!  THANK YOU !!!!!!
> >
> > -T
>
> You've created a monster!!
>
> perl6
> To exit type 'exit' or '^D'
>  > my $x = Q[</b><br>]
> </b><br>
>  > say $x
> </b><br>
>  > (my $y = $x ) ~~ s/ Q[<] .* //;
> ===SORRY!=== Error while compiling:
> Unrecognized regex metacharacter < (must be quoted to match literally)
> ------> (my $y = $x ) ~~ s/ Q[<] .* //;
>  > my $x = Q[abc</b><br>]
> abc</b><br>
>  > (my $y = $x ) ~~ s/ '<' .* //;
> 「</b><br>」
>  > (my $y = $x ) ~~ s/ '<' .* //; say $y
> abc
>  > (my $y = $x ) ~~ s/ '<' .* //; say $x; say $y
> abc</b><br>
> abc
>
>
> Thank you!
>
>
> --
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> Computers are like air conditioners.
> They malfunction when you open windows
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About