/usr/share/perl5/Role/Commons.pm is in librole-commons-perl 0.104-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 | use 5.008;
use strict;
use warnings;
package Role::Commons;
use Carp qw[ carp croak ];
use Module::Runtime qw[ use_package_optimistically ];
use Moo::Role qw[];
use Types::TypeTiny qw[ HashLike ArrayLike ];
BEGIN {
$Role::Commons::AUTHORITY = 'cpan:TOBYINK';
$Role::Commons::VERSION = '0.104';
}
my @ALL = qw(
Authority
ObjectID
Tap
);
sub parse_arguments
{
my $class = shift;
# Translate "-all".
my $all = 0;
my @args = grep { /^\-all$/i ? do { $all++; 0 } : 1 } @_;
unshift @args, @ALL if $all;
my %roles;
my %options;
while (my $name = shift @args)
{
my $details;
if ($name =~ /^-/ or ref $args[0])
{ $details = shift @args }
if ($name =~ /^\-(.+)$/i)
{ $options{ lc $1 } = $details }
else
{ $roles{ $name } = $details }
}
carp "Role::Commons - no roles specified"
if keys %options && !keys %roles;
return(\%roles, \%options);
}
sub import
{
my $class = shift;
my ($roles, $options) = $class->parse_arguments(@_);
$options->{into} = caller unless exists $options->{into};
foreach my $role (sort keys %$roles)
{
use_package_optimistically( join q[::], $class, $role );
}
'Moo::Role'->apply_roles_to_package(
$options->{into},
map { join q[::], $class, $_ } sort keys %$roles,
);
foreach my $role (sort keys %$roles)
{
my $role_pkg = join q[::], $class, $role;
my $details = $roles->{$role};
my $setup_method = do {
no strict 'refs';
${"$role_pkg\::setup_for_class"};
} or next;
$role_pkg->$setup_method(
$options->{into},
HashLike->check($details)
? %$details
: ( ArrayLike->check($details) ? @$details : (option => $details) ),
);
}
}
sub apply_roles_to_object
{
my $class = shift unless blessed($_[0]);
my $object = shift;
my ($roles, $options) = $class->parse_arguments(@_);
foreach my $role (sort keys %$roles)
{
use_package_optimistically( join q[::], $class, $role );
}
'Moo::Role'->apply_roles_to_object(
$object,
map { join q[::], $class, $_ } sort keys %$roles,
);
foreach my $role (sort keys %$roles)
{
my $role_pkg = join q[::], $class, $role;
my $details = $roles->{$role};
my $setup_method = do {
no strict 'refs';
${"$role_pkg\::setup_for_class"};
} || sub { 0 };
$role_pkg->$setup_method(
ref($object),
HashLike->check($details)
? %$details
: ( ArrayLike->check($details) ? @$details : (option => $details) ),
);
$setup_method = do {
no strict 'refs';
${"$role_pkg\::setup_for_object"};
} or next;
$role_pkg->$setup_method(
$object,
HashLike->check($details)
? %$details
: ( ArrayLike->check($details) ? @$details : (option => $details) ),
);
}
}
1;
__END__
=head1 NAME
Role::Commons - roles that can be commonly used, for the mutual benefit of all
=head1 SYNOPSIS
use 5.010;
{
package Local::Class;
use Moo;
use Role::Commons -all;
our $AUTHORITY = 'cpan:JOEBLOGGS';
our $VERSION = '1.000';
}
say Local::Class->AUTHORITY
if Local::Class->DOES('Role::Commons::Authority');
my $obj = Local::Class->new;
say $obj->object_id
if $obj->DOES('Role::Commons::ObjectID');
=head1 DESCRIPTION
Role-Commons is not yet another implementation of roles. It is a collection
of generic, reusable roles that hopefully you will love to apply to your
classes. These roles are built using L<Moo::Role>, so automatically
integrate into the L<Moose> object system if you're using it, but they
do not require Moose.
The Role::Commons module itself provides shortcuts for applying roles to
your package, so that instead of doing:
{
package Local::Class;
use Moo; # or "use Moose"
with qw( Role::Commons::Authority Role::Commons::ObjectID );
}
You can just do this:
{
package Local::Class;
use Moo;
use Role::Commons qw( Authority ObjectID );
}
It also handles passing some simple parameters through to the role
from the consuming class. (Because Moo doesn't have anything like
L<MooseX::Role::Parameterized>.)
=begin trustme
Not sure if these should be documented or not...
=item parse_arguments
=item apply_roles_to_object
=end trustme
=head2 Roles
=over
=item L<Role::Commons::Authority>
Sets up a C<AUTHORITY> method for your class which is conceptually a
little like C<VERSION>.
=item L<Role::Commons::ObjectID>
Provides an C<object_id> method for your class which returns a unique
identifier for each object.
=item L<Role::Commons::Tap>
Provides a C<tap> method for your class, inspired by Ruby's method
of the same name. Helpful for writing chained method calls.
=back
=head2 Obsolescence
Role-Commons is the successor for my older projects:
authority-shared,
Object-AUTHORITY,
Object-DOES,
Object-Role, and
Object-Tap.
Role-Commons bundles L<Object::AUTHORITY> for the sake of backwards
compatibility. This is being phased out.
=head1 BUGS
Please report any bugs to
L<http://rt.cpan.org/Dist/Display.html?Queue=Role-Commons>.
=head1 SEE ALSO
L<Role::Commons::Authority>,
L<Role::Commons::ObjectID>,
L<Role::Commons::Tap>.
L<Role::Tiny>,
L<Moo::Role>,
L<Moose::Role>.
=head1 AUTHOR
Toby Inkster E<lt>tobyink@cpan.orgE<gt>.
=head1 COPYRIGHT AND LICENCE
This software is copyright (c) 2012, 2014 by Toby Inkster.
This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.
=head1 DISCLAIMER OF WARRANTIES
THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
|