X-Git-Url: http://git.vpit.fr/?p=perl%2Fmodules%2FSub-Prototype-Util.git;a=blobdiff_plain;f=lib%2FSub%2FPrototype%2FUtil.pm;h=a92647f6a4d224985ff72ac11dea3fd75ed980ca;hp=b78f396adcfa24ff67492fe13d81a281d70b91a1;hb=39b18d4f8f133cb03d47a486464c5c96cb148d5f;hpb=f88d790683ee83e8225ee6b0bcb783260347c814 diff --git a/lib/Sub/Prototype/Util.pm b/lib/Sub/Prototype/Util.pm index b78f396..a92647f 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 @@ -72,19 +72,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 +101,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,13 +126,13 @@ 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 @@ -202,7 +206,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; @@ -238,7 +243,7 @@ If you plan to recall several times, consider using L instead. =cut sub recall { - my $wrap = eval { wrap shift, compile => 1 }; + my $wrap = eval { wrap shift }; croak $@ if $@; return $wrap->(@_); }