Re: Is there another way to define a regex?
Curious but are the non capturing groups necessary? On 17 Jan 2016 11:35 p.m., "Tom Browder"wrote: > On Sun, Jan 17, 2016 at 3:03 PM, Moritz Lenz wrote: > > On 01/17/2016 06:07 PM, Tom Browder wrote: > ... > >> # regex of dirs to ignore > >> my regex dirs { > >> \/home\/user\/\.cpan | > >> \/home\/tbrowde\/\.emacs > >> } > > > > Better written as > > > > my regex dirs { > >| '/home/user/.cpan' > >| '/home/tbowde/.emacs' > > } > > > > Yes, quoting does now work in regexes too. Cool, right? :-) > > Yes, very cool! I have decided to use that format and it does ease > adding or modifying entries. > > My final regex looks like this (shortened to only a couple of entries): > > my regex dirs { > ^ > \s* > [# <= non-capture grouping >| '/home/user/.cpan' >| '/home/tbowde/.emacs' ># more entries... > ] > } > > used like this: > > next LINE if $line ~~ //; > > Thanks, Moritz! > > Best, > > -Tom >
Re: Is there another way to define a regex?
On 2016-01-17 Tom Browderwrote: > My question: Is there a way to have Perl 6 do the required escaping > for the regex programmatically, i.e., turn this: > > my $str = '/home/usr/.cpan'; > > into this: > > my regex dirs { > \/home\/usr\/\.cpan > } > > automatically? Yes! And it's also simpler than in Perl 5. I Perl 5, you would have to do something like: my $dirs = qr{\Q$str}; but in Perl 6 you just do: my $dirs = regex { $str }; because the other behaviour, to interpret the contents of $str as another regex to match, has more explicit syntax: regex { <$another_regex> } For your initial use-case there is also another shortcut: an array is interpreted as an alternation, so you could write: my @dirs = < /home/user/.cpan /home/tbrowde/.emacs >; my $regex = regex { @dirs }; and it would do what your Perl 5 example does. If you want to be more restrictive, you can anchor the alternation: my $regex = regex { ^ @dirs }; Here is a complete program: use v6; my @dirs = < foo bar baz >; my $matcher = regex { ^ @dirs $ }; for $*IN.lines -> $line { if $line ~~ $matcher { say "<$line> matched"; } else { say "<$line> didn't match"; } } -- Dakkar - GPG public key fingerprint = A071 E618 DD2C 5901 9574 6FE2 40EA 9883 7519 3F88 key id = 0x75193F88 To spot the expert, pick the one who predicts the job will take the longest and cost the most.
Re: Workaround for Perl 5's __DATA__
On Mon, Jan 18, 2016 at 6:47 AM, Kamil Kułagawrote: > You may be happy with =finish block ... Thanks, Kamil! -Tom
'!' versus 'not' in boolean expression
In creating some new Perl 6 programs I've run across several instances I'm confused about, to wit: Example 1 --- > my %h; say 'false' if !%h:exists; Unexpected named parameter 'exists' passed Example 2 --- > my %h; say 'false' if not %h:exists; false It looks like '!' doesn't work as I thought it was supposed to. But, I just discovered that when I use parens, it works. Example 3 --- > my %h; say 'false' if !(%h:exists); false I presume the parens would cure the similar things I've noticed with other classes. When I look at the docs on Operators I see this: prefix ! multi sub prefix:(Mu) returns Bool:D Negated boolean context operator. Coerces the argument to Bool by calling the Bool method on it, and returns the negation of the result. Note that this collapses Junctions. prefix not multi sub prefix:(Mu $x) returns Bool:D Evaluates its argument in boolean context (and thus collapses Junctions), and negates the result. Those two definitions look very similar to my eyes, but I think the subtle difference is intentional.But they are not identical. Is there some rule of thumb here that a Perl 6 wannabe can grasp in Perl 5 terms (e.g., prefer 'not' over '!')? Or am I going to have to go deep early into the object class structure? Many thanks. -Tom
Re: Workaround for Perl 5's __DATA__
You may be happy with =finish block use v6; say $=finish.split("\n").perl; =finish _ I like pancakes And apples On Sat, Jan 16, 2016 at 8:07 PM, Tom Browderwrote: > I have tried this in my Perl 6 code (v6.c): > > =begin DATA > blah > blah2 > =end DATA > > Then: > > for $=DATA.lines -> $line { > # process a line > } > > but I get this message: > > Pod variable @=DATA not yet implemented. Sorry. > > Is there any workaround for this other than putting the data in an array? > > Thanks. > > Best regards, > > -Tom -- Pozdrawiam Kamil Kułaga
Re: '!' versus 'not' in boolean expression
In Perl5, there's "&&" vs "and", "||" vs "or", "^" vs "xor", and "!" vs "not", the difference being precedence. Perhaps it's the same with Perl6...
Re: '!' versus 'not' in boolean expression
> On 18 Jan 2016, at 19:55, Tom Browderwrote: > > In creating some new Perl 6 programs I've run across several instances > I'm confused about, to wit: > > Example 1 > --- > >> my %h; say 'false' if !%h:exists; > Unexpected named parameter 'exists’ passed Yeah, this is an unexpected one. However, there is a simple solution: my %h; say 'false' if %h:!exists; In general, :foo is equivalent to foo => True, and :!foo is equivalent to foo => False. > Example 2 > --- > >> my %h; say 'false' if not %h:exists; > false > > It looks like '!' doesn't work as I thought it was supposed to. But, > I just discovered that when I use parens, it works. Yes, the ! binds closer, and eats the :exists, and then complains about it. Liz
Re: '!' versus 'not' in boolean expression
Too many Reimanns & Not enough role? -jas On 18 January 2016 at 11:37, Elizabeth Mattijsenwrote: > >> On 18 Jan 2016, at 19:55, Tom Browder wrote: >> >> In creating some new Perl 6 programs I've run across several instances >> I'm confused about, to wit: >> >> Example 1 >> --- >> >>> my %h; say 'false' if !%h:exists; >> Unexpected named parameter 'exists’ passed > > Yeah, this is an unexpected one. However, there is a simple solution: > > my %h; say 'false' if %h:!exists; > > In general, :foo is equivalent to foo => True, and :!foo is equivalent to foo > => False. > > >> Example 2 >> --- >> >>> my %h; say 'false' if not %h:exists; >> false >> >> It looks like '!' doesn't work as I thought it was supposed to. But, >> I just discovered that when I use parens, it works. > > Yes, the ! binds closer, and eats the :exists, and then complains about it. > > > > Liz
Re: '!' versus 'not' in boolean expression
On Mon, Jan 18, 2016 at 1:37 PM, Elizabeth Mattijsenwrote: >> On 18 Jan 2016, at 19:55, Tom Browder wrote: >> In creating some new Perl 6 programs I've run across several instances >> I'm confused about, to wit: >> >> Example 1 >> --- >> >>> my %h; say 'false' if !%h:exists; >> Unexpected named parameter 'exists’ passed > > Yeah, this is an unexpected one. However, there is a simple solution: > > my %h; say 'false' if %h:!exists; > > In general, :foo is equivalent to foo => True, and :!foo is equivalent to foo > => False. Okay, I'll just have to get used to it, then. Thanks, Liz! -Tom