use strict;
use warnings;
-use Carp qw/croak/;
-use Scalar::Util qw/reftype/;
+use Carp qw<croak>;
+use Scalar::Util qw<reftype>;
=head1 NAME
=head1 VERSION
-Version 0.09
+Version 0.10
=cut
-use vars qw/$VERSION/;
+use vars qw<$VERSION>;
-$VERSION = '0.09';
+$VERSION = '0.10';
=head1 SYNOPSIS
- use Sub::Prototype::Util qw/flatten wrap recall/;
+ use Sub::Prototype::Util qw<flatten wrap recall>;
- my @a = qw/a b c/;
+ my @a = qw<a b c>;
my @args = ( \@a, 1, { d => 2 }, undef, 3 );
- my @flat = flatten '\@$;$', @args; # ('a', 'b', 'c', 1, { d => 2 })
- recall 'CORE::push', @args; # @a contains 'a', 'b', 'c', 1, { d => 2 }, undef, 3
+ my @flat = flatten '\@$;$', @args;
+ # @flat contains now ('a', 'b', 'c', 1, { d => 2 })
+
+ my $res = recall 'CORE::push', @args;
+ # @a contains now 'a', 'b', 'c', 1, { d => 2 }, undef, 3
+ # and $res is 7
+
my $splice = wrap 'CORE::splice';
- my @b = $splice->(\@a, 4, 2); # @a is now ('a', 'b', 'c', 1, 3) and @b is ({ d => 2 }, undef)
+ my @b = $splice->(\@a, 4, 2);
+ # @a contains now ('a', 'b', 'c', 1, 3)
+ # and @b is ({ d => 2 }, undef)
=head1 DESCRIPTION
=cut
-my %sigils = qw/SCALAR $ ARRAY @ HASH % GLOB * CODE &/;
+my %sigils = qw<SCALAR $ ARRAY @ HASH % GLOB * CODE &>;
my %reftypes = reverse %sigils;
sub _check_ref {
- my ($a, $p) = @_;
- my $r;
- if (!defined $a || !defined($r = reftype $a)) { # not defined or plain scalar
- croak 'Got ' . ((defined $a) ? 'a plain scalar' : 'undef')
- . ' where a reference was expected';
+ my ($arg, $sigil) = @_;
+
+ my $reftype;
+ if (not defined $arg or not defined($reftype = reftype $arg)) {
+ # not defined or plain scalar
+ my $that = (defined $arg) ? 'a plain scalar' : 'undef';
+ croak "Got $that where a reference was expected";
}
- croak 'Unexpected ' . $r . ' reference' unless exists $sigils{$r}
- and $p =~ /\Q$sigils{$r}\E/;
- return $r;
+
+ croak "Unexpected $reftype reference" unless exists $sigils{$reftype}
+ and $sigil =~ /\Q$sigils{$reftype}\E/;
+
+ $reftype;
}
sub _clean_msg {
$msg;
}
-=head2 C<flatten $proto, @args>
+=head2 C<flatten>
+
+ my @flattened = flatten($proto, @args);
Flattens the array C<@args> according to the prototype C<$proto>.
When C<@args> is what C<@_> is after calling a subroutine with prototype C<$proto>, C<flatten> returns the list of what C<@_> would have been if there were no prototype.
return @args;
}
-=head2 C<wrap $name, %opts>
+=head2 C<wrap>
+
+ my $wrapper = wrap($name, %opts);
+ my $wrapper = wrap({ $name => $proto }, %opts);
Generates a wrapper that calls the function C<$name> with a prototyped argument list.
That is, the wrapper's arguments should be what C<@_> is when you define a subroutine with the same prototype as C<$name>.
my $push = wrap { 'CORE::push' => '\@$' }; # only pushes 1 arg
-Others arguments are seen as key / value pairs that are meant to tune the code generated by L</wrap>.
+The remaining arguments C<%opts> are treated as key / value pairs that are meant to tune the code generated by L</wrap>.
Valid keys are :
=over 4
For example, this allows you to recall into C<CORE::grep> and C<CORE::map> by using the C<\&@> prototype :
my $grep = wrap { 'CORE::grep' => '\&@' };
- sub mygrep (&@) { $grep->(@_) } # the prototypes are intentionally different
+ # the prototypes are intentionally different
+ sub mygrep (&@) { $grep->(@_) }
=cut
return $call;
}
-=head2 C<recall $name, @args>
+=head2 C<recall>
+
+ my @res = recall($name, @args);
+ my @res = recall({ $name => $proto }, @args);
Calls the function C<$name> with the prototyped argument list C<@args>.
That is, C<@args> should be what C<@_> is when you call a subroutine with C<$name> as prototype.
=cut
-sub recall {
- my $name = shift;
+sub recall;
- my ($wrap, $err);
- {
- local $@;
- $wrap = eval { wrap $name };
- $err = $@;
- }
- croak _clean_msg $err if $err;
+BEGIN {
+ my $safe_wrap = sub {
+ my $name = shift;
- goto $wrap;
+ my ($wrap, $err);
+ {
+ local $@;
+ $wrap = eval { wrap $name };
+ $err = $@;
+ }
+
+ $wrap, $err;
+ };
+
+ if ("$]" == 5.008) {
+ # goto tends to crash a lot on perl 5.8.0
+ *recall = sub {
+ my ($wrap, $err) = $safe_wrap->(shift);
+ croak _clean_msg $err if $err;
+ $wrap->(@_)
+ }
+ } else {
+ *recall = sub {
+ my ($wrap, $err) = $safe_wrap->(shift);
+ croak _clean_msg $err if $err;
+ goto $wrap;
+ }
+ }
}
=head1 EXPORT
=cut
-use base qw/Exporter/;
+use base qw<Exporter>;
-use vars qw/@EXPORT @EXPORT_OK %EXPORT_TAGS/;
+use vars qw<@EXPORT @EXPORT_OK %EXPORT_TAGS>;
@EXPORT = ();
%EXPORT_TAGS = (
- 'funcs' => [ qw/flatten wrap recall/ ]
+ 'funcs' => [ qw<flatten wrap recall> ]
);
@EXPORT_OK = map { @$_ } values %EXPORT_TAGS;
$EXPORT_TAGS{'all'} = [ @EXPORT_OK ];