2 Sub::Nary - Try to count how many elements a subroutine can return in
11 my $sn = Sub::Nary->new();
12 my $r = $sn->nary(\&hlagh);
15 This module uses the B framework to walk into subroutines and try to
16 guess how many scalars are likely to be returned in list context. It's
17 not always possible to give a definitive answer to this question at
18 compile time, so the results are given in terms of "probability of
19 return" (to be understood in a sense described below).
23 The usual constructor. Currently takes no argument.
26 Takes a code reference to a named or anonymous subroutine, and returns a
27 hash reference whose keys are the possible numbers of returning scalars,
28 and the corresponding values the "probability" to get them. The special
29 key 'list' is used to denote a possibly infinite number of returned
30 arguments. The return value hence would look at
32 { 1 => 0.2, 2 => 0.4, 4 => 0.3, list => 0.1 }
34 that is, we should get 1 scalar 1 time over 5 and so on. The sum of all
35 values is 1. The returned result, and all the results obtained from
36 intermediate subs, are cached into the object.
39 Flushes the Sub::Nary object cache. Returns the object itself.
42 The probability is computed as such :
44 * When branching, each branch is considered equally possible.
45 For example, the subroutine
55 is seen returning one or two arguments each with probability "1/2".
67 it is considered to return 3 scalars with probability "1/2", 2 with
68 probability "1/2 * 1/2 = 1/4" and 1 (when the two tests fail, the
69 last computed value is returned, which here is "$x > 0.9" evaluated
70 in the scalar context of the test) with remaining probability "1/4".
72 * The total probability law for a given returning point is the
73 convolution product of the probabilities of its list elements.
80 returns 3 or 4 arguments with probability "1/2" ; and
83 return simple(), simple()
86 never returns 1 argument but returns 2 with probability "1/2 * 1/2 =
87 1/4", 3 with probability "1/2 * 1/2 + 1/2 * 1/2 = 1/2" and 4 with
88 probability "1/4" too.
90 * If a core function may return different numbers of scalars, each kind
91 is considered equally possible.
92 For example, "stat" returns 13 elements on success and 0 on error.
93 The according probability will then be "{ 0 => 0.5, 13 => 0.5 }".
95 * The "list" state is absorbing in regard of all the other ones.
96 This is just a pedantic way to say that "list + fixed length =
100 return 1, simple(), @_
103 is considered as always returning an unbounded list.
105 Also, the convolution law does not behave the same when "list"
106 elements are involved : in the following example,
117 return oneorlist(), oneorlist()
120 "composed" returns 2 scalars with probability "1/2 * 1/2 = 1/4" and
121 a "list" with probability "3/4".
124 An object-oriented module shouldn't export any function, and so does
128 The algorithm may be pessimistic (things seen as "list" while they are
129 of fixed length) but not optimistic (the opposite, duh).
131 "wantarray" isn't specialized when encountered in the optree.
136 Carp (standard since perl 5), B (since perl 5.005) and XSLoader (since
140 Vincent Pit, "<perl at profvince.com>", <http://www.profvince.com>.
142 You can contact me by mail or on #perl @ FreeNode (vincent or
146 Please report any bugs or feature requests to "bug-b-nary at
147 rt.cpan.org", or through the web interface at
148 <http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Sub-Nary>. I will be
149 notified, and then you'll automatically be notified of progress on your
150 bug as I make changes.
153 You can find documentation for this module with the perldoc command.
157 Tests code coverage report is available at
158 <http://www.profvince.com/perl/cover/Sub-Nary>.
161 Thanks to Sebastien Aperghis-Tramoni for helping to name this module.
164 Copyright 2008 Vincent Pit, all rights reserved.
166 This program is free software; you can redistribute it and/or modify it
167 under the same terms as Perl itself.