/usr/share/perl5/Class/Accessor/Lite.pm is in libclass-accessor-lite-perl 0.5-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 | package Class::Accessor::Lite;
use strict;
our $VERSION = '0.05';
use Carp ();
sub import {
shift;
my %args = @_;
my $pkg = caller(0);
my %key_ctor = (
rw => \&_mk_accessors,
ro => \&_mk_ro_accessors,
wo => \&_mk_wo_accessors,
);
for my $key (sort keys %key_ctor) {
if (defined $args{$key}) {
Carp::croak "value of the '$key' parameter should be an arrayref"
unless ref($args{$key}) eq 'ARRAY';
$key_ctor{$key}->($pkg, @{$args{$key}});
}
}
_mk_new($pkg)
if $args{new};
1;
}
sub mk_new_and_accessors {
(undef, my @properties) = @_;
my $pkg = caller(0);
_mk_new($pkg);
_mk_accessors($pkg, @properties);
}
sub mk_new {
my $pkg = caller(0);
_mk_new($pkg);
}
sub mk_accessors {
(undef, my @properties) = @_;
my $pkg = caller(0);
_mk_accessors($pkg, @properties);
}
sub mk_ro_accessors {
(undef, my @properties) = @_;
my $pkg = caller(0);
_mk_ro_accessors($pkg, @properties);
}
sub mk_wo_accessors {
(undef, my @properties) = @_;
my $pkg = caller(0);
_mk_wo_accessors($pkg, @properties);
}
sub _mk_new {
my $pkg = shift;
no strict 'refs';
*{$pkg . '::new'} = __m_new($pkg);
}
sub _mk_accessors {
my $pkg = shift;
no strict 'refs';
for my $n (@_) {
*{$pkg . '::' . $n} = __m($n);
}
}
sub _mk_ro_accessors {
my $pkg = shift;
no strict 'refs';
for my $n (@_) {
*{$pkg . '::' . $n} = __m_ro($pkg, $n);
}
}
sub _mk_wo_accessors {
my $pkg = shift;
no strict 'refs';
for my $n (@_) {
*{$pkg . '::' . $n} = __m_wo($pkg, $n);
}
}
sub __m_new {
my $pkg = shift;
no strict 'refs';
return sub {
my $klass = shift;
bless {
(@_ == 1 && ref($_[0]) eq 'HASH' ? %{$_[0]} : @_),
}, $klass;
};
}
sub __m {
my $n = shift;
sub {
return $_[0]->{$n} if @_ == 1;
return $_[0]->{$n} = $_[1] if @_ == 2;
shift->{$n} = \@_;
};
}
sub __m_ro {
my ($pkg, $n) = @_;
sub {
if (@_ == 1) {
return $_[0]->{$n} if @_ == 1;
} else {
my $caller = caller(0);
Carp::croak("'$caller' cannot access the value of '$n' on objects of class '$pkg'");
}
};
}
sub __m_wo {
my ($pkg, $n) = @_;
sub {
if (@_ == 1) {
my $caller = caller(0);
Carp::croak( "'$caller' cannot alter the value of '$n' on objects of class '$pkg'")
} else {
return $_[0]->{$n} = $_[1] if @_ == 2;
shift->{$n} = \@_;
}
};
}
1;
__END__
=head1 NAME
Class::Accessor::Lite - a minimalistic variant of Class::Accessor
=head1 SYNOPSIS
package MyPackage;
use Class::Accessor::Lite (
new => 1,
rw => [ qw(foo bar) ],
ro => [ qw(baz) ],
wo => [ qw(hoge) ],
);
=head1 DESCRIPTION
The module is a variant of C<Class::Accessor>. It is fast and requires less typing, has no dependencies to other modules, and does not mess up the @ISA.
=head1 THE USE STATEMENT
The use statement (i.e. the C<import> function) of the module takes a single hash as an argument that specifies the types and the names of the properties. Recognises the following keys.
=over 4
=item new => $true_or_false
the default constructor is created if the value evaluates to true, otherwise nothing is done (the default behaviour)
=item rw => \@name_of_the_properties
creates a read / write accessor for the name of the properties passed through as an arrayref
=item ro => \@name_of_the_properties
creates a write-only accessor for the name of the properties passed through as an arrayref
=item rw => \@name_of_the_properties
creates a read-only accessor for the name of the properties passed through as an arrayref
=back
For more detailed explanation read the following section describing the behaviour of each function that actually creates the accessors.
=head1 FUNCTIONS
As of version 0.04 the properties can be specified as the arguments to the C<use> statement (as can be seen in the SYNOPSIS) which is now the recommended way of using the module, but for compatibility the following functions are provided as well.
=head2 Class::Accessor::Lite->mk_accessors(@name_of_the_properties)
Creates an accessor in current package under the name specified by the arguments that access the properties (of a hashref) with the same name.
=head2 Class::Accessor::Lite->mk_ro_accessors(@name_of_the_properties)
Same as mk_accessors() except it will generate read-only accessors (i.e. true accessors). If you attempt to set a value with these accessors it will throw an exception.
=head2 Class::Accessor::Lite->mk_wo_accessors(@name_of_the_properties)
Same as mk_accessors() except it will generate write-only accessors (i.e. mutators). If you attempt to read a value with these accessors it will throw an exception.
=head2 Class::Accessor::Lite->mk_new()
Creates the C<new> function that accepts a hash or a hashref as the initial properties of the object.
=head2 Class::Accessor::Lite->mk_new_and_accessors(@name_of_the_properties)
DEPRECATED. Use the new "use Class::Accessor::Lite (...)" style.
=head1 FAQ
=head2 Can I use C<Class::Accessor::Lite> in an inherited module?
Yes in most cases, when the class object in the super class is implemeted using a hashref. However you _should_ _not_ create the constructor for the inherited class by calling C<Class::Accessor::Lite->new()> or by C<use Class::Accessor::Lite (new => 1). The only other thing that C<Class::Accessor::Lite> does is to set up the accessor functions for given property names through a blessed hashref.
=head2 What happens when passing more than one arguments to the accessor?
When the accessor built by Class::Accessor::Lite is given more than one arguments, a reference to the arguments will be saved as an arrayref. This behaviour might not be necessary but is implemented as is to maintain compatibility with L<Class::Accessor::Fast>.
my @data = (1, 2, 3);
$obj->someproperty(@data);
$obj->someproperty->[2]++; # $data[3] is incremented
In general, you should pass an arrayref to set an arrayref to a property.
my @data = (1, 2, 3);
$obj->someproperty([ @data ]); # save a copy using arrayref
$obj->someproper->[2]++; # @data is not modified
=head1 SEE ALSO
L<Class::Accessor>
L<Class::Accessor::Lite>
=head1 AUTHORS
Copyright (C) 2008 - 2010 Kazuho Oku
=head1 LICENSE
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.8.6 or, at your option, any later version of Perl 5 you may have available.
=cut
|