Title: sub ScanDirectory my $workDir shift my files opendir DIRHND, $workDir or die "Unable to open $workDi
1sub ScanDirectory my workDir shift my
_at_files opendir( DIRHND, workDir) or
die "Unable to open workDir !\n"
while( my file readdir( DIRHND ) )
next if( file eq "." file eq ".." )
if( -d workDir . file )
push( _at_files, ScanDirectory( workDir . file )
) next push( _at_files,
"file\n" ) closedir( DIRHND )
return _at_files my dir shiftmy
_at_dirContent ScanDirectory( dir )print
_at_dirContent
2kw.pm
- Perl 6 Rules and Grammars
3Introduction
- Talk in two parts
- A quick look and evaluation of what exists to use
now. - A quick look at what is proposed, and what is
good about it. - Then beer.
perl 6 rules and grammars
4State of mi/Onion/
- Here is what exists currently in terms of
implementation - Perl6Rules a CPAN module that implements a
subset of the spec. - The Parrot/Perl6 implementation has 0 so far on
Pattern matching. Pugs is in the same state.
perl 6 rules and grammars
5Perl6Rules
- Perl6Rules was created and published to CPAN in
April 2004 by Damian Conway. - It was originally part of the Perl6 bootstrapping
plan They would implement Ponie and Parrot, and
then use Perl6Rules to compile the initial
Perl6. Then Perl6 could be self-hosting.
perl 6 rules and grammars
6Perl6Rules
- But Perl6Rules simply isnt very good. At
least in my opinion. - It is rich on Damian-esque Source-Filter magic,
but a little low on the set of things not
Source-Filter magic. - First of all, getting it going on Cygwin took me
some time
perl 6 rules and grammars
7Perl6Rules
- I ended up with stuff like this
- Failed 12/41 test scripts, 70.73 okay.
-124/2401 subtests failed, 105.16 okay. - test program seems to have generated a core
- Fatal error in one or more Perl 6 rules panic
regfree data code 'ÿ' during global destruction.
perl 6 rules and grammars
8Perl6Rules
- I know cygwin is a less popular, less robust
place to do business, but 6 cores seemed a little
high. However, Testers shows that it also fails
on Solaris, BSD and Linux. - I also disliked that it depended on YAML, but not
in a way that CPAN.pm understood. - Lastly, it was slow. Deadly slow.
perl 6 rules and grammars
9Perl6Rules
- The other-other issue was that it doesnt
completely implement Rules, nor consistent with
the current spec. - All that said, I was able to play around with the
module, and get a bit of a feel for Rules. - My suspicion is that Perl6 will bootstrap with
Pugs.
perl 6 rules and grammars
10Working Example
- __DATA__
- id centre points (relative to 1x1 unit
square) - centre indicates squares that are
rotationally - symetrical
- ------------------------------------------------
- - 1 (centre) 0, 0 1, 0 1, 1 0, 1
- 2 0, 1 1, 1 1, 0
- 3 0, 0 0.5, 1 1, 0
- 4 (centre) 0, 0.5 0.5, 1 1, 0.5 0.5, 0
- 5 0.5, 0 0.5, 1 1, 1 1, 0
- 6 0, 0 0, 0.5 1, 1 0.5, 0
- 7 0.25, 0.5 0.5, 0 0, 0 0.5, 0.99
\ - 1, 0 0.5, 0 0.75, 0.5 0.25, 0.5
- etc
perl 6 rules and grammars
11Working Example
- grammar NineBlock
- meta bits
- rule continuation \\ \n
- rule blankline
- rule comment \ .?
- top level
- rule config
- rule line w ?
- line components
- rule idnum w ( \d ) \ 1 print
"1\n" - rule centre \( centre center \)
- rule points w
- rule pair w ,
- rule point \d \. \d ?
perl 6 rules and grammars
12Working Example
- slurp
- my config do local /
- heal continuations, ditch blanks and comments
- config s
-
-
-
- g
- validate
- print "valid\n" if config m/
/
perl 6 rules and grammars
13Working Example
- And that was about as far as I got with my
working example. - I spent an awfully long time trying to get it to
capture things automatically, but without much
luck. - The captured results are returned into a tied
global called 0, which was quirky and
unmanageable.
perl 6 rules and grammars
14Interpolation and Closures
- Perl 5
- _at_cmd qw get put try find copy
- cmd join '', map quotemeta _ _at_cmd
- str / (?cmd) \( .? \) /x
- Perl 6
- _at_cmd qw get put try find copy
- str mw/ _at_cmd \( .? \) /
- Or
- my _at_cmds
- str mweach/ ( _at_cmd ) ( \( \N? \) )
- push _at_cmds, cmd(1) arg(2) /
perl 6 rules and grammars
15Qualifiers
- Other qualifiers
- my foo "fee fi fo feh far foo fum "
- foo seach2nd/ ( f \w ) /bar/
- foo now "fee bar fo bar far bar fum "
- my str "ahhh"
- _at_matches str m/ ah /
- returns "ahhh"
- _at_matches str many/ ah /
- returns "ahhh", "ahh", "ah", "a"
perl 6 rules and grammars
16Flow control
- The backtracking controls are cleaner and way
more powerful. But, they force you to think in a
different way. - dont retry previous atom
- fail out of current alternation
- fail out of current rule
- fail out of entire match
- uri m http\ftp\mailto\
- //
perl 6 rules and grammars
17Flowcontrol
- Specifically, they cause you to think about your
regex as a little program. - Better examples
- m/ rm _at_options /
- mw/ if
- for
- loop ? /
- rule subname
- ( _ \w )
- fail if reserved1
-
perl 6 rules and grammars
18Rules/Assertions
- Predefined Rules/Assertions
-
-
-
- ,,
- lookahead/behind
- (was (?pattern))
-
- ,
perl 6 rules and grammars
19More Assertions
- m/ () /
- a closure that evaluates to a rule
- closure that runs at canonical time
- assertion that can be optimized away
- ie. m/ (.) bar /
- symbolic indirect
- short for
- same as p5 a-z_
- same
- negated a-z_
- -A-Za-z set ops
perl 6 rules and grammars
20Yet More Assertions
- aggregate rules
-
- much like bare hash _at_array, but only
- evaluate to rules (string or ref).
- my _at_dog ( rxi Butch , rxi Duke ,
- rxi Teacup , )
- mw/Lets take for a walk./
- Literal assertions
-
perl 6 rules and grammars
21Rules are Subs/Methods
- rule badline( Str errmsg )
- ( \N ) fail "errmsg 1"
-
- other rules
- given( lines )
- m
- line group
- _at_cmd
- command followed by etc.
-
-
-
perl 6 rules and grammars
22Rules are Subs/Methods
- rule block
- \(
- stuff that isnt a block, or a block
-
-
- \)
-
- rule xblock(left, right)
- left
-
- right
-
- right
-
perl 6 rules and grammars
23Readable 0-width assertions
- rule quoted_string (type)
- type
-
-
- type
-
- type
-
- rule string
- (
- )
-
perl 6 rules and grammars
24Assertions and Binding
- rule quoted_string (type)
- type
-
-
-
-
- type
-
- type
-
perl 6 rules and grammars
25Binding
- So, the part I like best about Perl6 Rules is the
ease of reasonable capturing. - In p5, I do this sort of junk all the time
- hash str m/(\w)(\w)/g
- Or, I see people doing stuff like this
- str m/Number\s(\d)/
- my num 1
perl 6 rules and grammars
26Binding
- Or struggling with things like this
- m/(? attr(\w) )/xi
- or
- m/I ((dont)? know (howwhere)) to count from./
- In Perl6 rules, there are lots of nice, clean
ways of binding your results to meaningful,
lexical names.
perl 6 rules and grammars
27Hypothetical Binding
- Starting from least cool, moving towards most
cool - normal lexical
- my num
- m/ (\d) let num 1 () /
- match lexical
- m/ (\d) let 1 () /
- In this case the variable lives in match
- variable / rather than the surrounding
- lexical pad. /num or /num.
- Also available as while / in scope.
perl 6 rules and grammars
28Hypothetical Binding
- Alternations
- m/
- (\d) let 1
- () let 2
- (.) let 3
- /
- Shorthand for binding hypothetically
- m/
- (\d)
- ()
- (.)
- /
perl 6 rules and grammars
29Hypothetical Binding
- hypo-bind n, if desired
- my (key, val) mw
- 1 (\w) \ 2 (.?)
- 2 (.?) \
- Repeated captures can be bound to arrays
- m/ _at_ (.?) , /
- Pairs of repeated captures can be bound to
hashes - m/ () (\N) /
- m/ (\N) /
perl 6 rules and grammars
30Auto Hypothetical Binding
- captures to by default
- m/ ws \ ws
- hashkey value /
- suppresses autocapture
- What exactly happens in recursive-auto-hypo-bind
cases is entirely hazy to me at this time. And I
cant test it. - In theory we are supposed to end up with a nice
tree structure.
perl 6 rules and grammars
31Auto Hypothetical Binding
- captures to by default
- m/ ws \ ws
- hashkey value /
- suppresses autocapture
- What exactly happens in recursive-auto-hypo-bind
cases is entirely hazy to me at this time. And I
cant test it. - In theory we are supposed to end up with a nice
tree structure.
perl 6 rules and grammars
32Example
- grammar NineBlock
- meta bits
- rule continuation \\ \n
- rule blankline
- rule comment \ .?
- top level
- rule config
- rule line w ?
- line components
- rule idnum w ( \d ) \ 1 print
"1\n" - rule centre \( centre center \)
- rule points w
- rule pair w ,
- rule point \d \. \d ?
perl 6 rules and grammars
33Ameliorated Example
- grammar NineBlock
- rule config
- push _at_,
- id ( )
- centre( ?
10 ) - coord ( _at_ )
-
- rule line w ?
- rule points w _at_ ()
- ?
- push _at_,
, -
- rule pair w ,
- rule point ( \d \.\d? )
- rule idnum w ( \d ) \
- rule centre \( centre center \)
perl 6 rules and grammars
34Ameliorated Example
- slurp
- my conffile do local /
- Parse!
- my _at_nodes
- if ( conffile m/ / )
-
- _at_nodes _at_
- else
- die couldnt parse conffile!
-
- if ( _at_node0centre )
- etc
perl 6 rules and grammars
35Conclusion
- In fact, I shouldnt have to do all that pushing
and manual capturing. I should be able to let
the grammar do it for me. - But, I dont know how yet, and I suspect no one
does. - Questions?
- Beers?
perl 6 rules and grammars