Репозиторий Sisyphus
Последнее обновление: 18 октября 2019 | Пакетов: 17501 | Посещений: 15289992
en ru br
Репозитории ALT

Группа :: Разработка/Perl
Пакет: perl-Class-Accessor-Lite

 Главная   Изменения   Спек   Патчи   Исходники   Загрузить   Gear   Bugs and FR  Repocop 

pax_global_header00006660000000000000000000000064114771053230014515gustar00rootroot0000000000000052 comment=e7998757377a90cacfe7a646877f2ec3bb77c14a
perl-Class-Accessor-Lite-0.05/000075500000000000000000000000001147710532300160775ustar00rootroot00000000000000perl-Class-Accessor-Lite-0.05/Changes000064400000000000000000000010101147710532300173620ustar00rootroot00000000000000Revision history for Perl extension Class::Accessor::Lite.

0.05
- improve usage error checking: assert that the accessor lists passed through the "import" function are arrayrefs

0.04
- add read-only and write-only accessor constructors (tokuhirom)
- the new "use Class::Accessor::Lite (rw => ...)" style

0.03
- add mk_new_and_accessors
- more documentation

0.02 Wed Nov 26 18:00:00 2008
- used to create accessor for different package in caller stack

0.01 Wed Nov 26 17:00:00 2008
- initial release
perl-Class-Accessor-Lite-0.05/MANIFEST.SKIP000064400000000000000000000003341147710532300177750ustar00rootroot00000000000000\bRCS\b
\bCVS\b
^MANIFEST\.
^Makefile$
~$
^#
\.old$
^blib/
^pm_to_blib
^MakeMaker-\d
\.gz$
\.cvsignore
^t/9\d_.*\.t
^t/perlcritic
^tools/
\.svn/
^[^/]+\.yaml$
^[^/]+\.pl$
^\.shipit$
\.git$
\.git/
nytprof/
\.rej$
\.orig$
perl-Class-Accessor-Lite-0.05/Makefile.PL000064400000000000000000000002601147710532300200470ustar00rootroot00000000000000use strict;
use inc::Module::Install;

name('Class-Accessor-Lite');
all_from('lib/Class/Accessor/Lite.pm');
readme_from('lib/Class/Accessor/Lite.pm');

auto_install;
WriteAll;
perl-Class-Accessor-Lite-0.05/README000064400000000000000000000101651147710532300167620ustar00rootroot00000000000000NAME
Class::Accessor::Lite - a minimalistic variant of Class::Accessor

SYNOPSIS
package MyPackage;

use Class::Accessor::Lite (
new => 1,
rw => [ qw(foo bar) ],
ro => [ qw(baz) ],
wo => [ qw(hoge) ],
);

DESCRIPTION
The module is a variant of "Class::Accessor". It is fast and requires
less typing, has no dependencies to other modules, and does not mess up
the @ISA.

THE USE STATEMENT
The use statement (i.e. the "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.

new => $true_or_false
the default constructor is created if the value evaluates to true,
otherwise nothing is done (the default behaviour)

rw => \@name_of_the_properties
creates a read / write accessor for the name of the properties
passed through as an arrayref

ro => \@name_of_the_properties
creates a write-only accessor for the name of the properties passed
through as an arrayref

rw => \@name_of_the_properties
creates a read-only accessor for the name of the properties passed
through as an arrayref

For more detailed explanation read the following section describing the
behaviour of each function that actually creates the accessors.

FUNCTIONS
As of version 0.04 the properties can be specified as the arguments to
the "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.

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.

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.

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.

Class::Accessor::Lite->mk_new()
Creates the "new" function that accepts a hash or a hashref as the
initial properties of the object.

Class::Accessor::Lite->mk_new_and_accessors(@name_of_the_properties)
DEPRECATED. Use the new "use Class::Accessor::Lite (...)" style.

FAQ
Can I use "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
"Class::Accessor::Lite-"new()> or by "use Class::Accessor::Lite (new ="
1). The only other thing that "Class::Accessor::Lite" does is to set up
the accessor functions for given property names through a blessed
hashref.

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 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

