]> git.vpit.fr Git - perl/modules/Sub-Prototype-Util.git/blobdiff - lib/Sub/Prototype/Util.pm
Make compile => 1 the default for wrap()
[perl/modules/Sub-Prototype-Util.git] / lib / Sub / Prototype / Util.pm
index b78f396adcfa24ff67492fe13d81a281d70b91a1..91520b823361f417e9d715e1d3f7f63d4888a04e 100644 (file)
@@ -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</wrap>. Valid keys are :
 
@@ -122,13 +122,13 @@ Encloses the code into a C<sub { }> block. Default is true.
 
 =item C<< compile => $bool >>
 
-Makes L</wrap> compile the code generated and return the resulting code reference. Implies C<< sub => 1 >>. Be careful that in this case C<ref> must be a fully qualified function name. Defaults to false.
+Makes L</wrap> compile the code generated and return the resulting code reference. Be careful that in this case C<ref> must be a fully qualified function name. Defaults to true, but turned off when C<sub> is false.
 
 =back
 
 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' => '\&@' }, 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</wrap> instead.
 =cut
 
 sub recall {
- my $wrap = eval { wrap shift, compile => 1 };
+ my $wrap = eval { wrap shift };
  croak $@ if $@;
  return $wrap->(@_);
 }