]> git.vpit.fr Git - perl/modules/Sub-Prototype-Util.git/blob - README
Importing Sub-Prototype-Util-0.08.tar.gz
[perl/modules/Sub-Prototype-Util.git] / README
1 NAME
2     Sub::Prototype::Util - Prototype-related utility routines.
3
4 VERSION
5     Version 0.08
6
7 SYNOPSIS
8         use Sub::Prototype::Util qw/flatten recall wrap/;
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', compile => 1;
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.
32
33   "recall $name, @args"
34     Calls the function $name with the prototyped argument list @args. That
35     is, @args should be what @_ is when you define a subroutine with the
36     same prototype as $name. For example,
37
38         my $a = [ ];
39         recall 'CORE::push', $a, 1, 2, 3;
40
41     will call "push @$a, 1, 2, 3" and so fill the arrayref $a with "1, 2,
42     3". This is especially needed for core functions because you can't
43     "goto" into them.
44
45     You can also force the use of a specific prototype. In this case, $name
46     must be a hash reference that holds exactly one key/value pair, the key
47     being the function name and the value the prototpye that should be used
48     to call it.
49
50         recall { 'CORE::push' => '\@$' }, $a, 1, 2, 3; # will only push 1
51
52     This allows you to recall into "CORE::grep" and "CORE::map" by using the
53     "\&@" prototype :
54
55         sub mygrep (&@) { recall { 'CORE::grep' => '\&@' }, @_ } # the prototypes are intentionally different
56
57   "wrap $name, %opts"
58     Generates a wrapper that does the same thing as "recall", but
59     specialized for a given function. This wrapper can be compiled once for
60     all to avoid calling "eval" at each run (like "recall" does). You can
61     still force the prototype by passing "{ $name => $proto }" as the first
62     argument. Others arguments are seen as key / value pairs and tune the
63     code generated by "wrap". Valid keys are :
64
65     "ref => $func"
66         Specifies the function used in the generated code to test the
67         reference type of scalars. Defaults to 'ref'. You may also want to
68         use "Scalar::Util::reftype".
69
70     "wrong_ref => $code"
71         The code executed when a reference of incorrect type is encountered.
72         The result of this snippet is also the result of the generated code,
73         hence it defaults to 'undef'. It's a good place to "croak" or "die"
74         too.
75
76     "sub => $bool"
77         Encloses the code into a "sub { }" block. Default is true.
78
79     "compile => $bool"
80         Makes "wrap" compile the code generated and return the resulting
81         code reference. Implies "sub => 1". Be careful that in this case
82         "ref" must be a fully qualified function name. Defaults to false.
83
84     This is how you make your own "push" that pushes into array references :
85
86         my @a = (0 .. 2);
87         my $push = wrap 'CORE::push', compile => 1;
88         $push->(\@a, 3 .. 7); # returns 3 + 5 = 8, and @a now contains 0 .. 7
89
90 EXPORT
91     The functions "flatten", "recall" and "wrap" are only exported on
92     request, either by providing their name or by the ':funcs' and ':all'
93     tags.
94
95 DEPENDENCIES
96     Carp, Exporter (core modules since perl 5), Scalar::Util (since 5.7.3).
97
98 AUTHOR
99     Vincent Pit, "<perl at profvince.com>", <http://www.profvince.com>.
100
101     You can contact me by mail or on #perl @ FreeNode (vincent or
102     Prof_Vince).
103
104 BUGS
105     Please report any bugs or feature requests to "bug-sub-prototype-util at
106     rt.cpan.org", or through the web interface at
107     <http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Sub-Prototype-Util>. I
108     will be notified, and then you'll automatically be notified of progress
109     on your bug as I make changes.
110
111 SUPPORT
112     You can find documentation for this module with the perldoc command.
113
114         perldoc Sub::Prototype::Util
115
116     Tests code coverage report is available at
117     <http://www.profvince.com/perl/cover/Sub-Prototype-Util>.
118
119 COPYRIGHT & LICENSE
120     Copyright 2008 Vincent Pit, all rights reserved.
121
122     This program is free software; you can redistribute it and/or modify it
123     under the same terms as Perl itself.
124