/usr/share/perl5/Method/Signatures/Simple.pm is in libmethod-signatures-simple-perl 1.07-1.
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 | package Method::Signatures::Simple;
{
$Method::Signatures::Simple::VERSION = '1.07';
}
use warnings;
use strict;
=head1 NAME
Method::Signatures::Simple - Basic method declarations with signatures, without source filters
=head1 VERSION
version 1.07
=cut
use base 'Devel::Declare::MethodInstaller::Simple';
sub import {
my $class = shift;
my %opts = @_;
$opts{into} ||= caller;
my $meth = delete $opts{name} || delete $opts{method_keyword};
my $func = delete $opts{function_keyword};
# if no options are provided at all, then we supply defaults
unless (defined $meth || defined $func) {
$meth = 'method';
$func = 'func';
}
# we only install keywords that are requested
if (defined $meth) {
$class->install_methodhandler(
name => $meth,
invocant => '$self',
%opts,
);
}
if (defined $func) {
$class->install_methodhandler(
name => $func,
%opts,
invocant => undef,
);
}
}
sub strip_proto {
my $self = shift;
my ($proto) = $self->SUPER::strip_proto()
or return '';
# we strip comments and newlines here, and stash the number of newlines.
# we will re-inject the newlines in strip_attrs(), because DD does not
# like it when you inject them into the following code block. it does not
# object to tacking on newlines to the code attribute spec though.
# (see the call to inject_if_block() in DD::MethodInstaller::Simple->parser)
$proto =~ s/\s*#.*$//mg;
$self->{__nls} = $proto =~ s/[\r\n]//g;
$proto;
}
sub strip_attrs {
my $self = shift;
my ($attrs) = $self->SUPER::strip_attrs();
$attrs ||= '';
$attrs .= $/ x $self->{__nls} if $self->{__nls};
$attrs;
}
sub parse_proto {
my $self = shift;
my ($proto) = @_;
$proto ||= '';
$proto =~ s/\s*#.*$//mg;
$proto =~ s/^\s+//mg;
$proto =~ s/\s+$//mg;
$proto =~ s/[\r\n]//g;
my $invocant = $self->{invocant};
$invocant = $1 if $proto =~ s{(\$\w+)\s*:\s*}{};
my $inject = '';
$inject .= "my ${invocant} = shift;" if $invocant;
$inject .= "my ($proto) = \@_;" if defined $proto and length $proto;
$inject .= '();'; # fix for empty method body
return $inject;
}
=head1 SYNOPSIS
# -- a basic class -- #
package User;
use Method::Signatures::Simple;
method new ($class: $name, $email) {
my $user = {
id => new_id(42),
name => $name,
email => $email,
};
bless $user, $class;
}
func new_id ($seed) {
state $id = $seed;
$id++;
}
method name { $self->{name}; }
method email { $self->{email}; }
1;
# -- other features -- #
# attributes
method foo : lvalue { $self->{foo} }
# change invocant name
use Method::Signatures::Simple invocant => '$this';
method foo ($bar) { $this->bar($bar) }
method bar ($class: $bar) { $class->baz($bar) }
# use a different function keyword
use Method::Signatures::Simple function_keyword => 'fun';
fun triple ($num) { 3 * $num }
# use a different method keyword
use Method::Signatures::Simple method_keyword => 'action';
action foo { $self->bar }
=head1 RATIONALE
This module provides basic C<method> and C<func> keywords with simple
signatures. It's intentionally simple, and is supposed to be a stepping stone
for its bigger brothers L<MooseX::Method::Signatures> and
L<Method::Signatures>. It only has a small benefit over regular subs, so
if you want more features, look at those modules. But if you're looking
for a small amount of syntactic sugar, this might just be enough.
=head1 FEATURES
=over 4
=item * invocant
The C<method> keyword automatically injects the annoying C<my $self = shift;>
for you. You can rename the invocant with the first argument, followed by a
colon:
method ($this:) {}
method ($this: $that) {}
The C<func> keyword doesn't inject an invocant, but does do the signature
processing below:
func ($that) {}
=item * signature
The signature C<($sig)> is transformed into C<"my ($sig) = \@_;">. That way, we
mimic perl's usual argument handling.
method foo ($bar, $baz, %opts) {
func xyzzy ($plugh, @zorkmid) {
# becomes
sub foo {
my $self = shift;
my ($bar, $baz, %opts) = @_;
sub xyzzy {
my ($plugh, @zorkmid) = @_;
=back
=head1 ADVANCED CONFIGURATION
Since this module subclasses L<Devel::Declare::MethodInstaller::Simple>, you
can change the keywords and the default invocant with import arguments. These
changes affect the current scope.
=over 4
=item * change the invocant name
use Method::Signatures::Simple invocant => '$this';
method x { $this->{x} }
method y { $this->{y} }
# and this of course still works:
method z ($self:) { $self->{z} }
=item * change the keywords
You can install a different keyword (instead of the default 'method' and
'func'), by passing names to the C<use> line:
use Method::Signatures::Simple method_keyword => 'action',
function_keyword => 'thing';
action foo ($some, $args) { ... }
thing bar ($whatever) { ... }
One benefit of this is that you can use this module together with e.g.
L<MooseX::Declare>:
# untested
use MooseX::Declare;
class Foo {
use Method::Signatures::Simple method_keyword => 'routine';
method x (Int $x) { ... } # from MooseX::Method::Signatures
routine y ($y) { ... } # from this module
}
If you specify neither C<method_keyword> nor C<function_keyword>, then we
default to injecting C<method> and C<func>. If you only specify one of these
options, then we only inject that one keyword into your scope.
Examples:
# injects 'method' and 'func'
use Method::Signatures::Simple;
# only injects 'action'
use Method::Signatures::Simple method_keyword => 'action';
# only injects 'procedure'
use Method::Signatures::Simple function_keyword => 'procedure';
# injects 'action' and 'function'
use Method::Signatures::Simple method_keyword => 'action',
function_keyword => 'function';
=item * install several keywords
You're not limited to a single C<use> line, so you can install several keywords with the same
semantics as 'method' into the current scope:
use Method::Signatures::Simple; # provides 'method' and 'func'
use Method::Signatures::Simple method_keyword => 'action';
method x { ... }
func y { ... }
action z { ... }
=back
=begin pod-coverage
=over 4
=item parse_proto
Overridden.
=back
=end pod-coverage
=head1 AUTHOR
Rhesa Rozendaal, C<< <rhesa at cpan.org> >>
=head1 BUGS
Please report any bugs or feature requests to C<bug-method-signatures-simple at rt.cpan.org>, or through
the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Method-Signatures-Simple>. I will be notified, and then you'll
automatically be notified of progress on your bug as I make changes.
=head1 SUPPORT
You can find documentation for this module with the perldoc command.
perldoc Method::Signatures::Simple
You can also look for information at:
=over 4
=item * RT: CPAN's request tracker
L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Method-Signatures-Simple>
=item * AnnoCPAN: Annotated CPAN documentation
L<http://annocpan.org/dist/Method-Signatures-Simple>
=item * CPAN Ratings
L<http://cpanratings.perl.org/d/Method-Signatures-Simple>
=item * Search CPAN
L<http://search.cpan.org/dist/Method-Signatures-Simple>
=back
=head1 ACKNOWLEDGEMENTS
=over 4
=item * MSTROUT
For writing L<Devel::Declare> and providing the core concepts.
=item * MSCHWERN
For writing L<Method::Signatures> and publishing about it. This is what got my attention.
=item * FLORA
For helping me abstracting the Devel::Declare bits and suggesting improvements.
=item * CHIPS
For suggesting we add a 'func' keyword.
=back
=head1 SEE ALSO
L<Devel::Declare>, L<Method::Signatures>, L<MooseX::Method::Signatures>.
=head1 COPYRIGHT & LICENSE
Copyright 2011 Rhesa Rozendaal, all rights reserved.
This program is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.
=cut
1; # End of Method::Signatures::Simple
|