use warnings;
use 5.008001;
-our $VERSION = 0.001000;
+our $VERSION = '0.001003';
# mirrored in Declare.xs as DD_HANDLE_*
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);
+use vars qw(%declarators %declarator_handlers @ISA);
use base qw(DynaLoader);
+use Scalar::Util 'set_prototype';
bootstrap Devel::Declare;
+@ISA = ();
+
sub import {
my ($class, %args) = @_;
my $target = caller;
if (@_ == 1) { # "use Devel::Declare;"
no strict 'refs';
- foreach my $name (qw(DECLARE_NAME DECLARE_PROTO)) {
- *{"${target}::${name}"} = *{"${name}"};
+ foreach my $name (qw(NAME PROTO NONE PACKAGE)) {
+ *{"${target}::DECLARE_${name}"} = *{"DECLARE_${name}"};
}
} else {
$class->setup_for($target => \%args);
teardown();
}
-my $temp_pack;
my $temp_name;
my $temp_save;
sub init_declare {
- my ($pack, $use, $name, $proto) = @_;
- my ($name_h, $XX_h) = $declarator_handlers{$pack}{$use}->(
- $pack, $use, $name, $proto
- );
- ($temp_pack, $temp_name, $temp_save) = ($pack, [], []);
+ my ($usepack, $use, $inpack, $name, $proto) = @_;
+ my ($name_h, $XX_h, $extra_code)
+ = $declarator_handlers{$usepack}{$use}->(
+ $usepack, $use, $inpack, $name, $proto, defined(wantarray)
+ );
+ ($temp_name, $temp_save) = ([], []);
if ($name) {
+ $name = "${inpack}::${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, "${inpack}::X");
no strict 'refs';
- push(@$temp_save, \&{"${pack}::X"});
+ push(@$temp_save, \&{"${inpack}::X"});
no warnings 'redefine';
no warnings 'prototype';
- *{"${pack}::X"} = $XX_h;
+ *{"${inpack}::X"} = $XX_h;
+ }
+ if (defined wantarray) {
+ return $extra_code || '0;';
+ } else {
+ return;
}
}
sub done_declare {
no strict 'refs';
- my $name = pop(@{$temp_name||[]});
+ my $name = shift(@{$temp_name||[]});
die "done_declare called with no temp_name stack" unless defined($name);
- my $saved = pop(@$temp_save);
+ my $saved = shift(@$temp_save);
+ $name =~ s/(.*):://;
+ my $temp_pack = $1;
delete ${"${temp_pack}::"}{$name};
if ($saved) {
no warnings 'prototype';
}
}
+sub build_sub_installer {
+ my ($class, $pack, $name, $proto) = @_;
+ return eval "
+ package ${pack};
+ my \$body;
+ sub ${name} (${proto}) :lvalue {\n"
+ .'my $ret = $body->(@_);
+ return $ret;
+ };
+ sub { ($body) = @_; };';
+}
+
+sub setup_declarators {
+ my ($class, $pack, $to_setup) = @_;
+ die "${class}->setup_declarators(\$pack, \\\%to_setup)"
+ unless defined($pack) && ref($to_setup) eq 'HASH';
+ my %setup_for_args;
+ foreach my $name (keys %$to_setup) {
+ my $info = $to_setup->{$name};
+ my $flags = $info->{flags} || DECLARE_NAME;
+ my $run = $info->{run};
+ my $compile = $info->{compile};
+ my $proto = $info->{proto} || '&';
+ my $sub_proto = $proto;
+ # make all args optional to enable lvalue for DECLARE_NONE
+ $sub_proto =~ s/;//; $sub_proto = ';'.$sub_proto;
+ #my $installer = $class->build_sub_installer($pack, $name, $proto);
+ my $installer = $class->build_sub_installer($pack, $name, '@');
+ my $proto_maker = eval q!
+ sub {
+ my $body = shift;
+ sub (!.$sub_proto.q!) {
+ $body->(@_);
+ };
+ };
+ !;
+ $installer->(sub :lvalue {
+ if (@_) {
+ if (ref $_[0] eq 'HASH') {
+ shift;
+ my $r = $run->(undef, undef, @_);
+ return $r;
+ } else {
+ return $_[1];
+ }
+ }
+ return my $sv;
+ });
+ $setup_for_args{$name} = [
+ $flags,
+ sub {
+ my ($usepack, $use, $inpack, $name, $proto) = @_;
+ my $extra_code = $compile->($name, $proto);
+ my $main_handler = $proto_maker->(sub {
+ ("DONE", $run->($name, $proto, @_));
+ });
+ my ($name_h, $XX);
+ if (defined $proto) {
+ $name_h = sub :lvalue { return my $sv; };
+ $XX = $main_handler;
+ } elsif (defined $name && length $name) {
+ $name_h = $main_handler;
+ } else {
+ $extra_code ||= '';
+ $extra_code = '}, sub {'.$extra_code;
+ }
+ return ($name_h, $XX, $extra_code);
+ }
+ ];
+ }
+ $class->setup_for($pack, \%setup_for_args);
+}
+
+sub install_declarator {
+ my ($class, $target_pack, $target_name, $flags, $filter, $handler) = @_;
+ $class->setup_declarators($target_pack, {
+ $target_name => {
+ flags => $flags,
+ compile => $filter,
+ run => $handler,
+ }
+ });
+}
+
=head1 NAME
Devel::Declare -
=head1 SYNOPSIS
+Look at the tests. This module is currently on CPAN to ease smoke testing
+and allow early adopters who've been involved in the design to experiment
+with it.
+
=head1 DESCRIPTION
=head2 import
=head1 AUTHOR
-Matt S Trout - <mst@shadowcatsystems.co.uk>
+Matt S Trout - <mst@shadowcat.co.uk>
-Company: http://www.shadowcatsystems.co.uk/
+Company: http://www.shadowcat.co.uk/
Blog: http://chainsawblues.vox.com/
=head1 LICENSE