From: Matt S Trout Date: Sun, 1 Jul 2007 18:42:26 +0000 (+0000) Subject: package handling X-Git-Tag: 0.005000~136 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=15d0d01400f87e62e06cc5b9ab302371a6f8149f;p=p5sagit%2FDevel-Declare.git package handling --- diff --git a/Declare.xs b/Declare.xs index 15ff50c..c1407a0 100644 --- a/Declare.xs +++ b/Declare.xs @@ -15,6 +15,7 @@ #define DD_HANDLE_NAME 1 #define DD_HANDLE_PROTO 2 +#define DD_HANDLE_PACKAGE 8 #ifdef DD_DEBUG #define DD_DEBUG_S printf("Buffer: %s\n", s); @@ -135,9 +136,9 @@ STATIC OP *dd_ck_rv2cv(pTHX_ OP *o) { DD_DEBUG_S - /* 0 in arg 4 is allow_package - not trying that yet :) */ + /* arg 4 is allow_package */ - s = scan_word(s, tmpbuf, sizeof tmpbuf, 0, &len); + s = scan_word(s, tmpbuf, sizeof tmpbuf, dd_flags & DD_HANDLE_PACKAGE, &len); DD_DEBUG_S diff --git a/lib/Devel/Declare.pm b/lib/Devel/Declare.pm index 4f4a473..4d4e3c9 100644 --- a/lib/Devel/Declare.pm +++ b/lib/Devel/Declare.pm @@ -11,8 +11,9 @@ our $VERSION = 0.001000; use constant DECLARE_NAME => 1; use constant DECLARE_PROTO => 2; use constant DECLARE_NONE => 4; +use constant DECLARE_PACKAGE => 8+1; # name implicit -use vars qw(%declarators %declarator_handlers @next_pad_inject); +use vars qw(%declarators %declarator_handlers); use base qw(DynaLoader); bootstrap Devel::Declare; @@ -22,7 +23,7 @@ sub import { my $target = caller; if (@_ == 1) { # "use Devel::Declare;" no strict 'refs'; - foreach my $name (qw(NAME PROTO NONE)) { + foreach my $name (qw(NAME PROTO NONE PACKAGE)) { *{"${target}::DECLARE_${name}"} = *{"DECLARE_${name}"}; } } else { @@ -62,7 +63,6 @@ sub teardown_for { teardown(); } -my $temp_pack; my $temp_name; my $temp_save; @@ -72,17 +72,18 @@ sub init_declare { = $declarator_handlers{$pack}{$use}->( $pack, $use, $name, $proto, defined(wantarray) ); - ($temp_pack, $temp_name, $temp_save) = ($pack, [], []); + ($temp_name, $temp_save) = ([], []); if ($name) { + $name = "${pack}::${name}" unless $name =~ /::/; push(@$temp_name, $name); no strict 'refs'; - push(@$temp_save, \&{"${pack}::${name}"}); + push(@$temp_save, \&{$name}); no warnings 'redefine'; no warnings 'prototype'; - *{"${pack}::${name}"} = $name_h; + *{$name} = $name_h; } if ($XX_h) { - push(@$temp_name, 'X'); + push(@$temp_name, "${pack}::X"); no strict 'refs'; push(@$temp_save, \&{"${pack}::X"}); no warnings 'redefine'; @@ -101,6 +102,8 @@ sub done_declare { my $name = pop(@{$temp_name||[]}); die "done_declare called with no temp_name stack" unless defined($name); my $saved = pop(@$temp_save); + $name =~ s/(.*):://; + my $temp_pack = $1; delete ${"${temp_pack}::"}{$name}; if ($saved) { no warnings 'prototype'; @@ -108,10 +111,6 @@ sub done_declare { } } -sub inject_into_next_pad { - shift; @next_pad_inject = @_; -} - =head1 NAME Devel::Declare - diff --git a/t/pack.t b/t/pack.t new file mode 100644 index 0000000..0222dbd --- /dev/null +++ b/t/pack.t @@ -0,0 +1,22 @@ +use strict; +use warnings; +use Test::More 'no_plan'; + +sub class { $_[0]->(); } + +sub handle_class { + my ($pack, $use, $name, $proto, $is_block) = @_; + return (sub (&) { shift; }, undef, "package ${name};"); +} + +use Devel::Declare; +use Devel::Declare 'class' => [ DECLARE_PACKAGE, \&handle_class ]; + +my $packname; + +class Foo::Bar { + $packname = __PACKAGE__; +}; + +is($packname, 'Foo::Bar', 'Package saved ok'); +is(__PACKAGE__, 'main', 'Package scoped correctly');