develooper Front page | perl.perl6.users | Postings from October 2018

Re: sub name has unexpected interaction with s///

Thread Previous | Thread Next
Richard Hainsworth
October 23, 2018 01:49
Re: sub name has unexpected interaction with s///
Message ID:
Short answer: Yes.

Longer: Perl 6 allows you to over-ride the names of routines. 's' is a 
routine. You over-rode it.

Perl 6 is different from most other languages because it uses multiple 
dispatch. Effectively this means it is not just the name of the 
subroutine (s) that matters, but also its signature (the thing that 
follows the name and before the block {...} defining its action).

Your declaration of 'sub s {1}' defined a routine with a zero length 

When you wrote '$str ~~ s:g/ b /x/', you were ultimately calling a 
subroutine 's' with a signature that has adverbs (:g) and a regex.

But without the ':g', the compiler choose your subroutine 's', which 
meant that it needed to decide what 'b' was, but couldn't.

This is a good thing because it means that you can create a subroutine 
's' for your own objects without the compiler blowing up and telling you 
that you can't use 's'.

On 23/10/18 04:52, Richard Hogaboom wrote:
> The following code:
> use v6;
> my $str = 'abc';
> sub s {1};
> say s;
> $str ~~ s:g/ b /x/;
> dd $str;
> say $/;
> outputs:
> 1
> Str $str = "axc"
> (「b」)
> as expected.
> But, just remove the :g global flag and:
> ===SORRY!=== Error while compiling 
> /home/hogaboom/hogaboom/Perl6/p6ex/./t.p6
> Undeclared routine:
>     b used at line 10
> results.
> Is the sub name s() interfering with the s/// somehow, but s:g/// does 
> not?
> rahogaboom

Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About