my @a = qw<a b c>;
my @args = ( \@a, 1, { d => 2 }, undef, 3 );
- 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 @flat = flatten '\@$;$', @args;
+ # @flat contains now ('a', 'b', 'c', 1, { d => 2 })
+
+ my $res = recall 'CORE::push', @args;
+ # @a contains now 'a', 'b', 'c', 1, { d => 2 }, undef, 3
+ # and $res is 7
+
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)
+ my @b = $splice->(\@a, 4, 2);
+ # @a contains now ('a', 'b', 'c', 1, 3)
+ # and @b is ({ d => 2 }, undef)
=head1 DESCRIPTION
$msg;
}
-=head2 C<flatten $proto, @args>
+=head2 C<flatten>
+
+ my @flattened = flatten($proto, @args);
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<flatten> returns the list of what C<@_> would have been if there were no prototype.
return @args;
}
-=head2 C<wrap $name, %opts>
+=head2 C<wrap>
+
+ my $wrapper = wrap($name, %opts);
+ my $wrapper = wrap({ $name => $proto }, %opts);
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>.
=over 4
-=item C<< ref => $func >>
+=item *
+
+C<< ref => $func >>
Specifies the function used in the generated code to test the reference type of scalars.
Defaults to C<'ref'>.
You may also want to use L<Scalar::Util/reftype>.
-=item C<< wrong_ref => $code >>
+=item *
+
+C<< wrong_ref => $code >>
The code executed when a reference of incorrect type is encountered.
The result of this snippet is also the result of the generated code, hence it defaults to C<'undef'>.
It's a good place to C<croak> or C<die> too.
-=item C<< sub => $bool >>
+=item *
+
+C<< sub => $bool >>
Encloses the code into a C<sub { }> block.
Default is true.
-=item C<< compile => $bool >>
+=item *
+
+C<< compile => $bool >>
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.
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' => '\&@' };
- sub mygrep (&@) { $grep->(@_) } # the prototypes are intentionally different
+ # the prototypes are intentionally different
+ sub mygrep (&@) { $grep->(@_) }
=cut
return $call;
}
-=head2 C<recall $name, @args>
+=head2 C<recall>
+
+ my @res = recall($name, @args);
+ my @res = recall({ $name => $proto }, @args);
Calls the function C<$name> with the prototyped argument list C<@args>.
That is, C<@args> should be what C<@_> is when you call a subroutine with C<$name> as prototype.