/usr/share/perl5/Method/Signatures/Parameter.pm is in libmethod-signatures-perl 20160315-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 | package Method::Signatures::Parameter;
use Mouse;
use Carp;
use Method::Signatures::Utils;
my $IDENTIFIER = qr{ [^\W\d] \w* }x;
my $VARIABLE = qr{ [\$\@%] $IDENTIFIER }x;
my $TYPENAME = qr{ $IDENTIFIER (?: \:\: $IDENTIFIER )* }x;
our $PARAMETERIZED;
$PARAMETERIZED = do{ use re 'eval';
qr{ $TYPENAME (?: \[ (??{$PARAMETERIZED}) \] )? }x;
};
my $TYPESPEC = qr{ ^ \s* $PARAMETERIZED (?: \s* \| \s* $PARAMETERIZED )* \s* }x;
has original_code =>
is => 'ro',
isa => 'Str',
required => 1;
# Note: Have to preparse with regexes up to traits
# because :, ! and ? in sigs confuse PPI
has ppi_clean_code =>
is => 'rw',
isa => 'Str',
;
has is_yadayada =>
is => 'ro',
isa => 'Bool',
lazy => 1,
default => sub {
my $self = shift;
return $self->original_code =~ m{^ \s* \Q...\E \s* $}x;
};
has type =>
is => 'rw',
isa => 'Str',
default => '';
;
has is_ref_alias =>
is => 'rw',
isa => 'Bool',
default => 0;
has is_named =>
is => 'rw',
isa => 'Bool',
;
sub is_positional {
my $self = shift;
return !$self->is_named;
}
has variable =>
is => 'rw',
isa => 'Str',
default => '';
has first_line_number =>
is => 'rw',
isa => 'Int';
has position =>
is => 'rw',
isa => 'Maybe[Int]', # XXX 0 or positive int
trigger => sub {
my($self, $new_position, $old_position) = @_;
if( $self->is_named ) {
croak("A named parameter cannot have a position")
if defined $new_position and length $new_position;
}
else { # positional parameter
croak("A positional parameter must have a position")
if !(defined $new_position and length $new_position);
}
};
has sigil =>
is => 'rw',
isa => 'Str', # XXX [%$@*]
;
has variable_name =>
is => 'rw',
isa => 'Str',
;
has where =>
is => 'rw',
isa => 'ArrayRef',
default => sub { [] };
sub has_where {
my $self = shift;
return @{$self->where} ? 1 : 0;
}
has traits =>
is => 'rw',
isa => 'HashRef[Int]',
default => sub { {} };
sub has_traits {
my $self = shift;
return keys %{$self->traits} ? 1 : 0;
}
has default =>
is => 'rw',
isa => 'Maybe[Str]'
;
has default_when =>
is => 'rw',
isa => 'Str',
;
has passed_in =>
is => 'rw',
isa => 'Str',
;
has check_exists =>
is => 'rw',
isa => 'Str'
;
has is_slurpy =>
is => 'ro',
isa => 'Bool',
lazy => 1,
default => sub {
my $self = shift;
return 0 if $self->is_ref_alias;
return 0 if !$self->sigil;
return $self->sigil =~ m{ ^ [%\@] $ }x;
};
has is_at_underscore =>
is => 'ro',
isa => 'Bool',
lazy => 1,
default => sub {
my $self = shift;
return $self->variable eq '@_';
};
has required_flag =>
is => 'rw',
isa => 'Str',
default => '';
has is_required =>
is => 'rw',
isa => 'Bool',
;
# A PPI::Document representing the parameter
has ppi_doc =>
is => 'ro',
isa => 'PPI::Document',
lazy => 1,
default => sub {
my $code = $_[0]->ppi_clean_code;
return new_ppi_doc(\$code);
};
sub is_optional {
my $self = shift;
return !$self->is_required;
}
sub BUILD {
my $self = shift;
return if $self->is_yadayada;
$self->_preparse_original_code_for_ppi;
$self->_parse_with_ppi;
$self->_init_split_variable;
$self->_init_is_required;
return;
}
sub _init_is_required {
my $self = shift;
$self->is_required( $self->_determine_is_required );
}
sub _determine_is_required {
my $self = shift;
return 1 if $self->required_flag eq '!';
return 0 if $self->required_flag eq '?';
return 0 if $self->has_default;
return 0 if $self->is_named;
return 0 if $self->is_slurpy;
return 1;
}
sub has_default {
my $self = shift;
return defined $self->default;
}
sub _parse_with_ppi {
my $self = shift;
# Nothing to parse.
return if $self->ppi_clean_code !~ /\S/;
# Replace parameter var so as not to confuse PPI...
$self->ppi_clean_code($self->variable. " " .$self->ppi_clean_code);
# Tokenize...
my $components = $self->ppi_doc;
my $statement = $components->find_first("PPI::Statement")
or sig_parsing_error("Could not understand parameter specification: @{[$self->ppi_clean_code]}");
my $tokens = [ $statement->children ];
# Re-remove parameter var
shift @$tokens;
# Extract any 'where' constraints...
while ($self->_extract_leading(qr{^ where $}x, $tokens)) {
sig_parsing_error("'where' constraint only available under Perl 5.10 or later. Error")
if $] < 5.010;
push @{$self->where}, $self->_extract_until(qr{^ (?: where | is | = | //= ) $}x, $tokens);
}
# Extract parameter traits...
while ($self->_extract_leading(qr{^ is $}x, $tokens)) {
$self->traits->{ $self->_extract_leading(qr{^ \S+ $}x, $tokens) }++;
}
# Extract normal default specifier (if any)...
if ($self->_extract_leading(qr{^ = $}x, $tokens)) {
$self->default( $self->_extract_until(qr{^ when $}x, $tokens) );
# Extract 'when' modifier (if any)...
if ($self->_extract_leading(qr{^ when $}x, $tokens)) {
sig_parsing_error("'when' modifier on default only available under Perl 5.10 or later. Error")
if $] < 5.010;
$self->default_when( join(q{}, @$tokens) );
$tokens = [];
}
}
# Otherwise, extract undef-default specifier (if any)...
elsif ($self->_extract_leading(qr{^ //= $}x, $tokens)) {
sig_parsing_error("'//=' defaults only available under Perl 5.10 or later. Error")
if $] < 5.010;
$self->default_when('undef');
$self->default( join(q{}, @$tokens) );
$tokens = [];
}
# Anything left over is an error...
elsif (my $trailing = $self->_extract_leading(qr{ \S }x, $tokens)) {
sig_parsing_error("Unexpected extra code after parameter specification: '",
$trailing . join(q{}, @$tokens), "'"
);
}
return;
}
# Remove leading whitespace + token, if token matches the specified pattern...
sub _extract_leading {
my ($self, $selector_pat, $tokens) = @_;
while (@$tokens && $tokens->[0]->class eq 'PPI::Token::Whitespace') {
shift @$tokens;
}
return @$tokens && $tokens->[0] =~ $selector_pat
? "" . shift @$tokens
: undef;
}
# Remove tokens up to (but excluding) the first that matches the delimiter...
sub _extract_until {
my ($self, $delimiter_pat, $tokens) = @_;
my $extracted = q{};
while (@$tokens) {
last if $tokens->[0] =~ $delimiter_pat;
my $token = shift @$tokens;
# Flatten multi-line data structures into a single line which
# Devel::Declare can inject.
$token->prune(sub { !$_[1]->significant }) if $token->isa("PPI::Node");
$extracted .= $token;
}
return $extracted;
}
sub _preparse_original_code_for_ppi {
my $self = shift;
my $original_code = $self->original_code;
$self->type($1) if $original_code =~ s{^ ($TYPESPEC) \s+ }{}ox;
# Extract ref-alias & named-arg markers, param var, and required/optional marker...
$original_code =~ s{ ^ \s* ([\\:]*) \s* ($VARIABLE) \s* ([!?]?) }{}ox
or sig_parsing_error("Could not understand parameter specification: $original_code");
my ($premod, $var, $postmod) = ($1, $2, $3);
$self->is_ref_alias ($premod =~ m{ \\ }x);
$self->is_named ($premod =~ m{ : }x);
$self->required_flag($postmod) if $postmod;
$self->variable($var) if $var;
$self->ppi_clean_code($original_code);
return;
}
sub _init_split_variable {
my $self = shift;
$self->variable =~ /^(.) (.*)/x;
$self->sigil ($1);
$self->variable_name($2);
return;
}
# Check the integrity of one piece of the signature
sub check {
my($self, $signature) = @_;
if( $self->is_slurpy ) {
sig_parsing_error("Signature can only have one slurpy parameter")
if $signature->num_slurpy >= 1;
sig_parsing_error("Slurpy parameter '@{[$self->variable]}' cannot be named; use a reference instead")
if $self->is_named;
}
if( $self->is_named ) {
if( $signature->num_optional_positional ) {
my $pos_var = $signature->positional_parameters->[-1]->variable;
my $var = $self->variable;
sig_parsing_error("Named parameter '$var' mixed with optional positional '$pos_var'");
}
}
else { # is_positional
if( $signature->num_named ) {
my $named_var = $signature->named_parameters->[-1]->variable;
my $var = $self->variable;
sig_parsing_error("Positional parameter '$var' after named param '$named_var'");
}
# Required positional after an optional.
# Required positional after a slurpy will be handled elsewhere.
if( $self->is_required && $signature->num_optional_positional &&
!$signature->num_slurpy
) {
my $var = $self->variable;
my $opt_pos_var = $signature->optional_positional_parameters->[-1]
->variable;
sig_parsing_error("Required positional parameter '$var' cannot follow an optional positional parameter '$opt_pos_var'");
}
}
return 1;
}
1;
|