3 use Pod::Simple::SimpleTree;
5 my ($tap, $test, %Missing);
8 @ARGV = grep { not($_ eq '--tap' and $tap = 1) } @ARGV;
15 my (%Kinds, %Flavor, @Types);
18 my $p = Pod::Simple::SimpleTree->new;
19 $p->accept_targets('Pod::Functions');
20 my $tree = $p->parse_file(shift)->root;
22 foreach my $TL_node (@$tree[2 .. $#$tree]) {
23 next unless $TL_node->[0] eq 'over-text';
25 while ($i <= $#$TL_node) {
26 if ($TL_node->[$i][0] ne 'item-text') {
31 my $item_text = $TL_node->[$i][2];
32 die "Confused by $item_text at line $TL_node->[$i][1]{start_line}"
34 $item_text =~ s/\s+\z//s;
36 if ($TL_node->[$i+1][0] ne 'for'
37 || $TL_node->[$i+1][1]{target} ne 'Pod::Functions') {
39 ++$Missing{$item_text} unless $Omit{$item_text};
42 my $data = $TL_node->[$i+1][2];
43 die "Confused by $data at line $TL_node->[$i+1][1]{start_line}"
44 unless ref $data eq 'ARRAY';
45 my $text = $data->[2];
46 die "Confused by $text at line $TL_node->[$i+1][1]{start_line}"
51 if ($text =~ s/^=//) {
52 # We are in "Perl Functions by Category"
53 die "Expected a paragraph after =item at $TL_node->[$i-2][1]{start_line}"
54 unless $TL_node->[$i][0] eq 'Para';
55 my $para = $TL_node->[$i];
56 # $text is the "type" of the built-in
57 # Anything starting ! is not for inclusion in Pod::Functions
59 foreach my $func (@$para[2 .. $#$para]) {
60 next unless ref $func eq 'ARRAY';
62 $func->[0] eq 'C' && !ref $func->[2] ? $func :
63 $func->[0] eq 'L' && ref $func->[2]
64 && $func->[2][0] eq 'C' && !ref $func->[2][2] ? $func->[2] :
65 die "Expected only C<> blocks in paragraph after item at $TL_node->[$i-2][1]{start_line}";
66 # Everything is plain text (ie $c_node->[2] is everything)
67 # except for C<-I<X>>. So untangle up to one level of nested <>
68 my $funcname = join '', map {
70 } @$c_node[2..$#$c_node];
71 $funcname =~ s!(q.?)//!$1/STRING/!;
72 push @{$Kinds{$text}}, $funcname;
75 ++$Omit{$_} foreach @{$Kinds{$text}};
77 push @Types, [$text, $item_text];
80 $item_text =~ s/ .*//;
81 # For now, just remove any metadata about when it was added:
83 $Flavor{$item_text} = $text;
84 ++$Omit{$item_text} if $text =~ /^!/;
89 # Take the lists of functions for each type group, and invert them to get the
90 # type group (or groups) for each function:
92 while (my ($type, $funcs) = each %Kinds) {
93 push @{$Type{$_}}, $type foreach @$funcs;
96 # We sort __SUB__ after sub, but before substr, but __PACKAGE__ after package,
97 # and __END__ after END. (We create a temporary array of two elements, where
98 # the second has the underscores squeezed out, and sort on that element
102 sort { uc $a->[1] cmp uc $b->[1]
103 || $b->[1] cmp $a->[1]
104 || $a->[0] =~ /^_/ # here $a and $b are identical when
105 # underscores squeezed out; so if $a
106 # begins with an underscore, it should
108 || $a->[0] cmp $b->[0]
109 } map { my $f = tr/_//dr; [ $_, $f ] }
114 foreach my $func (sort_funcs(keys %Flavor)) {
115 ok ( $Type{$func}, "$func is mentioned in at least one category group");
117 foreach (sort keys %Missing) {
118 # Ignore anything that looks like an alternative for a function we've
120 s!(?: [A-Z].*| \(\)|\( LIST \)| /PATTERN/.*)!!;
123 fail( "function '$_' has no summary for Pod::Functions" );
125 fail( "for Pod::Functions" );
128 foreach my $kind (sort keys %Kinds) {
129 my $funcs = $Kinds{$kind};
131 my $want = join ' ', sort_funcs(@$funcs);
132 is ("@$funcs", $want, "category $kind is correctly sorted" );
138 # blead will run this with miniperl, hence we can't use autodie
139 my $real = 'Functions.pm';
140 my $temp = "Functions.$$";
143 return if !defined $temp || !-e $temp;
144 unlink $temp or warn "Can't unlink '$temp': $!";
147 foreach ($real, $temp) {
149 unlink $_ or die "Can't unlink '$_': $!";
152 open my $fh, '>', $temp or die "Can't open '$temp' for writing: $!";
154 package Pod::Functions;
159 Pod::Functions - Group Perl's functions a la perlfunc.pod
165 my @misc_ops = @{ $Kinds{ 'Misc' } };
166 my $misc_dsc = $Type_Description{ 'Misc' };
170 perl /path/to/lib/Pod/Functions.pm
172 This will print a grouped list of Perl's functions, like the
173 L<perlfunc/"Perl Functions by Category"> section.
177 It exports the following variables:
183 This holds a hash-of-lists. Each list contains the functions in the category
188 In this hash each key represents a function and the value is the category.
189 The category can be a comma separated list.
193 In this hash each key represents a function and the value is a short
194 description of that function.
196 =item %Type_Description
198 In this hash each key represents a category of functions and the value is
199 a short description of that category.
203 This list of categories is used to produce the same order as the
204 L<perlfunc/"Perl Functions by Category"> section.
210 our $VERSION = '1.11';
214 our @ISA = qw(Exporter);
215 our @EXPORT = qw(%Kinds %Type %Flavor %Type_Description @Type_Order);
217 our(%Kinds, %Type, %Flavor, %Type_Description, @Type_Order);
223 my ($type, $desc) = @$_;
224 $type = "'$type'" if $type =~ /[^A-Za-z]/;
225 $desc =~ s!([\\'])!\\$1!g;
226 printf $fh " [%-9s => '%s'],\n", $type, $desc;
231 push @Type_Order, $_->[0];
232 $Type_Description{$_->[0]} = $_->[1];
239 my($name, @data) = split "\t", $_;
240 $Flavor{$name} = pop @data;
241 $Type{$name} = join ',', @data;
243 push @{$Kinds{$t}}, $name;
249 my( $typedesc, $list );
251 foreach my $type ( @Type_Order ) {
252 $list = join(", ", sort @{$Kinds{$type}});
253 $typedesc = $Type_Description{$type} . ":";
260 ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
262 ~~ ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
264 ~~ ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
273 foreach my $func (sort_funcs(keys %Flavor)) {
274 my $desc = $Flavor{$func};
275 die "No types listed for $func" unless $Type{$func};
276 next if $Omit{$func};
277 print $fh join("\t", $func, (sort @{$Type{$func}}), $desc), "\n";
280 close $fh or die "Can't close '$temp': $!";
281 rename $temp, $real or die "Can't rename '$temp' to '$real': $!";