/usr/share/perl5/Object/Role.pm is in libobject-role-perl 0.001-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 | package Object::Role;
use 5.006;
use strict;
BEGIN {
$Object::Role::AUTHORITY = 'cpan:TOBYINK';
$Object::Role::VERSION = '0.001';
}
use Sub::Name qw/subname/;
BEGIN {
eval 'use Object::AUTHORITY; 1' or do {
*AUTHORITY = sub { $Object::Role::AUTHORITY }
}
}
sub parse_arguments
{
my (undef, $default, @args) = @_;
my %args;
while (defined(my $arg = shift @args))
{
if ($arg =~ /^-/)
{
$args{$arg} = shift @args;
}
else
{
push @{$args{$default}}, $arg;
}
}
my $caller = defined $args{-package} ? $args{-package} : caller(1);
return ($caller, %args);
}
sub install_method
{
my ($class, $subname, $coderef, $caller) = @_;
my $name = "$caller\::$subname";
no strict 'refs';
no warnings 'redefine';
*{$name} = subname($name, $coderef);
$class->register_consumer($caller)
unless $class->has_consumer($caller);
$class;
}
sub register_consumer
{
my ($class, $consumer) = @_;
no strict 'refs';
no warnings 'redefine';
push @{"$class\::CONSUMERS"}, $consumer;
$class;
}
sub has_consumer
{
my ($class, $consumer) = @_;
$consumer = ref($consumer) if ref($consumer);
no strict 'refs';
no warnings 'redefine';
foreach (@{"$class\::CONSUMERS"})
{
return $class if $_ eq $consumer;
}
return;
}
1;
__END__
=head1 NAME
Object::Role - base class for non-Moose roles
=head1 SYNOPSIS
{
package Object::Dumpable;
use base qw/Object::Role/;
use Data::Dumper;
sub import
{
my ($class, @args) = @_;
my ($caller, %args) = __PACKAGE__->parse_arguments(undef, @args);
my $coderef = sub
{
my ($self) = @_;
return Dumper($self);
};
__PACKAGE__->install_method(dump => $coderef, $caller);
}
}
{
package Foo;
use Object::Dumpable;
sub new { ... }
}
{
package main;
my $foo = Foo->new;
warn $foo->dump;
}
=head1 DESCRIPTION
This will be better documented once I fully understand it myself!
The idea of this is to be a base class for roles like L<Object::DOES>,
L<Object::Stash> and L<Object::ID>. It handles parsing of import arguments,
installing methods into the caller's namespace (like L<Exporter>, but using
a technique that is immune to L<namespace::autoclean>) and tracking which
packages have consumed your role.
While C<Object::Role> is a base class for roles, it is not itself a role,
so does not export anything. Instead, your role must inherit from it.
=head2 Methods
=over
=item C<< parse_arguments($default_arg_name, @arguments) >>
Will parse:
package My::Class;
use My::Role -foo => 1, -bar => [2,3], 4, 5;
as:
(
'My::Class', # caller,
(
'-foo' => [1],
'-bar' => [2, 3],
$default_arg_name => [4, 5],
)
)
=item C<< install_method($subname => $coderef, $package) >>
Installs $coderef as "$package\::$subname".
Automatically calls register_consumer($package).
=item C<< register_consumer($package) >>
Records that $package has consumed (used) your role.
=item C<< has_consumer($package) >>
Check if $package has consumed (used) your role.
=back
=head1 BUGS
Please report any bugs to
L<http://rt.cpan.org/Dist/Display.html?Queue=Object-Role>.
=head1 SEE ALSO
L<Object::DOES>, L<Object::AUTHORITY>.
=head1 AUTHOR
Toby Inkster E<lt>tobyink@cpan.orgE<gt>.
=head1 COPYRIGHT AND LICENCE
This software is copyright (c) 2011 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.
|