Perl6 regular expression ("regex") syntax has a number of improvements over the Perl5 syntax. The inclusion of grammars as first-class entities in the language makes many uses of regexes clearer, simpler, and more maintainable. This talk looks at a few improvements in the regex syntax and also at how grammars can help make regex use cleaner and simpler.
7. Parse *.ini
for my $nextline (readline($INI_FILE)) {
# If it's a header line, start a new section...
if ($nextline =~ /$header/) {
$section = $config{ $+{name} } //= {};
}
# If it's a property, add the key and value to the current section...
elsif ($nextline =~ /$property/) {
$section->{ $+{key} } = $+{value};
}
# Ignore comments or empty lines
elsif ($nextline =~ /$comment|$empty_line/) {
# Do nothing
}
# Report anything else as a probable error...
else {
warn "Invalid data in INI file at line $.n"
. "t$nextlinen";
}
}
Code processes it.
27. Aside: Future speed
Perl6 regexen execute as DFA where possible.
Opportunity for much faster execution.
For some definition of ...
28. Aside: Future speed
Perl6 regexen execute as DFA where possible.
Also benefit from saner results.
DFA more likely to Do What I Mean.
Alternations match best, not first match.
31. Smartest matching
Perl 5:
say $& if 'Which regex engine is smartest?'
=~ /smart|smarter|smartest/;
Perl 6:
say $/ if 'Which regex engine is smartest?'
~~ /smart|smarter|smartest/;
32. Smartest matching
Perl 5:
say $& if 'Which regex engine is smartest?'
=~ /smart|smarter|smartest/;
Perl 6:
say $/ if 'Which regex engine is smartest?'
~~ /smart|smarter|smartest/;
33. Smartest matching
Perl 5:
say $& if 'Which regex engine is smartest?'
=~ /smart|smarter|smartest/;
Perl 6:
say $/ if 'Which regex engine is smartest?'
~~ /smart|smarter|smartest/;
34. Perl5 nested structure
#! /usr/bin/env perl
use 5.010;
# We're going to need this to extract hierarchical data structures...
our @stack = [];
my $LIST = qr{
# Match this...
(?&NESTED)
# Which is defined as...
(?(DEFINE)
(?<NESTED>
# Keep track of recursions on a stack...
(?{ local @::stack = (@::stack, []); })
# Match a list of items...
[ s* (?>
(?&ITEM)
(?:
s* , s* (?&ITEM)
)*+
)? s*
]
# Pop the stack and add that frame to the growing data structure...
(?{ local @::stack = @::stack;
my $nested = pop @stack;
push @{$::stack[-1]}, $nested;
})
)
# For each item, push it onto the stack if it's a leaf node...
(?<ITEM>
(d+) (?{ push @{$stack[-1]}, $^N })
| (?&NESTED)
)
)
}x;
# Match, extracting a data structure...
'[1,2,[3,3,[4,4]],5]' =~ /$LIST/;
# Retrieve the data structure...
my $parse_tree = pop @stack;
# Show it...
use Data::Dumper 'Dumper';
say Dumper($parse_tree);
35. Perl6 nested grammar
#! /usr/bin/env perl6
use v6;
# Define the structure of a list...
grammar LIST {
rule TOP { '[' <ITEM>* % ',' ']' }
token ITEM { d+ | <TOP> }
}
# Define how to convert list elements to a suitable data structure...
class TREE {
method TOP ($/) { make [ $<ITEM>».ast ] }
method ITEM ($/) { make $<TOP>.ast // +$/ }
}
# Parse, extracting the data structure...
my $parse_tree = LIST.parse('[1,2,[3,3,[4,4]],5]', :actions(TREE)).ast;
# Show what we got...
say $parse_tree.perl;