From: Robin Edwards Date: Mon, 14 Dec 2009 10:15:50 +0000 (+0000) Subject: now uses actions keyword in method example, updated docs X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=3ea33f0320ac08c2cf6a5eef0d6d63176ba169d1;p=p5sagit%2FDevel-Declare-Keyword.git now uses actions keyword in method example, updated docs --- diff --git a/README b/README index 202a370..7eea09d 100644 --- a/README +++ b/README @@ -1,5 +1,20 @@ +ABOUT +This module aims to provide an easy to use interface to Devel::Declare. + +If you haven't heard of DD, you have probably been sitting under a rock for the few months, I suggest you take a look at the following modules: + +Devel::Develare +http://search.cpan.org/~flora/Devel-Declare-0.005011/lib/Devel/Declare.pm + +MooseX::Declare +http://search.cpan.org/~flora/MooseX-Declare-0.32/lib/MooseX/Declare.pm + +I really don't know what I am doing, so all feedback / abuse is welcome + TODO -* write more tests and examples +* write more tests, docs and examples +* cleanup parser construction (check if code exists etc) +* write default action to return whatever the parse routine matched SYNTAX #parse an action routines called for each entry in proto @@ -11,6 +26,16 @@ keyword method (ident?, proto?, custom, block) { #passed a Keyword::Parser object parse custom ($parser) { + if (my $len = $parser->scan_word(1)) { + my $l = $parser->line; + my $ident = substr($l, $parser->offset, $len); + substr($l, $parser->offset, $len) = ''; + $parser->line($l); + return $ident if $ident =~ /^[a-z]{1}\w+$/i; + } + else { + die "unable to match blah"; + } } #passed whatever the parse routine matches @@ -19,7 +44,7 @@ action proto ($match) { return $code; } -INFO +YADDA rob: hey, we were talking about this basically, the answer is to standardise the declaration forms so you have something like diff --git a/examples/Methods.pm b/examples/Methods.pm index 162ee1b..ac3d747 100644 --- a/examples/Methods.pm +++ b/examples/Methods.pm @@ -9,13 +9,17 @@ keyword method (ident?, proto?, block) { $block->terminate; } -sub action_ident { shift; } # return method name -sub action_proto { - my $proto = shift; +action proto ($proto) { $proto =~ s/\s//g; $proto = "\$self,$proto" if length($proto); return " my ($proto) = \@_; "; } +# return method name +action ident ($ident) { + return $ident; +} + + 1; diff --git a/lib/Keyword.pm b/lib/Keyword.pm index 23c24b0..a176e46 100644 --- a/lib/Keyword.pm +++ b/lib/Keyword.pm @@ -118,13 +118,10 @@ sub action_parser { substr($l, $parser->offset+1, 0) = $code; $parser->line($l); - no strict 'refs'; - no warnings 'redefine'; - warn $name; - *{$KW_MODULE.'::action'} = sub (&) { - warn $name; + $parser->shadow("$KW_MODULE\::action", sub (&) { + no strict 'refs'; *{$KW_MODULE."::action_$name"} = shift; - }; + }); } sub eos { @@ -304,22 +301,48 @@ Each identifier in a keywords prototype represents a parse routine and its assoc =head2 Parse routines -There 3 built-in parse routines: +There are three built-in parse routines: ident - matches an identifier proto - matches anything surrounded by parenthese block - matches the start of a block +Its possible to write your own with the following syntax: + + parse something($parser) { + if (my $len = $parser->scan_word(1)) { + my $l = $parser->line; + my $ident = substr($l, $parser->offset, $len); + substr($l, $parser->offset, $len) = ''; + $parser->line($l); + return $ident if $ident =~ /^[a-z]{1}\w+$/i; + } + } + +=head3 Blocks + +A block is different from a standard parse routine as it returns an object. + +This object contains several routines for injecting code into the block: + + $block->name($identifier); + $block->code("some(); code();"); # no newlines please + $block->terminate; # adds semicolon + =cut =head2 Actions -Actions get passed whatever its parse routine matches and return directly +Actions get passed whatever the associated parse routine 'matches'. + +There job is to convert whatever is matched to injectable perl code. =cut =head1 CODE +http://github.com/robinedwards/Keyword + git@github.com:robinedwards/Keyword.git =head1 AUTHOR diff --git a/lib/Keyword/Parser.pm b/lib/Keyword/Parser.pm index 09e1a3c..8e6559f 100644 --- a/lib/Keyword/Parser.pm +++ b/lib/Keyword/Parser.pm @@ -71,8 +71,9 @@ sub shadow { #set name as global for import; no strict 'refs'; - ${$self->package."::__block_name"} = $name; + ${$self->package."::__block_name"} = $name; + unless ($sub) { if($name) { $sub = sub (&) { diff --git a/t/02-syntax.t b/t/02-syntax.t index f9b7c33..9c695bf 100644 --- a/t/02-syntax.t +++ b/t/02-syntax.t @@ -18,6 +18,7 @@ action thing ($match) { } parse_thing(); +action_thing(); ok 1;