2 Sub::Prototype::Util - Prototype-related utility routines.
8 use Sub::Prototype::Util qw<flatten wrap recall>;
11 my @args = ( \@a, 1, { d => 2 }, undef, 3 );
13 my @flat = flatten '\@$;$', @args; # ('a', 'b', 'c', 1, { d => 2 })
14 recall 'CORE::push', @args; # @a contains 'a', 'b', 'c', 1, { d => 2 }, undef, 3
15 my $splice = wrap 'CORE::splice';
16 my @b = $splice->(\@a, 4, 2); # @a is now ('a', 'b', 'c', 1, 3) and @b is ({ d => 2 }, undef)
19 Prototypes are evil, but sometimes you just have to bear with them,
20 especially when messing with core functions. This module provides
21 several utilities aimed at facilitating "overloading" of prototyped
24 They all handle 5.10's "_" prototype.
27 "flatten $proto, @args"
28 Flattens the array @args according to the prototype $proto. When @args
29 is what @_ is after calling a subroutine with prototype $proto,
30 "flatten" returns the list of what @_ would have been if there were no
31 prototype. It croaks if the arguments can't possibly match the required
32 prototype, e.g. when a reference type is wrong or when not enough
33 elements were provided.
36 Generates a wrapper that calls the function $name with a prototyped
37 argument list. That is, the wrapper's arguments should be what @_ is
38 when you define a subroutine with the same prototype as $name.
41 my $push = wrap 'CORE::push';
42 $push->($a, 3, 4); # returns 3 + 2 = 5 and $a now contains 0 .. 4
44 You can force the use of a specific prototype. In this case, $name must
45 be a hash reference that holds exactly one key / value pair, the key
46 being the function name and the value the prototpye that should be used
49 my $push = wrap { 'CORE::push' => '\@$' }; # only pushes 1 arg
51 Others arguments are seen as key / value pairs that are meant to tune
52 the code generated by "wrap". Valid keys are :
55 Specifies the function used in the generated code to test the
56 reference type of scalars. Defaults to 'ref'. You may also want to
57 use "reftype" in Scalar::Util.
60 The code executed when a reference of incorrect type is encountered.
61 The result of this snippet is also the result of the generated code,
62 hence it defaults to 'undef'. It's a good place to "croak" or "die"
66 Encloses the code into a "sub { }" block. Default is true.
69 Makes "wrap" compile the code generated and return the resulting
70 code reference. Be careful that in this case "ref" must be a fully
71 qualified function name. Defaults to true, but turned off when "sub"
74 For example, this allows you to recall into "CORE::grep" and "CORE::map"
75 by using the "\&@" prototype :
77 my $grep = wrap { 'CORE::grep' => '\&@' };
78 sub mygrep (&@) { $grep->(@_) } # the prototypes are intentionally different
81 Calls the function $name with the prototyped argument list @args. That
82 is, @args should be what @_ is when you call a subroutine with $name as
83 prototype. You can still force the prototype by passing "{ $name =>
84 $proto }" as the first argument.
87 recall { 'CORE::push' => '\@$' }, $a, 1, 2, 3; # $a just contains 1
89 It's implemented in terms of "wrap", and hence calls "eval" at each run.
90 If you plan to recall several times, consider using "wrap" instead.
93 The functions "flatten", "wrap" and "recall" are only exported on
94 request, either by providing their name or by the ':funcs' and ':all'
98 Carp, Exporter (core modules since perl 5), Scalar::Util (since 5.7.3).
101 Vincent Pit, "<perl at profvince.com>", <http://www.profvince.com>.
103 You can contact me by mail or on "irc.perl.org" (vincent).
106 Please report any bugs or feature requests to "bug-sub-prototype-util at
107 rt.cpan.org", or through the web interface at
108 <http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Sub-Prototype-Util>. I
109 will be notified, and then you'll automatically be notified of progress
110 on your bug as I make changes.
113 You can find documentation for this module with the perldoc command.
115 perldoc Sub::Prototype::Util
117 Tests code coverage report is available at
118 <http://www.profvince.com/perl/cover/Sub-Prototype-Util>.
121 Copyright 2008,2009,2010,2011 Vincent Pit, all rights reserved.
123 This program is free software; you can redistribute it and/or modify it
124 under the same terms as Perl itself.