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;
14 # @flat contains now ('a', 'b', 'c', 1, { d => 2 })
16 my $res = recall 'CORE::push', @args;
17 # @a contains now 'a', 'b', 'c', 1, { d => 2 }, undef, 3
20 my $splice = wrap 'CORE::splice';
21 my @b = $splice->(\@a, 4, 2);
22 # @a contains now ('a', 'b', 'c', 1, 3)
23 # and @b is ({ d => 2 }, undef)
26 Prototypes are evil, but sometimes you just have to bear with them,
27 especially when messing with core functions. This module provides
28 several utilities aimed at facilitating "overloading" of prototyped
31 They all handle 5.10's "_" prototype.
35 my @flattened = flatten($proto, @args);
37 Flattens the array @args according to the prototype $proto. When @args
38 is what @_ is after calling a subroutine with prototype $proto,
39 "flatten" returns the list of what @_ would have been if there were no
40 prototype. It croaks if the arguments can't possibly match the required
41 prototype, e.g. when a reference type is wrong or when not enough
42 elements were provided.
45 my $wrapper = wrap($name, %opts);
46 my $wrapper = wrap({ $name => $proto }, %opts);
48 Generates a wrapper that calls the function $name with a prototyped
49 argument list. That is, the wrapper's arguments should be what @_ is
50 when you define a subroutine with the same prototype as $name.
53 my $push = wrap 'CORE::push';
54 $push->($a, 3, 4); # returns 3 + 2 = 5 and $a now contains 0 .. 4
56 You can force the use of a specific prototype. In this case, $name must
57 be a hash reference that holds exactly one key / value pair, the key
58 being the function name and the value the prototpye that should be used
61 my $push = wrap { 'CORE::push' => '\@$' }; # only pushes 1 arg
63 The remaining arguments %opts are treated as key / value pairs that are
64 meant to tune the code generated by "wrap". Valid keys are :
68 Specifies the function used in the generated code to test the
69 reference type of scalars. Defaults to 'ref'. You may also want to
70 use "reftype" in Scalar::Util.
72 * "wrong_ref => $code"
74 The code executed when a reference of incorrect type is encountered.
75 The result of this snippet is also the result of the generated code,
76 hence it defaults to 'undef'. It's a good place to "croak" or "die"
81 Encloses the code into a "sub { }" block. Default is true.
85 Makes "wrap" compile the code generated and return the resulting
86 code reference. Be careful that in this case "ref" must be a fully
87 qualified function name. Defaults to true, but turned off when "sub"
90 For example, this allows you to recall into "CORE::grep" and "CORE::map"
91 by using the "\&@" prototype :
93 my $grep = wrap { 'CORE::grep' => '\&@' };
94 # the prototypes are intentionally different
95 sub mygrep (&@) { $grep->(@_) }
98 my @res = recall($name, @args);
99 my @res = recall({ $name => $proto }, @args);
101 Calls the function $name with the prototyped argument list @args. That
102 is, @args should be what @_ is when you call a subroutine with $name as
103 prototype. You can still force the prototype by passing "{ $name =>
104 $proto }" as the first argument.
107 recall { 'CORE::push' => '\@$' }, $a, 1, 2, 3; # $a just contains 1
109 It's implemented in terms of "wrap", and hence calls "eval" at each run.
110 If you plan to recall several times, consider using "wrap" instead.
113 The functions "flatten", "wrap" and "recall" are only exported on
114 request, either by providing their name or by the ':funcs' and ':all'
118 Carp, Exporter (core modules since perl 5), Scalar::Util (since 5.7.3).
121 Vincent Pit, "<perl at profvince.com>", <http://www.profvince.com>.
123 You can contact me by mail or on "irc.perl.org" (vincent).
126 Please report any bugs or feature requests to "bug-sub-prototype-util at
127 rt.cpan.org", or through the web interface at
128 <http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Sub-Prototype-Util>. I
129 will be notified, and then you'll automatically be notified of progress
130 on your bug as I make changes.
133 You can find documentation for this module with the perldoc command.
135 perldoc Sub::Prototype::Util
137 Tests code coverage report is available at
138 <http://www.profvince.com/perl/cover/Sub-Prototype-Util>.
141 Copyright 2008,2009,2010,2011,2013 Vincent Pit, all rights reserved.
143 This program is free software; you can redistribute it and/or modify it
144 under the same terms as Perl itself.