]> git.vpit.fr Git - perl/modules/Sub-Prototype-Util.git/blob - README
Update VPIT::TestHelpers to 15e8aee3
[perl/modules/Sub-Prototype-Util.git] / README
1 NAME
2     Sub::Prototype::Util - Prototype-related utility routines.
3
4 VERSION
5     Version 0.11
6
7 SYNOPSIS
8         use Sub::Prototype::Util qw<flatten wrap recall>;
9
10         my @a = qw<a b c>;
11         my @args = ( \@a, 1, { d => 2 }, undef, 3 );
12
13         my @flat = flatten '\@$;$', @args;
14         # @flat contains now ('a', 'b', 'c', 1, { d => 2 })
15
16         my $res = recall 'CORE::push', @args;
17         # @a contains now 'a', 'b', 'c', 1, { d => 2 }, undef, 3
18         # and $res is 7
19
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)
24
25 DESCRIPTION
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
29     functions.
30
31     They all handle 5.10's "_" prototype.
32
33 FUNCTIONS
34   "flatten"
35         my @flattened = flatten($proto, @args);
36
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.
43
44   "wrap"
45         my $wrapper = wrap($name, %opts);
46         my $wrapper = wrap({ $name => $proto }, %opts);
47
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.
51
52         my $a = [ 0 .. 2 ];
53         my $push = wrap 'CORE::push';
54         $push->($a, 3, 4); # returns 3 + 2 = 5 and $a now contains 0 .. 4
55
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
59     to call it.
60
61         my $push = wrap { 'CORE::push' => '\@$' }; # only pushes 1 arg
62
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 :
65
66     *   "ref => $func"
67
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.
71
72     *   "wrong_ref => $code"
73
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"
77         too.
78
79     *   "sub => $bool"
80
81         Encloses the code into a "sub { }" block. Default is true.
82
83     *   "compile => $bool"
84
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"
88         is false.
89
90     For example, this allows you to recall into "CORE::grep" and "CORE::map"
91     by using the "\&@" prototype :
92
93         my $grep = wrap { 'CORE::grep' => '\&@' };
94         # the prototypes are intentionally different
95         sub mygrep (&@) { $grep->(@_) }
96
97   "recall"
98         my @res = recall($name, @args);
99         my @res = recall({ $name => $proto }, @args);
100
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.
105
106         my $a = [ ];
107         recall { 'CORE::push' => '\@$' }, $a, 1, 2, 3; # $a just contains 1
108
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.
111
112 EXPORT
113     The functions "flatten", "wrap" and "recall" are only exported on
114     request, either by providing their name or by the ':funcs' and ':all'
115     tags.
116
117 DEPENDENCIES
118     Carp, Exporter (core modules since perl 5), Scalar::Util (since 5.7.3).
119
120 AUTHOR
121     Vincent Pit, "<perl at profvince.com>", <http://www.profvince.com>.
122
123     You can contact me by mail or on "irc.perl.org" (vincent).
124
125 BUGS
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.
131
132 SUPPORT
133     You can find documentation for this module with the perldoc command.
134
135         perldoc Sub::Prototype::Util
136
137     Tests code coverage report is available at
138     <http://www.profvince.com/perl/cover/Sub-Prototype-Util>.
139
140 COPYRIGHT & LICENSE
141     Copyright 2008,2009,2010,2011,2013 Vincent Pit, all rights reserved.
142
143     This program is free software; you can redistribute it and/or modify it
144     under the same terms as Perl itself.
145