From: Rhesa Rozendaal Date: Sat, 18 Oct 2008 13:40:29 +0000 (+0000) Subject: branches/context_object: reformatted with 2-space indent, and "if (" X-Git-Tag: 0.005000~47^2~2 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=5b27c9b27fb76cf9174e86a3ad98db42504023b2;p=p5sagit%2FDevel-Declare.git branches/context_object: reformatted with 2-space indent, and "if (" --- diff --git a/lib/Devel/Declare/Context/Simple.pm b/lib/Devel/Declare/Context/Simple.pm index 268514c..548aa45 100644 --- a/lib/Devel/Declare/Context/Simple.pm +++ b/lib/Devel/Declare/Context/Simple.pm @@ -6,95 +6,93 @@ use strict; use warnings; sub new { - my $class = shift; - bless {@_}, $class; + my $class = shift; + bless {@_}, $class; } sub init { - my $ctx = shift; - @{$ctx}{ qw(Declarator Offset) } = @_; - $ctx; + my $self = shift; + @{$self}{ qw(Declarator Offset) } = @_; + $self; } sub offset : lvalue { shift->{Offset}; } sub declarator { shift->{Declarator} } sub skip_declarator { - my $ctx = shift; - $ctx->offset += Devel::Declare::toke_move_past_token( $ctx->offset ); + my $self = shift; + $self->offset += Devel::Declare::toke_move_past_token( $self->offset ); } sub skipspace { - my $ctx = shift; - $ctx->offset += Devel::Declare::toke_skipspace( $ctx->offset ); + my $self = shift; + $self->offset += Devel::Declare::toke_skipspace( $self->offset ); } sub strip_name { - my $ctx = shift; - $ctx->skipspace; - if( my $len = Devel::Declare::toke_scan_word( $ctx->offset, 1 ) ) { - my $linestr = Devel::Declare::get_linestr(); - my $name = substr( $linestr, $ctx->offset, $len ); - substr( $linestr, $ctx->offset, $len ) = ''; - Devel::Declare::set_linestr($linestr); - return $name; - } - return; + my $self = shift; + $self->skipspace; + if (my $len = Devel::Declare::toke_scan_word( $self->offset, 1 )) { + my $linestr = Devel::Declare::get_linestr(); + my $name = substr( $linestr, $self->offset, $len ); + substr( $linestr, $self->offset, $len ) = ''; + Devel::Declare::set_linestr($linestr); + return $name; + } + return; } sub strip_proto { - my $ctx = shift; - $ctx->skipspace; - - my $linestr = Devel::Declare::get_linestr(); - if( substr( $linestr, $ctx->offset, 1 ) eq '(' ) { - my $length = Devel::Declare::toke_scan_str( $ctx->offset ); - my $proto = Devel::Declare::get_lex_stuff(); - Devel::Declare::clear_lex_stuff(); - $linestr = Devel::Declare::get_linestr(); - substr( $linestr, $ctx->offset, $length ) = ''; - Devel::Declare::set_linestr($linestr); - return $proto; - } - return; + my $self = shift; + $self->skipspace; + + my $linestr = Devel::Declare::get_linestr(); + if (substr( $linestr, $self->offset, 1 ) eq '(') { + my $length = Devel::Declare::toke_scan_str( $self->offset ); + my $proto = Devel::Declare::get_lex_stuff(); + Devel::Declare::clear_lex_stuff(); + $linestr = Devel::Declare::get_linestr(); + substr( $linestr, $self->offset, $length ) = ''; + Devel::Declare::set_linestr($linestr); + return $proto; + } + return; } sub get_curstash_name { - return Devel::Declare::get_curstash_name; + return Devel::Declare::get_curstash_name; } sub shadow { - my $ctx = shift; - my $pack = $ctx->get_curstash_name; - Devel::Declare::shadow_sub( $pack . '::' . $ctx->declarator, $_[0] ); + my $self = shift; + my $pack = $self->get_curstash_name; + Devel::Declare::shadow_sub( $pack . '::' . $self->declarator, $_[0] ); } sub inject_if_block { - my $ctx = shift; - my $inject = shift; - $ctx->skipspace; - my $linestr = Devel::Declare::get_linestr; - if( substr( $linestr, $ctx->offset, 1 ) eq '{' ) { - substr( $linestr, $ctx->offset + 1, 0 ) = $inject; - Devel::Declare::set_linestr($linestr); - } + my $self = shift; + my $inject = shift; + $self->skipspace; + my $linestr = Devel::Declare::get_linestr; + if (substr( $linestr, $self->offset, 1 ) eq '{') { + substr( $linestr, $self->offset + 1, 0 ) = $inject; + Devel::Declare::set_linestr($linestr); + } } sub scope_injector_call { - return ' BEGIN { ' . __PACKAGE__ . '::inject_scope }; '; + return ' BEGIN { ' . __PACKAGE__ . '::inject_scope }; '; } sub inject_scope { - my $ctx = shift; - $^H |= 0x120000; - $^H{DD_METHODHANDLERS} = Scope::Guard->new( - sub { - my $linestr = Devel::Declare::get_linestr; - my $offset = Devel::Declare::get_linestr_offset; - substr( $linestr, $offset, 0 ) = ';'; - Devel::Declare::set_linestr($linestr); - } - ); + my $self = shift; + $^H |= 0x120000; + $^H{DD_METHODHANDLERS} = Scope::Guard->new(sub { + my $linestr = Devel::Declare::get_linestr; + my $offset = Devel::Declare::get_linestr_offset; + substr( $linestr, $offset, 0 ) = ';'; + Devel::Declare::set_linestr($linestr); + }); } 1; diff --git a/lib/Devel/Declare/MethodInstaller/Simple.pm b/lib/Devel/Declare/MethodInstaller/Simple.pm index 0df21d1..4167116 100644 --- a/lib/Devel/Declare/MethodInstaller/Simple.pm +++ b/lib/Devel/Declare/MethodInstaller/Simple.pm @@ -8,54 +8,53 @@ use strict; use warnings; sub install_methodhandler { - my $class = shift; - my %args = @_; - { - no strict 'refs'; - *{$args{into}.'::'.$args{name}} = sub (&) {}; - } - - my $ctx = $class->new( %args ); - Devel::Declare->setup_for( - $args{into}, - { $args{name} => { const => sub { $ctx->parser(@_) } } } - ); - + my $class = shift; + my %args = @_; + { + no strict 'refs'; + *{$args{into}.'::'.$args{name}} = sub (&) {}; + } + + my $ctx = $class->new(%args); + Devel::Declare->setup_for( + $args{into}, + { $args{name} => { const => sub { $ctx->parser(@_) } } } + ); } sub parser { - my $ctx = shift; - $ctx->init(@_); - - $ctx->skip_declarator; - my $name = $ctx->strip_name; - my $proto = $ctx->strip_proto; - my @decl = $ctx->parse_proto($proto); - my $inject = $ctx->inject_parsed_proto(@decl); - if( defined $name ) { - $inject = $ctx->scope_injector_call() . $inject; - } - $ctx->inject_if_block($inject); - if( defined $name ) { - my $pkg = $ctx->get_curstash_name; - $name = join( '::', $pkg, $name ) - unless( $name =~ /::/ ); - $ctx->shadow( sub (&) { - my $code = shift; - # So caller() gets the subroutine name - no strict 'refs'; - *{$name} = subname $name => $code; - }); - } else { - $ctx->shadow(sub (&) { shift }); - } + my $self = shift; + $self->init(@_); + + $self->skip_declarator; + my $name = $self->strip_name; + my $proto = $self->strip_proto; + my @decl = $self->parse_proto($proto); + my $inject = $self->inject_parsed_proto(@decl); + if (defined $name) { + $inject = $self->scope_injector_call() . $inject; + } + $self->inject_if_block($inject); + if (defined $name) { + my $pkg = $self->get_curstash_name; + $name = join( '::', $pkg, $name ) + unless( $name =~ /::/ ); + $self->shadow( sub (&) { + my $code = shift; + # So caller() gets the subroutine name + no strict 'refs'; + *{$name} = subname $name => $code; + }); + } else { + $self->shadow(sub (&) { shift }); + } } + sub parse_proto { } + sub inject_parsed_proto { - my $ctx = shift; - shift; + return $_[1]; } - 1; diff --git a/t/methinstaller-simple.t b/t/methinstaller-simple.t index 37ba354..11f4e47 100644 --- a/t/methinstaller-simple.t +++ b/t/methinstaller-simple.t @@ -34,7 +34,7 @@ my ($test_method1, $test_method2, @test_list); MethodHandlers->install_methodhandler( name => 'method', into => __PACKAGE__, - ); + ); } method new {