]> git.vpit.fr Git - perl/modules/Sub-Prototype-Util.git/blob - README
POD tweak
[perl/modules/Sub-Prototype-Util.git] / README
1 NAME
2     Sub::Prototype::Util - Prototype-related utility routines.
3
4 VERSION
5     Version 0.10
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; # ('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)
17
18 DESCRIPTION
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
22     functions.
23
24     They all handle 5.10's "_" prototype.
25
26 FUNCTIONS
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.
34
35   "wrap $name, %opts"
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.
39
40         my $a = [ 0 .. 2 ];
41         my $push = wrap 'CORE::push';
42         $push->($a, 3, 4); # returns 3 + 2 = 5 and $a now contains 0 .. 4
43
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
47     to call it.
48
49         my $push = wrap { 'CORE::push' => '\@$' }; # only pushes 1 arg
50
51     Others arguments are seen as key / value pairs that are meant to tune
52     the code generated by "wrap". Valid keys are :
53
54     "ref => $func"
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.
58
59     "wrong_ref => $code"
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"
63         too.
64
65     "sub => $bool"
66         Encloses the code into a "sub { }" block. Default is true.
67
68     "compile => $bool"
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"
72         is false.
73
74     For example, this allows you to recall into "CORE::grep" and "CORE::map"
75     by using the "\&@" prototype :
76
77         my $grep = wrap { 'CORE::grep' => '\&@' };
78         sub mygrep (&@) { $grep->(@_) } # the prototypes are intentionally different
79
80   "recall $name, @args"
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.
85
86         my $a = [ ];
87         recall { 'CORE::push' => '\@$' }, $a, 1, 2, 3; # $a just contains 1
88
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.
91
92 EXPORT
93     The functions "flatten", "wrap" and "recall" are only exported on
94     request, either by providing their name or by the ':funcs' and ':all'
95     tags.
96
97 DEPENDENCIES
98     Carp, Exporter (core modules since perl 5), Scalar::Util (since 5.7.3).
99
100 AUTHOR
101     Vincent Pit, "<perl at profvince.com>", <http://www.profvince.com>.
102
103     You can contact me by mail or on "irc.perl.org" (vincent).
104
105 BUGS
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.
111
112 SUPPORT
113     You can find documentation for this module with the perldoc command.
114
115         perldoc Sub::Prototype::Util
116
117     Tests code coverage report is available at
118     <http://www.profvince.com/perl/cover/Sub-Prototype-Util>.
119
120 COPYRIGHT & LICENSE
121     Copyright 2008,2009,2010,2011 Vincent Pit, all rights reserved.
122
123     This program is free software; you can redistribute it and/or modify it
124     under the same terms as Perl itself.
125