/usr/share/perl5/Method/Signatures/Signature.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 | package Method::Signatures::Signature;
use Carp;
use Mouse;
use Method::Signatures::Types;
use Method::Signatures::Parameter;
use Method::Signatures::Utils qw(new_ppi_doc sig_parsing_error DEBUG);
my $INF = ( 0 + "inf" ) == 0 ? 9e9999 : "inf";
has num_lines =>
is => 'rw',
isa => 'Int',
lazy => 1,
default => sub {
my $self = shift;
my $num =()= $self->signature_string =~ /\n/g;
return $num + 1;
};
# The unmodified, uncleaned up original signature for reference
has signature_string =>
is => 'ro',
isa => 'Str',
required => 1;
# Just the parameter part of the signature, no invocant
has parameter_string =>
is => 'ro',
isa => 'Str',
lazy => 1,
builder => '_build_parameter_string';
# The parsed Method::Signature::Parameter objects
has parameters =>
is => 'ro',
isa => 'ArrayRef[Method::Signatures::Parameter]',
lazy => 1,
builder => '_build_parameters';
has named_parameters =>
is => 'ro',
isa => 'ArrayRef[Method::Signatures::Parameter]',
default => sub { [] };
has positional_parameters =>
is => 'ro',
isa => 'ArrayRef[Method::Signatures::Parameter]',
default => sub { [] };
has optional_parameters =>
is => 'ro',
isa => 'ArrayRef[Method::Signatures::Parameter]',
default => sub { [] };
has optional_positional_parameters =>
is => 'ro',
isa => 'ArrayRef[Method::Signatures::Parameter]',
default => sub { [] };
has slurpy_parameters =>
is => 'ro',
isa => 'ArrayRef[Method::Signatures::Parameter]',
default => sub { [] };
has yadayada_parameters =>
is => 'ro',
isa => 'ArrayRef[Method::Signatures::Parameter]',
default => sub { [] };
sub num_named {
return scalar @{$_[0]->named_parameters};
}
sub num_positional {
return scalar @{$_[0]->positional_parameters};
}
sub num_optional {
return scalar @{$_[0]->optional_parameters};
}
sub num_optional_positional {
return scalar @{$_[0]->optional_positional_parameters};
}
sub num_slurpy {
return scalar @{$_[0]->slurpy_parameters};
}
sub num_yadayada {
return scalar @{$_[0]->yadayada_parameters};
}
# Anything we need to pull out before the invocant.
# Primary example would be the $orig for around modifiers in Moose/Mouse
has pre_invocant =>
is => 'rw',
isa => 'Maybe[Str]',
default => '';
has invocant =>
is => 'rw',
isa => 'Maybe[Str]',
default => '';
sub has_invocant {
return $_[0]->invocant ? 1 : 0;
}
# How big can @_ be?
has max_argv_size =>
is => 'rw',
isa => 'Int|Inf';
# The maximum logical arguments (name => value counts as one argument)
has max_args =>
is => 'rw',
isa => 'Int|Inf';
# A PPI::Document representing the list of parameters
has ppi_doc =>
is => 'ro',
isa => 'PPI::Document',
lazy => 1,
default => sub {
my $code = $_[0]->parameter_string;
return new_ppi_doc(\$code);
};
# If set, no checks will be done on the signature or parameters
has no_checks =>
is => 'rw',
isa => 'Bool',
default => 0;
sub BUILD {
my $self = shift;
for my $sig (@{$self->parameters}) {
# Handle "don't care" specifier
if ($sig->is_yadayada) {
push @{$self->slurpy_parameters}, $sig;
push @{$self->yadayada_parameters}, $sig;
next;
}
$sig->check($self) unless $self->no_checks;
push @{$self->named_parameters}, $sig if $sig->is_named;
push @{$self->positional_parameters}, $sig if $sig->is_positional;
push @{$self->optional_parameters}, $sig if $sig->is_optional;
push @{$self->optional_positional_parameters}, $sig
if $sig->is_optional and $sig->is_positional;
push @{$self->slurpy_parameters}, $sig if $sig->is_slurpy;
DEBUG( "sig: ", $sig );
}
$self->_calculate_max_args;
$self->check unless $self->no_checks;
return;
}
sub _calculate_max_args {
my $self = shift;
# If there's a slurpy argument, the max is infinity.
if( $self->num_slurpy ) {
$self->max_argv_size($INF);
$self->max_args($INF);
return;
}
$self->max_argv_size( ($self->num_named * 2) + $self->num_positional );
$self->max_args( $self->num_named + $self->num_positional );
return;
}
# Check the integrity of the signature as a whole
sub check {
my $self = shift;
# Check that slurpy arguments come at the end
if(
$self->num_slurpy &&
!($self->num_yadayada || $self->positional_parameters->[-1]->is_slurpy)
)
{
my $slurpy_param = $self->slurpy_parameters->[0];
sig_parsing_error("Slurpy parameter '@{[$slurpy_param->variable]}' must come at the end");
}
return 1;
}
sub _strip_ws {
$_[1] =~ s/^\s+//;
$_[1] =~ s/\s+$//;
}
my $IDENTIFIER = qr{ [^\W\d] \w* }x;
sub _build_parameter_string {
my $self = shift;
my $sig_string = $self->signature_string;
my $invocant;
# Extract an invocant, if one is present.
if ($sig_string =~ s{ ^ \s* (\$ $IDENTIFIER) \s* : \s* }{}x) {
$self->invocant($1);
}
# The siganture, minus the invocant, is just the list of parameters
return $sig_string;
}
sub _build_parameters {
my $self = shift;
my $param_string = $self->parameter_string;
return [] unless $param_string =~ /\S/;
my $ppi = $self->ppi_doc;
$ppi->prune('PPI::Token::Comment');
my $statement = $ppi->find_first("PPI::Statement");
sig_parsing_error("Could not understand parameter list specification: $param_string")
unless $statement;
my $token = $statement->first_token;
# Split the signature into parameters as tokens.
my @tokens_by_param = ([]);
do {
if( $token->class eq "PPI::Token::Operator" and $token->content eq ',' )
{
push @tokens_by_param, [];
}
else {
push @{$tokens_by_param[-1]}, $token;
}
# "Type: $arg" is interpreted by PPI as a label, which is lucky for us.
$token = $token->class eq 'PPI::Token::Label'
? $token->next_token : $token->next_sibling;
} while( $token );
# Turn those token sets into Parameter objects.
my $idx = 0;
my @params;
for my $tokens (@tokens_by_param) {
my $code = join '', map { $_->content } @$tokens;
next unless $code =~ /\S/;
DEBUG( "raw_parameter: $code\n" );
$self->_strip_ws($_) for ($code);
my $first_significant_token = _first_significant_token($tokens);
my $param = Method::Signatures::Parameter->new(
original_code => $code,
position => $idx,
first_line_number => $first_significant_token->line_number,
);
$idx++ if $param->is_positional;
push @params, $param;
}
return \@params;
}
sub _first_significant_token {
my $tokens = shift;
for my $token (@$tokens) {
return $token if $token->significant;
}
croak "No significant token found";
}
1;
|