El jue., 20 dic. 2018 a las 23:17, ToddAndMargo via perl6-users (< perl6-us...@perl.org>) escribió:
> >> El jue., 20 dic. 2018 21:43, ToddAndMargo via perl6-users > >> <perl6-us...@perl.org <mailto:perl6-us...@perl.org>> escribió: > >> > >> Hi All, > >> > >> Exactly what is type "Match"? > >> > >> Here I want $D0..$D3 to only be strings. And it throws a match > error. > >> > >> $ p6 'my $x="11.2.3.4"; my Str $D0; my Str $D1; my Str $D2; my Str > $D3; > >> $x~~m{ (<:N>) [.] (\d+) [.] (\d+) [.] (\d+) }; $D0 = $0; $D1 = $1; > >> $D2 = > >> $2; $D3 = $3; print "$D0 $D1 $D2 $D3\n";' > >> > >> Type check failed in assignment to $D0; expected Str but got Match > >> (Match.new(from => 1, made ...) > >> in block <unit> at -e line 1 > >> > >> Here is my work around: > >> > >> $ p6 'my $x="11.2.3.4"; my Str $D0; my Str $D1; my Str $D2; my Str > $D3; > >> $x~~m{ (<:N>+) [.] (\d+) [.] (\d+) [.] (\d+) }; $D0 = $0.Str; $D1 = > >> $1.Str; $D2 = $2.Str; $D3 = $3.Str; print "$D0 $D1 $D2 $D3\n";' > >> 11 2 3 4 > >> > >> > >> Many thanks, > >> -T > > On 12/20/18 2:08 PM, JJ Merelo wrote: > > Put a wriggly ~ in front of $0 to turn it into a Str; it's the Str > > contextualizer > > > > Hi JJ, > > You did not actually answer the question I asked. What is type "Match"? > I didn't think I needed to answer a question that can be so easily obtained from the documentation: https://docs.perl6.org/type/Match, which, unsurprisingly, says: "Match objects are the result of a successful regex match" > > And I am missing something in your answer > > This works: > > $ p6 'my $x="11.2."; my Str $D0; my Str $D1; $x~~m{ (<:N>+) [.] (\d+) }; > $D0 = $0.Str; $D1 = $1.Str; print "$D0 $D1\n";' > 11 2 > > > This does not. One with a space after the ~, one without it. > > $ p6 'my $x="11.2."; my Str $D0; my Str $D1; $x~~m{ (<:N>+) [.] (\d+) }; > $D0 ~$0; $D1 ~ $1; print "$D0 $D1\n";' > WARNINGS for -e: > Useless use of "~" in expression "$D1 ~ $1" in sink context (line 1) > Useless use of "~" in expression "$D0 ~$0" in sink context (line 1) > Use of uninitialized value of type Str in string context. > It does not because you are missing the =. ~ is the contextualizer, it's not or even pretends to be an assignment operator. It's exactly the same as .Str actually. And it works that way because $0 is a Match, and matches in a string context (that is, when .Str is called, which is actually what ~ does) reduce to the string contained in the match. You can also just not declare D0 as a Str. " are also string contextualizers, so my $x="11.2."; $x~~m{ (<:N>+) [.] (\d+) }; print "$0 $1\n"; # OUTPUT: «11 2» $0 and $1 are contextualized to Strings directly. Cheers -- JJ