Hi,

David Green wrote:
> I thought I'd have a go at command-line parsing 
> (http://perlgeek.de/blog-en/perl-6/contribute-now-main-sub.html), 
> and I've got the basics, even though I keep trying to put P5isms in my 
> regexes!  
> (I even found a bug, but it's already reported, RT 73608.)

Thanks for giving it a shot!

> I wanted to check that named regexes aren't in Rakudo yet --
> I get "Useless declaration of has-scoped regex in a module".  

Named regexes are pretty much like methods, not subs.
So the default scope is the same as method (which is in a method table,
not in a package), which is pretty useless outside a grammar. Which is
why you get that warning.

In ideal Perl 6, you'd write

my regex word { <alpha>+ }
if "00 foo" ~~ /<word>/ {
   say $<word>;          # prints "foo"
}

But Rakudo currently doesn't support lookup from lexical scope. So I
currently know of only two ways to use named regexes:

my regex word { <alpha>+ }
my $m = "00 foo".match(&word);
say "alive";
if $m {
   say $m;          # prints "foo"
}

Or as part of a grammar, which only matches full strings (anchored to
start and end, no searching for matches inside a string):

grammar MyGrammar {
    regex word { <alpha>+ }
    regex TOP { <word> };
}
MyGrammar.parse('thisisaword');
say $/<word>;


I hope one of those workarounds helps you.

Cheers,
Moritz

Reply via email to