From: Robin Edwards Date: Mon, 14 Dec 2009 12:58:16 +0000 (+0000) Subject: added after block hook, renamed code method X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=6aaa87d90edd16486eff3a73927bbd937545c6a6;p=p5sagit%2FDevel-Declare-Keyword.git added after block hook, renamed code method --- diff --git a/README b/README index 49a7a32..289a506 100644 --- a/README +++ b/README @@ -23,7 +23,8 @@ SYNTAX #parse an action routines called for each entry in proto keyword method (ident?, proto?, custom, block) { $block->name($ident); # name the block - $block->code($proto); # inject proto code (at begining) + $block->inject_begin($proto); # inject at begin of block + $block->inject_after("warn '$ident() finished';"); $block->terminate; # add semi colon } diff --git a/examples/Methods.pm b/examples/Methods.pm index ac3d747..4db63f4 100644 --- a/examples/Methods.pm +++ b/examples/Methods.pm @@ -5,7 +5,8 @@ use Data::Dumper; keyword method (ident?, proto?, block) { $block->name($ident); - $block->code($proto); + $block->inject_begin($proto); + $block->inject_after("warn 'HELLO!';"); $block->terminate; } diff --git a/lib/Keyword.pm b/lib/Keyword.pm index f58d9a9..2895889 100644 --- a/lib/Keyword.pm +++ b/lib/Keyword.pm @@ -252,7 +252,8 @@ Keyword - an easy way to declare keyword with custom parsers keyword method (ident?, proto?, block) { $block->name($ident); # assign the block to subroutine - $block->code($proto); # inject proto code + $block->inject_begin($proto); # inject proto code + $block->inject_after("warn '$ident() finished';"); $block->terminate; # add semi colon } diff --git a/lib/Keyword/Parse/Block.pm b/lib/Keyword/Parse/Block.pm index 1e3acc1..67633ea 100644 --- a/lib/Keyword/Parse/Block.pm +++ b/lib/Keyword/Parse/Block.pm @@ -2,6 +2,7 @@ package Keyword::Parse::Block; use strict; use warnings; use B::Hooks::EndOfScope; +use Data::Dumper; #doesnt actualy 'parse' a block, just detects the start. @@ -19,10 +20,19 @@ sub match { } } -#inject code -sub code { + +sub inject_before { + my ($self,$code) = @_; +} + +sub code { inject_begin(@_); }; + +#inject code into start of block +sub inject_begin { my ($self, $code) = @_; + $code =~ s/\n/\ /g; + $self->{eos} =$self->{parser}->package."::_". $self->{name}."_inject_scope"; @@ -39,6 +49,14 @@ sub code { $self->{parser}->line($l); } +#injects code after the end of the block +sub inject_after { + my ($self, $code) = @_; + $code =~ s/\n/\ /g; + $self->{inject_after} = $code; + +} + sub name { my ($self, $name) = @_; no strict 'refs'; @@ -58,7 +76,8 @@ sub terminate { on_scope_end { my $l = $self->{parser}->line; my $loffset = $self->{parser}->line_offset; - substr($l, $loffset, 0) = ';'; + substr($l, $loffset, 0) = '; '. + ($self->{inject_after}?$self->{inject_after}:""); $self->{parser}->line($l); }; };