change begin accessor to code removed end accessor
Robin Edwards [Fri, 11 Dec 2009 13:17:56 +0000 (13:17 +0000)]
lib/Keyword/Parse/Block.pm
lib/Keyword/Parser.pm

index b4c8114..9106897 100644 (file)
@@ -1,6 +1,9 @@
 package Keyword::Parse::Block;
 use strict;
 use warnings;
+use B::Hooks::EndOfScope;
+
+#doesnt actualy 'parse' a block, just detects the start.
 
 sub new {
        my ($parser) = @_;
@@ -16,34 +19,48 @@ sub match {
        }
 }
 
-#add end of scope hook
 #inject code
-sub begin {
+sub code {
        my ($self, $code) = @_;
 
+       $self->{eos} =$self->{parser}->package."::_".
+               $self->{name}."_inject_scope";
+
+       #add end of scope hook
+       $code = " BEGIN { $self->{eos}()}; $code";
+
+       no strict 'refs';
+       *{$self->{eos}} = sub {};
+
        my $l = $self->{parser}->line;
        substr($l, $self->{parser}->offset+1, 0) = $code;
        $self->{parser}->skip_ws;
+       #added end of scope hook
        $self->{parser}->line($l);
 }
 
 sub name {
        my ($self, $name) = @_;
        no strict 'refs';
+       $self->{name} = $name;
        $self->{parser}->shadow($name);
 }
 
+
+#set end of scope code
+# !! B:H:EOS won't allow you to inject code into the block
 sub terminate {
-       my $self = shift
-               # remove need for semi colon
-               #*{$module_user."::inject_scope"} = sub {
-               #               on_scope_end {
-               #                       my $l = $parser->line;
-               #                       my $loffset = $parser->line_offset;
-               #                       substr($l, $loffset, 0) = ';';
-               #                       $parser->line($l);
-               #       };
-               #}
+       my ($self) = @_;
+       no strict 'refs';
+       no warnings 'redefine';
+       *{$self->{eos}} = sub {
+               on_scope_end {
+                       my $l = $self->{parser}->line;
+                       my $loffset = $self->{parser}->line_offset;
+                       substr($l, $loffset, 0) = ';';
+                       $self->{parser}->line($l);
+               };
+       };
 }
 
 1;
index 6d5cdbb..b8c1328 100644 (file)
@@ -61,6 +61,8 @@ sub package {
 }
 
 sub line_offset {
+       my ($self, $os) = @_;
+        Devel::Declare::set_linestr_offset($os) if $os;
        return Devel::Declare::get_linestr_offset;
 }