X-Git-Url: http://git.vpit.fr/?p=perl%2Fmodules%2FSub-Prototype-Util.git;a=blobdiff_plain;f=lib%2FSub%2FPrototype%2FUtil.pm;h=91520b823361f417e9d715e1d3f7f63d4888a04e;hp=b78f396adcfa24ff67492fe13d81a281d70b91a1;hb=4e977a0b1db65e44cf4c6184792208a7930c34f4;hpb=118a833f999d3ed851b0f2aa28f10f13bd888046 diff --git a/lib/Sub/Prototype/Util.pm b/lib/Sub/Prototype/Util.pm index b78f396..91520b8 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 @@ -97,12 +97,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 +122,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 +202,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 +239,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->(@_); }