| 1 | package MVC::Neaf::Util; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 109 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 109 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 109 | our $VERSION = '0.2901'; | |||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | =head1 NAME | ||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | MVC::Neaf::Util - Some static functions for Not Even A Framework | ||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | This is utility class. | ||||||||||||||||||||||||||||
| 14 | Nothing to see here unless one intends to work on L<MVC::Neaf> itself. | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | =head1 EXPORT | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | This module optionally exports anything it has. | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | =cut | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 109 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 23 | 109 | ▶ | use MIME::Base64 3.11; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 24 | 109 | ▶ | use Scalar::Util qw( openhandle ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 109 | ▶ | use parent qw(Exporter); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 27 | 109 | our @EXPORT_OK = qw( | |||||||||||||||||||||||||||
| 28 | bare_html_escape | ||||||||||||||||||||||||||||
| 29 | caller_info | ||||||||||||||||||||||||||||
| 30 | canonize_path | ||||||||||||||||||||||||||||
| 31 | check_path | ||||||||||||||||||||||||||||
| 32 | data_fh | ||||||||||||||||||||||||||||
| 33 | decode_json | ||||||||||||||||||||||||||||
| 34 | encode_b64 decode_b64 | ||||||||||||||||||||||||||||
| 35 | encode_json | ||||||||||||||||||||||||||||
| 36 | extra_missing | ||||||||||||||||||||||||||||
| 37 | http_date | ||||||||||||||||||||||||||||
| 38 | JSON | ||||||||||||||||||||||||||||
| 39 | make_getters | ||||||||||||||||||||||||||||
| 40 | maybe_list | ||||||||||||||||||||||||||||
| 41 | path_prefixes | ||||||||||||||||||||||||||||
| 42 | rex | ||||||||||||||||||||||||||||
| 43 | run_all | ||||||||||||||||||||||||||||
| 44 | run_all_nodie | ||||||||||||||||||||||||||||
| 45 | supported_methods | ||||||||||||||||||||||||||||
| 46 | ); | ||||||||||||||||||||||||||||
| 47 | 109 | our @CARP_NOT; | |||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | # use JSON::MaybeXS; # not now, see JSON() below | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | # Alphabetic order, please | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | =head2 caller_info() | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | Returns first caller(n) that is not inside MVC::Neaf itself. | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | This is implemented inside L<Carp::shortmess> | ||||||||||||||||||||||||||||
| 58 | but we can't rely on Carp's internals. | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | =cut | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 40 | ▶ | sub caller_info { | ||||||||||||||||||||||||||
|
Subroutine
caller_info: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||
| 63 | 40 | my $level = 0; | |||||||||||||||||||||||||||
| 64 | 40 | my @caller; | |||||||||||||||||||||||||||
| 65 | { | ||||||||||||||||||||||||||||
| 66 | # code stolen from Carp. | ||||||||||||||||||||||||||||
| 67 | # it's just a while(1) with fancy next/last conditionals. | ||||||||||||||||||||||||||||
| 68 | 40 | @caller = caller($level++); | |||||||||||||||||||||||||||
| 69 | 268 | ▶ | last unless defined $caller[0]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 70 | 268 | ▶ | redo if $caller[0] =~ /^MVC::Neaf/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 71 | 41 | ▶ | redo if $caller[0]->isa('MVC::Neaf::Util::Base'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 72 | }; | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 40 | ▶ | return wantarray ? @caller : \@caller; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 75 | }; | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | =head2 canonize_path( path, want_slash ) | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | Convert '////fooo//bar/' to '/foo/bar' and '//////' to either '' or '/'. | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | =cut | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | # Search for CANONIZE for ad-hoc implementations of this (for speed etc) | ||||||||||||||||||||||||||||
| 84 | 916 | ▶ | sub canonize_path { | ||||||||||||||||||||||||||
|
Subroutine
canonize_path: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 85 | 916 | my ($path, $want_slash) = @_; | |||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 916 | $path =~ s#//+#/#g; | |||||||||||||||||||||||||||
| 88 | 916 | ▶ | if ($want_slash) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 89 | 15 | $path =~ s#/$##; | |||||||||||||||||||||||||||
| 90 | 15 | $path =~ s#^/*#/#; | |||||||||||||||||||||||||||
| 91 | } else { | ||||||||||||||||||||||||||||
| 92 | 901 | $path =~ s#^/*#/#; | |||||||||||||||||||||||||||
| 93 | 901 | $path =~ s#/$##; | |||||||||||||||||||||||||||
| 94 | }; | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 916 | return $path; | |||||||||||||||||||||||||||
| 97 | }; | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | =head2 check_path | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | @array = check_path @array | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | Check a list of path for bad characters in path spec. | ||||||||||||||||||||||||||||
| 104 | Will issue a warning if something strange is present. | ||||||||||||||||||||||||||||
| 105 | Most notably, forbids C<:> in order to allow for future C</my/path/:param> | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | Returns unmodified list. | ||||||||||||||||||||||||||||
| 108 | This as well as prototype is done so for simpler integration with map. | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | =cut | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | 109 | my $path_allow = q{-/A-Za-z_0-9~.,!+'()*@}; | |||||||||||||||||||||||||||
| 113 | 109 | my $re_path_not = qr#[^$path_allow]#; | |||||||||||||||||||||||||||
| 114 | 282 | ▶ | sub check_path(@) { ## no critic # need proto for simpler wrapping around map | ||||||||||||||||||||||||||
|
Subroutine
check_path: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 115 | 282 | ▶ | if ( grep { $_ =~ $re_path_not } @_ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 116 | 1 | local @CARP_NOT = caller; | |||||||||||||||||||||||||||
| 117 | 1 | carp "NEAF Characters outside [$path_allow] in path are DEPRECATED until 0.30"; | |||||||||||||||||||||||||||
| 118 | }; | ||||||||||||||||||||||||||||
| 119 | 282 | ▶ | return wantarray ? @_ : shift; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 120 | }; | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | =head2 decode_b64 | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | Decode unpadded URL-friendly base64. | ||||||||||||||||||||||||||||
| 125 | Also works on normal one. | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | See L<MIME::Base64/decode_base64url>. | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | =cut | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | 6 | ▶ | sub decode_b64 { | ||||||||||||||||||||||||||
|
Subroutine
decode_b64: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 132 | 6 | my $str = shift; | |||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | 6 | $str =~ tr#-_#+/#; | |||||||||||||||||||||||||||
| 135 | 6 | return MIME::Base64::decode_base64($str); | |||||||||||||||||||||||||||
| 136 | }; | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | =head2 encode_b64 | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | Encode data as unpadded URL-friendly base64 - with C<-> for 62 and C<_> for 63. | ||||||||||||||||||||||||||||
| 141 | C<=> signs are removed. | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | See L<MIME::Base64/encode_base64url>. | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | =cut | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | sub encode_b64; | ||||||||||||||||||||||||||||
| 148 | 109 | *encode_b64 = \&MIME::Base64::encode_base64url; | |||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | =head2 extra_missing | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | extra_missing( \%input, \%allowed, \@required ) | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | Dies if %input doesn't pass validation. | ||||||||||||||||||||||||||||
| 155 | Only definedness is checked. | ||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | =cut | ||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | # Now this MUST be an existing module, right? | ||||||||||||||||||||||||||||
| 160 | 148 | ▶ | sub extra_missing { | ||||||||||||||||||||||||||
|
Subroutine
extra_missing: called CC=6 SCAR=28.1
Pod
documented
| |||||||||||||||||||||||||||||
| 161 | 148 | my ($input, $allowed, $required) = @_; | |||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | 148 | ▶ | my @extra = $allowed ? grep { !$allowed->{$_} } keys %$input : (); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 164 | 0 | ▶ | my @missing = $required ? grep { !defined $input->{$_} } @$required : (); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | 148 | ▶ | if (@extra+@missing) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 167 | 0 | my @stack = caller(1); | |||||||||||||||||||||||||||
| 168 | 0 | my @msg; | |||||||||||||||||||||||||||
| 169 | 0 | ▶ | push @msg, "missing required fields: ".join ",", @missing | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 170 | if @missing; | ||||||||||||||||||||||||||||
| 171 | 0 | ▶ | push @msg, "unknown fields present: ".join ",", @extra | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 172 | if @extra; | ||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | 0 | my $fun = $stack[3]; | |||||||||||||||||||||||||||
| 175 | 0 | $fun =~ s/^(.*)::/$1->/; | |||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | 0 | local @CARP_NOT = $stack[0]; | |||||||||||||||||||||||||||
| 178 | 0 | croak "$fun: ".join "; ", @msg; | |||||||||||||||||||||||||||
| 179 | }; | ||||||||||||||||||||||||||||
| 180 | }; | ||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | =head2 http_date | ||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | Return a date in format required by HTTP standard for cookies | ||||||||||||||||||||||||||||
| 185 | and cache expiration. | ||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | Expires=Wed, 13 Jan 2021 22:23:01 GMT; | ||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | =cut | ||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | # Yay premature optimization - use ad-hoc weekdays because locale is so botched | ||||||||||||||||||||||||||||
| 192 | # The "proper" way to do it is to set locale to C, call strftime, | ||||||||||||||||||||||||||||
| 193 | # and reset locale to whatever it was. | ||||||||||||||||||||||||||||
| 194 | 109 | my @week = qw( Sun Mon Tue Wed Thu Fri Sat ); | |||||||||||||||||||||||||||
| 195 | 109 | my @month = qw( Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec ); | |||||||||||||||||||||||||||
| 196 | 150 | ▶ | sub http_date { | ||||||||||||||||||||||||||
|
Subroutine
http_date: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 197 | 150 | my $t = shift; | |||||||||||||||||||||||||||
| 198 | 150 | my @date = gmtime($t); | |||||||||||||||||||||||||||
| 199 | 150 | return sprintf( "%s, %02d %s %04d %02d:%02d:%02d GMT" | |||||||||||||||||||||||||||
| 200 | , $week[$date[6]], $date[3], $month[$date[4]], 1900+$date[5], @date[2,1,0]); | ||||||||||||||||||||||||||||
| 201 | }; | ||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||
| 203 | =head2 make_getters | ||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||
| 205 | Create dumb accessors in the calling class from hash. | ||||||||||||||||||||||||||||
| 206 | Keys are method names. | ||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||
| 208 | Key in the object is hash value if it's an identifier, | ||||||||||||||||||||||||||||
| 209 | or just method name otherwise: | ||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||
| 211 | package My::Class; | ||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||
| 213 | # (declare constructor somehow) | ||||||||||||||||||||||||||||
| 214 | make_getters ( | ||||||||||||||||||||||||||||
| 215 | foo => bar, | ||||||||||||||||||||||||||||
| 216 | baz => 1, | ||||||||||||||||||||||||||||
| 217 | quux => '', | ||||||||||||||||||||||||||||
| 218 | ); | ||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||
| 220 | # ... | ||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||
| 222 | my $obj = My::Class->new; | ||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||
| 224 | $obj->foo; # {bar} | ||||||||||||||||||||||||||||
| 225 | $obj->baz; # {baz} | ||||||||||||||||||||||||||||
| 226 | $obj->quux; # {quux} | ||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||
| 228 | =cut | ||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||
| 230 | # TODO 0.30 use Class::XSAccessor or smth | ||||||||||||||||||||||||||||
| 231 | 94 | ▶ | sub make_getters { | ||||||||||||||||||||||||||
|
Subroutine
make_getters: called CC=4 SCAR=14.0
Pod
documented
| |||||||||||||||||||||||||||||
| 232 | 94 | my %which = @_; | |||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||
| 234 | 94 | my $pkg = caller; | |||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||
| 236 | 94 | foreach (keys %which) { | |||||||||||||||||||||||||||
| 237 | 1598 | my $method = $_; | |||||||||||||||||||||||||||
| 238 | 1598 | my $key = $which{$method}; | |||||||||||||||||||||||||||
| 239 | 1598 | ▶ | $key = $method unless defined $key and $key =~ /^[a-z_][a-z_0-9]*$/i; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $key and $key =~ /^[a-z_][a-z_0-9]*$/i
MC/DC0%0/2
defined $key and $key =~ /^[a-z_][a-z_0-9]*$/i
defined $key
$key =~ /^[a-z_][a-z_0-9]*$/i
Truth table33%1/3
A and B
A= defined $keyB= $key =~ /^[a-z_][a-z_0-9]*$/i
| |||||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||
| 241 | 2756 | ▶ | my $sub = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 242 | 2756 | $_[0]->{$key}; | |||||||||||||||||||||||||||
| 243 | 1598 | }; | |||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||
| 245 | 109 | ▶ | use warnings FATAL => 'all'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 246 | 109 | ▶ | no strict 'refs'; ## no critic | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||
| 248 | 1598 | *{ $pkg."::".$method } = $sub; | |||||||||||||||||||||||||||
| 249 | }; | ||||||||||||||||||||||||||||
| 250 | }; | ||||||||||||||||||||||||||||
| 251 | |||||||||||||||||||||||||||||
| 252 | =head2 maybe_list | ||||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||
| 254 | maybe_list( $value, @defaults ) | ||||||||||||||||||||||||||||
| 255 | |||||||||||||||||||||||||||||
| 256 | If C<$value> is C<undef>, return a copy of \@defaults. | ||||||||||||||||||||||||||||
| 257 | |||||||||||||||||||||||||||||
| 258 | If C<$value> is a list, return a copy of it. | ||||||||||||||||||||||||||||
| 259 | |||||||||||||||||||||||||||||
| 260 | Otherwise, return C<[ $value ]>. | ||||||||||||||||||||||||||||
| 261 | |||||||||||||||||||||||||||||
| 262 | =cut | ||||||||||||||||||||||||||||
| 263 | |||||||||||||||||||||||||||||
| 264 | 508 | ▶ | sub maybe_list { | ||||||||||||||||||||||||||
|
Subroutine
maybe_list: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||
| 265 | 508 | my $item = shift; | |||||||||||||||||||||||||||
| 266 | |||||||||||||||||||||||||||||
| 267 | 508 | ▶ | confess "Useless use of maybe_list in void context, file a bug in NEAF" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 268 | unless defined wantarray; | ||||||||||||||||||||||||||||
| 269 | |||||||||||||||||||||||||||||
| 270 | 508 | ▶ | my @ret = defined $item ? ( | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 271 | ref $item eq 'ARRAY' ? @$item : ($item) | ||||||||||||||||||||||||||||
| 272 | ) : @_; | ||||||||||||||||||||||||||||
| 273 | |||||||||||||||||||||||||||||
| 274 | 508 | ▶ | return wantarray ? @ret : \@ret; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 275 | }; | ||||||||||||||||||||||||||||
| 276 | |||||||||||||||||||||||||||||
| 277 | =head2 path_prefixes ($path) | ||||||||||||||||||||||||||||
| 278 | |||||||||||||||||||||||||||||
| 279 | List ('', '/foo', '/foo/bar') for '/foo/bar' | ||||||||||||||||||||||||||||
| 280 | |||||||||||||||||||||||||||||
| 281 | =cut | ||||||||||||||||||||||||||||
| 282 | |||||||||||||||||||||||||||||
| 283 | 302 | ▶ | sub path_prefixes { | ||||||||||||||||||||||||||
|
Subroutine
path_prefixes: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 284 | 302 | my ($str, $rev) = @_; | |||||||||||||||||||||||||||
| 285 | |||||||||||||||||||||||||||||
| 286 | 302 | $str =~ s#^/*##; | |||||||||||||||||||||||||||
| 287 | 302 | $str =~ s#/+$##; | |||||||||||||||||||||||||||
| 288 | 302 | my @dir = split qr#/+#, $str; | |||||||||||||||||||||||||||
| 289 | 302 | my @ret = (''); | |||||||||||||||||||||||||||
| 290 | 302 | my $temp = ''; | |||||||||||||||||||||||||||
| 291 | |||||||||||||||||||||||||||||
| 292 | 302 | push @ret, $temp .= "/$_" for @dir; | |||||||||||||||||||||||||||
| 293 | |||||||||||||||||||||||||||||
| 294 | 302 | return @ret; | |||||||||||||||||||||||||||
| 295 | }; | ||||||||||||||||||||||||||||
| 296 | |||||||||||||||||||||||||||||
| 297 | =head2 rex( $string || qr/r.e.g.e.x/ ) | ||||||||||||||||||||||||||||
| 298 | |||||||||||||||||||||||||||||
| 299 | Convert string or regex to an I<anchored> regex. | ||||||||||||||||||||||||||||
| 300 | |||||||||||||||||||||||||||||
| 301 | =cut | ||||||||||||||||||||||||||||
| 302 | |||||||||||||||||||||||||||||
| 303 | 4 | ▶ | sub rex ($) { ## no critic | ||||||||||||||||||||||||||
|
Subroutine
rex: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||||||||||
| 304 | 4 | my $in = shift; | |||||||||||||||||||||||||||
| 305 | 4 | ▶ | $in = '' unless defined $in; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 306 | 4 | return qr/^$in$/; | |||||||||||||||||||||||||||
| 307 | }; | ||||||||||||||||||||||||||||
| 308 | |||||||||||||||||||||||||||||
| 309 | =head2 run_all( [CODE, ...], @args ) | ||||||||||||||||||||||||||||
| 310 | |||||||||||||||||||||||||||||
| 311 | Run all subroutines in array. Exceptions not handled. Return nothing. | ||||||||||||||||||||||||||||
| 312 | |||||||||||||||||||||||||||||
| 313 | =cut | ||||||||||||||||||||||||||||
| 314 | |||||||||||||||||||||||||||||
| 315 | 26 | ▶ | sub run_all { | ||||||||||||||||||||||||||
|
Subroutine
run_all: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 316 | 26 | my $list = shift; | |||||||||||||||||||||||||||
| 317 | |||||||||||||||||||||||||||||
| 318 | 26 | foreach my $sub (@$list) { | |||||||||||||||||||||||||||
| 319 | 37 | $sub->(@_); | |||||||||||||||||||||||||||
| 320 | }; | ||||||||||||||||||||||||||||
| 321 | 24 | return; | |||||||||||||||||||||||||||
| 322 | }; | ||||||||||||||||||||||||||||
| 323 | |||||||||||||||||||||||||||||
| 324 | =head2 run_all_nodie( [CODE, ...], $on_error, @args ) | ||||||||||||||||||||||||||||
| 325 | |||||||||||||||||||||||||||||
| 326 | Run all subroutines in array, even if some die. | ||||||||||||||||||||||||||||
| 327 | |||||||||||||||||||||||||||||
| 328 | Execute on_error in such cases. | ||||||||||||||||||||||||||||
| 329 | |||||||||||||||||||||||||||||
| 330 | Return number of failed callbacks. | ||||||||||||||||||||||||||||
| 331 | |||||||||||||||||||||||||||||
| 332 | =cut | ||||||||||||||||||||||||||||
| 333 | |||||||||||||||||||||||||||||
| 334 | 13 | ▶ | sub run_all_nodie { | ||||||||||||||||||||||||||
|
Subroutine
run_all_nodie: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 335 | 13 | my ($list, $on_error, @args) = @_; | |||||||||||||||||||||||||||
| 336 | |||||||||||||||||||||||||||||
| 337 | 13 | my $dead = 0; | |||||||||||||||||||||||||||
| 338 | 13 | foreach my $sub (@$list) { | |||||||||||||||||||||||||||
| 339 | 21 | ▶ | eval { $sub->(@args); 1; } and next; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 340 | 1 | $dead++; | |||||||||||||||||||||||||||
| 341 | 1 | $on_error->( $@ ); | |||||||||||||||||||||||||||
| 342 | }; | ||||||||||||||||||||||||||||
| 343 | |||||||||||||||||||||||||||||
| 344 | 13 | return $dead; | |||||||||||||||||||||||||||
| 345 | }; | ||||||||||||||||||||||||||||
| 346 | |||||||||||||||||||||||||||||
| 347 | =head2 supported_methods | ||||||||||||||||||||||||||||
| 348 | |||||||||||||||||||||||||||||
| 349 | =cut | ||||||||||||||||||||||||||||
| 350 | |||||||||||||||||||||||||||||
| 351 | # TODO 0.90 configurable or somthing | ||||||||||||||||||||||||||||
| 352 | 109 | @MVC::Neaf::supported_methods = qw( GET HEAD POST PATCH PUT DELETE ); | |||||||||||||||||||||||||||
| 353 | 198 | ▶ | sub supported_methods { | ||||||||||||||||||||||||||
|
Subroutine
supported_methods: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 354 | 198 | return @MVC::Neaf::supported_methods; | |||||||||||||||||||||||||||
| 355 | }; | ||||||||||||||||||||||||||||
| 356 | |||||||||||||||||||||||||||||
| 357 | =head2 JSON() | ||||||||||||||||||||||||||||
| 358 | |||||||||||||||||||||||||||||
| 359 | Because JSON::MaybeXS is not available on all systems, try to load it | ||||||||||||||||||||||||||||
| 360 | or emulate it. | ||||||||||||||||||||||||||||
| 361 | |||||||||||||||||||||||||||||
| 362 | =head2 encode_json | ||||||||||||||||||||||||||||
| 363 | |||||||||||||||||||||||||||||
| 364 | =head2 decode_json | ||||||||||||||||||||||||||||
| 365 | |||||||||||||||||||||||||||||
| 366 | These two are reexported from whatever JSON module we were lucky enough | ||||||||||||||||||||||||||||
| 367 | to load. | ||||||||||||||||||||||||||||
| 368 | |||||||||||||||||||||||||||||
| 369 | =cut | ||||||||||||||||||||||||||||
| 370 | |||||||||||||||||||||||||||||
| 371 | sub JSON(); ## no critic | ||||||||||||||||||||||||||||
| 372 | |||||||||||||||||||||||||||||
| 373 | 109 | ▶ | my $luck = eval "use JSON::MaybeXS; 1"; ## no critic | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 374 | 109 | my $err = $@; | |||||||||||||||||||||||||||
| 375 | |||||||||||||||||||||||||||||
| 376 | 109 | ▶ | if (!$luck) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 377 | 0 | require JSON::PP; | |||||||||||||||||||||||||||
| 378 | 0 | JSON::PP->import; | |||||||||||||||||||||||||||
| 379 | 0 | *JSON = sub () { "JSON::PP" }; | |||||||||||||||||||||||||||
| 380 | }; | ||||||||||||||||||||||||||||
| 381 | |||||||||||||||||||||||||||||
| 382 | =head2 data_fh($n) | ||||||||||||||||||||||||||||
| 383 | |||||||||||||||||||||||||||||
| 384 | Get C<DATA> filehandle in the calling package $n levels up the stack, | ||||||||||||||||||||||||||||
| 385 | together with the file name (so that we don't read the same __DATA__ twice). | ||||||||||||||||||||||||||||
| 386 | |||||||||||||||||||||||||||||
| 387 | =cut | ||||||||||||||||||||||||||||
| 388 | |||||||||||||||||||||||||||||
| 389 | 174 | ▶ | sub data_fh { | ||||||||||||||||||||||||||
|
Subroutine
data_fh: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 390 | 174 | my $n = shift; | |||||||||||||||||||||||||||
| 391 | |||||||||||||||||||||||||||||
| 392 | 174 | my @caller = caller($n); | |||||||||||||||||||||||||||
| 393 | |||||||||||||||||||||||||||||
| 394 | 174 | my $fh = do { | |||||||||||||||||||||||||||
| 395 | 109 | ▶ | no strict 'refs'; ## no critic | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 396 | 109 | ▶ | no warnings 'once'; ## no critic | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 397 | 174 | \*{ $caller[0].'::DATA' }; | |||||||||||||||||||||||||||
| 398 | }; | ||||||||||||||||||||||||||||
| 399 | 174 | ▶ | return unless openhandle $fh and !eof $fh; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
&openhandle($fh) and not eof $fh
MC/DC50%1/2
&openhandle($fh) and not eof $fh
&openhandle($fh)
not eof $fh
Truth table66%2/3
A and not(B)
A= &openhandle($fh)B= not eof $fh
| |||||||||||||||||||||||||||||
| 400 | |||||||||||||||||||||||||||||
| 401 | 3 | return ($caller[1], $fh); | |||||||||||||||||||||||||||
| 402 | }; | ||||||||||||||||||||||||||||
| 403 | |||||||||||||||||||||||||||||
| 404 | =head2 bare_html_escape( $dangerous ) | ||||||||||||||||||||||||||||
| 405 | |||||||||||||||||||||||||||||
| 406 | A crude html-entities escaper. | ||||||||||||||||||||||||||||
| 407 | Should be replaced by something real. | ||||||||||||||||||||||||||||
| 408 | |||||||||||||||||||||||||||||
| 409 | =cut | ||||||||||||||||||||||||||||
| 410 | |||||||||||||||||||||||||||||
| 411 | # TODO 0.40 replace with a normal module | ||||||||||||||||||||||||||||
| 412 | 109 | my %entity = ( | |||||||||||||||||||||||||||
| 413 | '&' => '&', | ||||||||||||||||||||||||||||
| 414 | '<' => '<', | ||||||||||||||||||||||||||||
| 415 | '>' => '>', | ||||||||||||||||||||||||||||
| 416 | '"' => '"', | ||||||||||||||||||||||||||||
| 417 | ); | ||||||||||||||||||||||||||||
| 418 | 109 | my $entity_rex = qr([&<>"]); | |||||||||||||||||||||||||||
| 419 | 3 | ▶ | sub bare_html_escape { | ||||||||||||||||||||||||||
|
Subroutine
bare_html_escape: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 420 | 3 | my $str = shift; | |||||||||||||||||||||||||||
| 421 | 3 | $str =~ s/($entity_rex)/$entity{$1}/g; | |||||||||||||||||||||||||||
| 422 | 3 | return $str; | |||||||||||||||||||||||||||
| 423 | }; | ||||||||||||||||||||||||||||
| 424 | |||||||||||||||||||||||||||||
| 425 | =head1 LICENSE AND COPYRIGHT | ||||||||||||||||||||||||||||
| 426 | |||||||||||||||||||||||||||||
| 427 | This module is part of L<MVC::Neaf> suite. | ||||||||||||||||||||||||||||
| 428 | |||||||||||||||||||||||||||||
| 429 | Copyright 2016-2023 Konstantin S. Uvarin C<khedin@cpan.org>. | ||||||||||||||||||||||||||||
| 430 | |||||||||||||||||||||||||||||
| 431 | This program is free software; you can redistribute it and/or modify it | ||||||||||||||||||||||||||||
| 432 | under the terms of either: the GNU General Public License as published | ||||||||||||||||||||||||||||
| 433 | by the Free Software Foundation; or the Artistic License. | ||||||||||||||||||||||||||||
| 434 | |||||||||||||||||||||||||||||
| 435 | See L<http://dev.perl.org/licenses/> for more information. | ||||||||||||||||||||||||||||
| 436 | |||||||||||||||||||||||||||||
| 437 | =cut | ||||||||||||||||||||||||||||
| 438 | |||||||||||||||||||||||||||||
| 439 | 109 | 1; | |||||||||||||||||||||||||||