| 1 | package App::Grok::Resource::Functions; | ||||||||||||||||||||||||||||
| 2 | 40 | ▶ | BEGIN { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 40 | $App::Grok::Resource::Functions::AUTHORITY = 'cpan:HINRIK'; | |||||||||||||||||||||||||||
| 4 | } | ||||||||||||||||||||||||||||
| 5 | { | ||||||||||||||||||||||||||||
| 6 | 40 | $App::Grok::Resource::Functions::VERSION = '0.26'; | |||||||||||||||||||||||||||
| 7 | } | ||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 40 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 40 | ▶ | use warnings FATAL => 'all'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 40 | ▶ | use File::ShareDir qw<dist_dir>; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 40 | ▶ | use File::Spec::Functions qw<catdir catfile splitpath>; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 40 | ▶ | use base qw(Exporter); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 40 | our @EXPORT_OK = qw(func_index func_fetch func_locate); | |||||||||||||||||||||||||||
| 16 | 40 | our %EXPORT_TAGS = ( ALL => [@EXPORT_OK] ); | |||||||||||||||||||||||||||
| 17 | 40 | ▶ | use constant { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | 40 | NAME => 0, | |||||||||||||||||||||||||||
| 19 | POD => 1, | ||||||||||||||||||||||||||||
| 20 | FILE => 2, | ||||||||||||||||||||||||||||
| 21 | 40 | }; | |||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 40 | my %functions; | |||||||||||||||||||||||||||
| 24 | 40 | my $syn_dir = catdir(dist_dir('Perl6-Doc'), 'Synopsis'); | |||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 12 | ▶ | sub func_fetch { | ||||||||||||||||||||||||||
|
Subroutine
func_fetch: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 27 | 12 | my ($func) = @_; | |||||||||||||||||||||||||||
| 28 | 12 | ▶ | _read_functions() if !%functions; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | 12 | ▶ | return $functions{$func}[POD] if defined $functions{$func}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 31 | 8 | return; | |||||||||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 2 | ▶ | sub func_index { | ||||||||||||||||||||||||||
|
Subroutine
func_index: called CC=2 SCAR=7.2
Pod
documented
| |||||||||||||||||||||||||||||
| 35 | 2 | ▶ | _read_functions() if !%functions; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 36 | 2 | return keys %functions; | |||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 2 | ▶ | sub func_locate { | ||||||||||||||||||||||||||
|
Subroutine
func_locate: called CC=3 SCAR=12.5
Pod
documented
| |||||||||||||||||||||||||||||
| 40 | 2 | my ($func) = @_; | |||||||||||||||||||||||||||
| 41 | 2 | ▶ | _read_functions() if !%functions; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 42 | 2 | ▶ | return if !defined $functions{$func}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 43 | 0 | return $functions{$func}[FILE]; | |||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | ## no critic (Subroutines::ProhibitExcessComplexity) | ||||||||||||||||||||||||||||
| 47 | 16 | ▶ | sub _read_functions { | ||||||||||||||||||||||||||
|
Subroutine
_read_functions: called CC=23 SCAR=31.4
| |||||||||||||||||||||||||||||
| 48 | 16 | my ($self) = @_; | |||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 16 | my $S29_file = catfile(dist_dir('Perl6-Doc'), 'Synopsis', 'S29-functions.pod'); | |||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | ## no critic (InputOutput::RequireBriefOpen) | ||||||||||||||||||||||||||||
| 53 | 16 | ▶ | open my $S29, '<', $S29_file or die "Can't open '$S29_file': $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | # read until you find 'Function Packages' | ||||||||||||||||||||||||||||
| 56 | 16 | ▶ | until (<$S29> =~ /Function Packages/) {} | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 16 | my (%S29_funcs, $func_name); | |||||||||||||||||||||||||||
| 59 | 16 | ▶ | while (my $line = <$S29>) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 60 | 12912 | ▶ | if (my ($directive, $title) = $line =~ /^=(\S+)(?: +(.+))?/) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 61 | 2000 | ▶ | if ($directive eq 'item') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 62 | # Found Perl6 function name | ||||||||||||||||||||||||||||
| 63 | 1440 | ▶ | if (my ($reference) = $title =~ /-- (see S\d+.*)/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 64 | # one-line entries | ||||||||||||||||||||||||||||
| 65 | 496 | (my $func = $title) =~ s/^(\S+).*/$1/; | |||||||||||||||||||||||||||
| 66 | 496 | $S29_funcs{$func} = $reference; | |||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | else { | ||||||||||||||||||||||||||||
| 69 | 944 | $title =~ s/\(.*\)//; | |||||||||||||||||||||||||||
| 70 | 944 | $func_name = $title; | |||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | else { | ||||||||||||||||||||||||||||
| 74 | 560 | $func_name = undef; | |||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | elsif ($func_name) { | ||||||||||||||||||||||||||||
| 78 | # Adding documentation to the function name | ||||||||||||||||||||||||||||
| 79 | 8960 | $S29_funcs{$func_name} .= $line; | |||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 16 | my %S29_sanitized; | |||||||||||||||||||||||||||
| 84 | 16 | ▶ | while (my ($func, $body) = each %S29_funcs) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 85 | 1424 | $body = "=encoding utf8\n\n=head2 C<<< $func >>>\n$body"; | |||||||||||||||||||||||||||
| 86 | 1424 | ▶ | $S29_sanitized{$func} = [$func, $body, $S29_file] if $func !~ /\s/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 1424 | ▶ | if ($func =~ /,/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 89 | 176 | my @funcs = split /,\s+/, $func; | |||||||||||||||||||||||||||
| 90 | 176 | $S29_sanitized{$_} = [$func, $body, $S29_file] for @funcs; | |||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 16 | %functions = %S29_sanitized; | |||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | # read S32 | ||||||||||||||||||||||||||||
| 97 | 16 | my $S32_dir = catdir($syn_dir, 'S32-setting-library'); | |||||||||||||||||||||||||||
| 98 | 40 | ▶ | my @sections = map { (splitpath($_))[2] } glob "$S32_dir/*.pod"; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 99 | 16 | $_ = catdir($S32_dir, $_) for @sections; | |||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | 16 | for my $section (@sections) { | |||||||||||||||||||||||||||
| 102 | ## no critic (InputOutput::RequireBriefOpen) | ||||||||||||||||||||||||||||
| 103 | 160 | ▶ | open my $handle, '<', $section or die "Can't open $section: $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 160 | my @new_func; | |||||||||||||||||||||||||||
| 106 | 160 | ▶ | while (my $line = <$handle>) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 107 | 70576 | ▶ | if (my ($directive, $title) = $line =~ /^=(\S+)(?: +(.+))?/) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 108 | 8384 | ▶ | if (defined $new_func[NAME]) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 109 | 4144 | my $name = $new_func[NAME]; | |||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | # S32 only overwrites S29 if the new definition is wordier | ||||||||||||||||||||||||||||
| 112 | 4144 | ▶ | if (!defined $functions{$name} || | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not defined $functions{$name} or length $new_func[1] > length $functions{$name}[1]
MC/DC100%2/2
not defined $functions{$name} or length $new_func[1] > length $functions{$name}[1]
not defined $functions{$name}
length $new_func[1] > length $functions{$name}[1]
Truth table100%3/3
not(A) or B
A= not defined $functions{$name}B= length $new_func[1] > length $functions{$name}[1]
| |||||||||||||||||||||||||||||
| 113 | length $new_func[POD] > length $functions{$name}[POD]) { | ||||||||||||||||||||||||||||
| 114 | 3552 | $functions{$new_func[NAME]} = [@new_func]; | |||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | 4144 | @new_func = (); | |||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||
| 118 | 8384 | ▶ | if ($directive eq 'item') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 119 | 4400 | $title =~ s/.*?method\s*//; | |||||||||||||||||||||||||||
| 120 | 4400 | $title =~ s/^(\S+)\s*\(.*/$1/; | |||||||||||||||||||||||||||
| 121 | 4400 | ▶ | if ($title =~ /^\S+$/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 122 | 4144 | $new_func[NAME] = $title; | |||||||||||||||||||||||||||
| 123 | 4144 | $new_func[POD] = "=encoding utf8\n\n=head2 C<<< $title >>>\n"; | |||||||||||||||||||||||||||
| 124 | 4144 | $new_func[FILE] = $section; | |||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||
| 128 | elsif (defined $new_func[FILE]) { | ||||||||||||||||||||||||||||
| 129 | # Adding documentation to the function name | ||||||||||||||||||||||||||||
| 130 | 38864 | $new_func[POD] .= $line; | |||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | 160 | close $handle; | |||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | 16 | return; | |||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | 40 | 1; | |||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | =encoding utf8 | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | =head1 NAME | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | App::Grok::Resource::Functions - S29/S32 functions resource for grok | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | use strict; | ||||||||||||||||||||||||||||
| 151 | use warnings; | ||||||||||||||||||||||||||||
| 152 | use App::Grok::Resource::Functions qw<:ALL>; | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | # a list of all functions | ||||||||||||||||||||||||||||
| 155 | my @index = func_index(); | ||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | # documentation for a specific functions | ||||||||||||||||||||||||||||
| 158 | my $pod = func_fetch('split'); | ||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | # the file where the function was found | ||||||||||||||||||||||||||||
| 161 | my $file = func_locate('split'); | ||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | This resource reads Synopses 29 and 32, and allows you to look up the | ||||||||||||||||||||||||||||
| 166 | functions therein. | ||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | =head1 FUNCTIONS | ||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | =head2 C<func_index> | ||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | Takes no arguments. Returns a list of all known function names. | ||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | =head2 C<func_fetch> | ||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | Takes the name of a function as an argument. Returns the documentation for | ||||||||||||||||||||||||||||
| 177 | that function. | ||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | =head2 C<func_locate> | ||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | Takes the same argument as L<C<func_fetch>|/func_fetch>. Returns the path to | ||||||||||||||||||||||||||||
| 182 | the Synopsis file where the given function was found. | ||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | =cut | ||||||||||||||||||||||||||||