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
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 :
=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
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;
=cut
sub recall {
- my $wrap = eval { wrap shift, compile => 1 };
+ my $wrap = eval { wrap shift };
croak $@ if $@;
return $wrap->(@_);
}