-
Notifications
You must be signed in to change notification settings - Fork 3
/
Documenter.pm
52 lines (43 loc) · 1.33 KB
/
Documenter.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package Params::Validate::Dependencies::Documenter;
use strict;
use warnings;
use vars qw($VERSION);
$VERSION = '2.00';
use Scalar::Util qw(blessed);
# sets a magic flag in P::V::D that the code-refs use to tell
# whether they should document themselves or validate, then
# calls the code-ref
sub _document {
my $sub = shift();
local $Params::Validate::Dependencies::DOC = $sub;
$sub->({});
}
# gets passed the list of options for this validator, and spits
# out doco, recursing as necessary
sub _doc_me {
my $sub = shift;
my $list = {@_}->{list};
(my $name = $sub->name()) =~ s/_/ /g;
my @list = (
(map { (my $t = $_) =~ s/'/\\'/g; "'$t'" } grep { !ref($_) } @{$list}), # scalars first, quoted
(grep { ref($_) } @{$list}) # then code-refs
);
return
$name.' ('.
(
$#list > 0
? join(', ', map { _doc_element($_) } @list[0 .. $#list - 1]).
" ".$sub->join_with().' '._doc_element($list[-1])
: _doc_element($list[0])
).
')';
}
# passed an option, returning it if it's scalar, otherwise
# calling its ->_document() method
sub _doc_element {
my $element = shift;
if(!ref($element)) { return $element }
elsif(blessed($element)) { return $element->_document(); }
else { return '[coderef does not support autodoc]' }
}
1;