X-Git-Url: http://git.vpit.fr/?a=blobdiff_plain;f=lib%2FSub%2FPrototype%2FUtil.pm;h=20525ece0971b354170d655b1ce89b344100467c;hb=92b328c090598b186ee6dd5168ca4b5047a834c9;hp=b78f396adcfa24ff67492fe13d81a281d70b91a1;hpb=f88d790683ee83e8225ee6b0bcb783260347c814;p=perl%2Fmodules%2FSub-Prototype-Util.git diff --git a/lib/Sub/Prototype/Util.pm b/lib/Sub/Prototype/Util.pm index b78f396..20525ec 100644 --- a/lib/Sub/Prototype/Util.pm +++ b/lib/Sub/Prototype/Util.pm @@ -29,7 +29,7 @@ $VERSION = '0.08'; 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 $splice = wrap 'CORE::splice', compile => 1; + 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) =head1 DESCRIPTION @@ -57,6 +57,12 @@ sub _check_ref { return $r; } +sub _clean_msg { + my ($msg) = @_; + $msg =~ s/(?:\s+called)?\s+at\s+.*$//s; + return $msg; +} + =head2 C 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 returns the list of what C<@_> would have been if there were no prototype. @@ -72,19 +78,23 @@ sub flatten { if ($1) { my $a = shift; my $r = _check_ref $a, $p; - my %deref = ( - SCALAR => sub { push @args, $$a }, - ARRAY => sub { push @args, @$a }, - HASH => sub { push @args, %$a }, - GLOB => sub { push @args, *$a }, - CODE => sub { push @args, &$a } - ); - $deref{$r}->(); + push @args, $r eq 'SCALAR' + ? $$a + : ($r eq 'ARRAY' + ? @$a + : ($r eq 'HASH' + ? %$a + : ($r eq 'GLOB' + ? *$a + : &$a # _check_ref ensures this must be a code ref + ) + ) + ); } elsif ($p =~ /[\@\%]/) { push @args, @_; last; - } elsif ($p eq '_' && @_ == 0) { - push @args, $_; + } elsif ($p eq '_') { + shift; # without prototype, this argument wouldn't have been passed } else { push @args, shift; } @@ -97,12 +107,12 @@ sub flatten { 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 $a = [ 0 .. 2 ]; - my $push = wrap 'CORE::push', compile => 1; + my $push = wrap 'CORE::push'; $push->($a, 3, 4); # returns 3 + 2 = 5 and $a now contains 0 .. 4 You can force the use of a specific prototype. In this case, C<$name> must be a hash reference that holds exactly one key / value pair, the key being the function name and the value the prototpye that should be used to call it. - my $push = wrap { 'CORE::push' => '\@$' }, compile => 1; # only pushes 1 arg + 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. Valid keys are : @@ -122,22 +132,21 @@ Encloses the code into a C block. Default is true. =item C<< compile => $bool >> -Makes L compile the code generated and return the resulting code reference. Implies C<< sub => 1 >>. Be careful that in this case C must be a fully qualified function name. Defaults to false. +Makes L compile the code generated and return the resulting code reference. Be careful that in this case C must be a fully qualified function name. Defaults to true, but turned off when C is false. =back For example, this allows you to recall into C and C by using the C<\&@> prototype : - my $grep = wrap { 'CORE::grep' => '\&@' }, compile => 1; + my $grep = wrap { 'CORE::grep' => '\&@' }; sub mygrep (&@) { $grep->(@_) } # the prototypes are intentionally different =cut sub _wrap { my ($name, $proto, $i, $args, $cr, $opts) = @_; - if ($proto =~ /(\\?)(\[[^\]]+\]|[^\];])(.*)/g) { + while ($proto =~ s/(\\?)(\[[^\]]+\]|[^\];])//) { my ($ref, $p) = ($1, $2); - $proto = $3; $p = $1 if $p =~ /^\[([^\]]+)\]/; my $cur = '$_[' . $i . ']'; if ($ref) { @@ -172,11 +181,10 @@ sub _wrap { } else { $args .= $cur . ', '; } - return _wrap($name, $proto, ($i + 1), $args, $cr, $opts); - } else { - $args =~ s/,\s*$//; - return $name . '(' . $args . ')'; + ++$i; } + $args =~ s/,\s*$//; + return $name . '(' . $args . ')'; } sub _check_name { @@ -202,7 +210,8 @@ sub wrap { croak 'Optional arguments must be passed as key => value pairs' if @_ % 2; my %opts = @_; $opts{ref} ||= 'ref'; - $opts{sub} = 1 if not exists $opts{sub} or $opts{compile}; + $opts{sub} = 1 if not defined $opts{sub}; + $opts{compile} = 1 if not defined $opts{compile} and $opts{sub}; $opts{wrong_ref} = 'undef' if not defined $opts{wrong_ref}; my @cr; my $call; @@ -220,7 +229,7 @@ sub wrap { $call = 'sub ' . $call if $opts{sub}; if ($opts{compile}) { $call = eval $call; - croak $@ if $@; + croak _clean_msg $@ if $@; } return $call; } @@ -238,8 +247,8 @@ If you plan to recall several times, consider using L instead. =cut sub recall { - my $wrap = eval { wrap shift, compile => 1 }; - croak $@ if $@; + my $wrap = eval { wrap shift }; + croak _clean_msg $@ if $@; return $wrap->(@_); }