SEE ALSO
Class::Accessor

Class::Accessor::Lite

AUTHORS
Copyright (C) 2008 - 2010 Kazuho Oku

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.

perl-Class-Accessor-Lite-0.05/lib/000075500000000000000000000000001147710532300166455ustar00rootroot00000000000000perl-Class-Accessor-Lite-0.05/lib/Class/000075500000000000000000000000001147710532300177125ustar00rootroot00000000000000perl-Class-Accessor-Lite-0.05/lib/Class/Accessor/000075500000000000000000000000001147710532300214545ustar00rootroot00000000000000perl-Class-Accessor-Lite-0.05/lib/Class/Accessor/Lite.pm000064400000000000000000000152001147710532300227050ustar00rootroot00000000000000package 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

perl-Class-Accessor-Lite-0.05/t/000075500000000000000000000000001147710532300163425ustar00rootroot00000000000000perl-Class-Accessor-Lite-0.05/t/00-base.t000064400000000000000000000014601147710532300176570ustar00rootroot00000000000000package K;

use strict;
use warnings;

use Test::More tests => 14;

use_ok('Class::Accessor::Lite');

Class::Accessor::Lite->mk_accessors(qw(foo bar));
Class::Accessor::Lite->mk_ro_accessors(qw(ro));
Class::Accessor::Lite->mk_wo_accessors(qw(wo));

ok(! $@, 'call mk_accessors');

my $k = bless { foo => 1, bar => 2, ro => 3, wo => 4 }, 'K';
is($k->foo, 1);
is($k->foo(2), 2);
is($k->foo, 2);
is_deeply($k->foo(2, 3), [ 2, 3 ]);
is_deeply($k->foo, [ 2, 3 ]);
is($k->bar, 2);
is($k->ro, 3);
eval { $k->ro(99) };
my $e = $@;
like $e, qr{'K' cannot access the value of 'ro' on objects of class 'K' at t/00-base.t line };
$k->wo(801);
is $k->{wo}, '801';
is_deeply($k->wo(2, 3), [ 2, 3 ]);
eval { $k->wo() };
$e = $@;
ok $e;
like $e, qr{'K' cannot alter the value of 'wo' on objects of class 'K' at t/00-base.t line };

perl-Class-Accessor-Lite-0.05/t/01-new.t000064400000000000000000000004371147710532300175420ustar00rootroot00000000000000package L;

use strict;
use warnings;

use Test::More tests => 5;

use_ok('Class::Accessor::Lite');

Class::Accessor::Lite->mk_new_and_accessors(qw(foo bar));

ok ! $@, 'call mk_new_and_accessors';

my $l = L->new(
foo => 2,
);
is ref($l), 'L';
is $l->foo, 2;
ok ! defined $l->bar;
perl-Class-Accessor-Lite-0.05/t/02-use.t000064400000000000000000000006121147710532300175410ustar00rootroot00000000000000package L;

use strict;
use warnings;
use Class::Accessor::Lite (
new => 1,
rw => [ qw(foo bar baz) ],
ro => [ qw(tokuhirom) ],
wo => [ qw(yappo) ],
);

use Test::More tests => 6;

my $l = L->new(
bar => 1,
tokuhirom => 2,
);
is ref($l), 'L';
ok ! defined $l->foo;
is $l->bar, 1;
is $l->tokuhirom, 2;
$l->bar(3);
is $l->bar, 3;
$l->yappo(4);
is $l->{yappo}, 4;
perl-Class-Accessor-Lite-0.05/t/03-use-error.t000064400000000000000000000002551147710532300206740ustar00rootroot00000000000000package L;

use strict;
use warnings;

use Class::Accessor::Lite;
use Test::More tests => 1;

local $@;
eval {
Class::Accessor::Lite->import(rw => "foo");
};
ok $@, $@;
 
дизайн и разработка: Vladimir Lettiev aka crux © 2004-2005, Andrew Avramenko aka liks © 2007-2008
текущий майнтейнер: Michael Shigorin