Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re^4: Perl6: Dynamic Grammars

by raiph (Deacon)
on Jun 12, 2016 at 18:16 UTC ( [id://1165429]=note: print w/replies, xml ) Need Help??


in reply to Re^3: Perl6: Dynamic Grammars
in thread Perl6: Dynamic Grammars

You can name a capture if you think that's an improvement over numbered captures (see the two lines with comments):

my regex line { ^^ (\w+) :my $s; <?{ $s = %sigils{$0} }> \h+ $s $<foo>=\N* $s # Note the foo bit here ... $$ } is ('bang !one!' ~~ m/<line>/)<line><foo>, 'one', 'is bang!one'; # +... and here

Another tweak is to drop one level of hash keys by using a grammar (again, see the two lines with comments):

grammar g { regex TOP { # drop 'my'; name 'TOP' to simplify .parse call in `is +` test ^^ (\w+) :my $s; <?{ $s = %sigils{$0} }> \h+ $s $<foo>=\N* $s $$ } } is g.parse('bang !one!')<foo>, 'one', 'is bang!one'; # calling +.parse method on grammar defaults to starting with rule (regex) named + 'TOP'

perl6.party (not mine)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1165429]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (5)
As of 2024-03-28 14:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found