Class-Accessor-0.51/0000700000175000017500000000000013173147305013310 5ustar martymartyClass-Accessor-0.51/t/0000700000175000017500000000000013173147305013553 5ustar martymartyClass-Accessor-0.51/t/croak.t0000644000175000017500000000072212360511642015046 0ustar martymartyuse strict; use Test::More tests => 2; require Class::Accessor::Fast; @Frog::ISA = ('Class::Accessor::Fast'); my $croaked = 0; sub Frog::_croak { ++$croaked } Frog->mk_ro_accessors('test_ro'); Frog->mk_wo_accessors('test_wo'); my $frog = Frog->new; eval { $croaked = 0; $frog->test_ro("foo"); is $croaked, 1, "we croaked for ro"; $croaked = 0; $frog->test_wo; is $croaked, 1, "we croaked for wo"; }; fail "We really croaked: $@" if $@; Class-Accessor-0.51/t/antlers.t0000644000175000017500000000254712360511642015426 0ustar martymarty#!perl use strict; use Test::More tests => 14; package No::Silly::Hands; use Class::Accessor; ::ok !defined &has, "I can haz has?"; package Silly::Hands; use Class::Accessor "antlers"; ::ok defined &has, "I iz in ur module"; has "foo"; has rwrw => ( is => "rw", isa => "Int" ); has roro => ( is => "ro", isa => "Str" ); has wowo => ( is => "wo", isa => "Str" ); package main; for my $f (qw/foo roro wowo rwrw/) { ok +Silly::Hands->can($f), "'$f' method exists"; } my $test = Silly::Hands->new({ foo => "bar", roro => "boat", rwrw => "huh", wowo => "whoa", }); is($test->foo, "bar", "initial foo"); $test->foo("stuff"); is($test->foo, "stuff", "new foo"); is($test->{foo}, "stuff", "new foo in hash"); is($test->roro, 'boat', 'ro accessor'); eval { $test->roro('stuff'); }; like(scalar $@, qr/cannot alter the value of 'roro' on objects of class 'Silly::Hands'/, 'ro accessor write protection'); $test->wowo(1001001); is( $test->{wowo}, 1001001, 'wo accessor'); eval { () = $test->wowo; }; like(scalar $@, qr/cannot access the value of 'wowo' on objects of class 'Silly::Hands'/, 'wo accessor read protection' ); package Silly::Hands; { my $eeek; local $SIG{__WARN__} = sub { $eeek = shift }; has DESTROY => (is => "rw"); ::like($eeek, qr/a data accessor named DESTROY/i, 'mk DESTROY accessor warning'); }; Class-Accessor-0.51/t/getset.t0000644000175000017500000000044112360511642015240 0ustar martymarty#!perl use strict; use Test::More tests => 3; require_ok("Class::Accessor"); @Foo::ISA = qw(Class::Accessor); Foo->mk_accessors(qw( foo )); my $test = Foo->new({ foo => 49 }); is $test->get('foo'), 49, "get initial foo"; $test->set('foo', 42); is $test->get('foo'), 42, "get new foo"; Class-Accessor-0.51/t/aliases.t0000644000175000017500000000207612360511642015374 0ustar martymarty#!perl use strict; use Test::More tests => 36; for my $class (qw(Class::Accessor Class::Accessor::Fast Class::Accessor::Faster)) { require_ok($class); my $silly = "Silly::$class"; { no strict 'refs'; @{"${silly}::ISA"} = ($class); *{"${silly}::accessor_name_for"} = sub { "read_$_[1]" }; *{"${silly}::mutator_name_for"} = sub { "write_$_[1]" }; $silly->mk_accessors(qw( foo )); $silly->mk_ro_accessors(qw(roro)); $silly->mk_wo_accessors(qw(wowo)); } for my $f (qw/foo roro /) { ok $silly->can("read_$f"), "'read_$f' method exists"; } for my $f (qw/foo wowo/) { ok $silly->can("write_$f"), "'write_$f' method exists"; } for my $f (qw/foo roro wowo write_roro read_wowo/) { ok !$silly->can($f), "no '$f' method"; } my $test = $silly->new({ foo => "bar", roro => "boat", wowo => "huh", }); is($test->read_foo, "bar", "initial foo"); $test->write_foo("stuff"); is($test->read_foo, "stuff", "new foo"); } Class-Accessor-0.51/t/accessors.t0000644000175000017500000000461612360511642015742 0ustar martymarty#!perl use strict; use Test::More tests => 42; for my $class (qw(Class::Accessor Class::Accessor::Fast Class::Accessor::Faster)) { require_ok($class); my $silly = "Silly::$class"; { no strict 'refs'; @{"${silly}::ISA"} = ($class); *{"${silly}::car"} = sub { shift->_car_accessor(@_); }; *{"${silly}::mar"} = sub { return "Overloaded"; }; $silly->mk_accessors(qw( foo bar yar car mar )); $silly->mk_ro_accessors(qw(static unchanged)); $silly->mk_wo_accessors(qw(sekret double_sekret)); } my $test = $silly->new({ static => "variable", unchanged => "dynamic", }); $test->foo(42); $test->bar('Meep'); is($test->foo, 42, "foo accessor"); is($test->{foo}, 42, "foo hash element") unless $class eq 'Class::Accessor::Faster'; is($test->static, 'variable', 'ro accessor'); eval { $test->static('foo'); }; like(scalar $@, qr/^'main' cannot alter the value of 'static' on objects of class '$silly'/, 'ro accessor write protection'); $test->double_sekret(1001001); is( $test->{double_sekret}, 1001001, 'wo accessor') unless $class eq 'Class::Accessor::Faster'; eval { () = $test->double_sekret; }; like(scalar $@, qr/^'main' cannot access the value of 'double_sekret' on objects of class '$silly'/, 'wo accessor read protection' ); is($test->_foo_accessor, 42, 'accessor alias'); $test->car("AMC Javalin"); is($test->car, 'AMC Javalin', 'internal override access'); is($test->mar, 'Overloaded', 'internal override constant'); # Make sure bogus accessors die. eval { $test->gargle() }; ok($@, 'bad accessor'); # Test that the accessor works properly in list context with a single arg. my $test2 = $silly->new; my @args = ($test2->foo, $test2->bar); is(@args, 2, 'accessor get in list context'); # test array setters $test->foo(qw(1 2 3)); is_deeply($test->foo, [qw(1 2 3)], "set an array ref via foo accessor"); $test->sekret(qw(1 2 3)); is_deeply($test->{'sekret'}, [qw(1 2 3)], "array ref") unless $class eq 'Class::Accessor::Faster'; { my $eeek; local $SIG{__WARN__} = sub { $eeek = shift }; $silly->mk_accessors(qw(DESTROY)); like($eeek, qr/a data accessor named DESTROY/i, 'mk DESTROY accessor warning'); }; } Class-Accessor-0.51/t/bestpractice.t0000644000175000017500000000172512360511642016423 0ustar martymarty#!perl use strict; use Test::More tests => 36; for my $class (qw(Class::Accessor Class::Accessor::Fast Class::Accessor::Faster)) { require_ok($class); my $silly = "Silly::$class"; { no strict 'refs'; @{"${silly}::ISA"} = ($class); $silly->follow_best_practice; $silly->mk_accessors(qw( foo )); $silly->mk_ro_accessors(qw(roro)); $silly->mk_wo_accessors(qw(wowo)); } for my $f (qw/foo roro /) { ok $silly->can("get_$f"), "'get_$f' method exists"; } for my $f (qw/foo wowo/) { ok $silly->can("set_$f"), "'set_$f' method exists"; } for my $f (qw/foo roro wowo set_roro get_wowo/) { ok !$silly->can($f), "no '$f' method"; } my $test = $silly->new({ foo => "bar", roro => "boat", wowo => "huh", }); is($test->get_foo, "bar", "initial foo"); $test->set_foo("stuff"); is($test->get_foo, "stuff", "new foo"); } Class-Accessor-0.51/t/caller.t0000644000175000017500000000210712360511642015210 0ustar martymarty#!perl use strict; use Test::More; unless (eval {require Sub::Name}) { plan skip_all => "Sub::Name is not installed"; exit 0; } plan tests => 6; require_ok("Class::Accessor"); require_ok("Class::Accessor::Fast"); package Foo; our @ISA = qw(Class::Accessor); sub get { my ($self, $key) = @_; my @c = caller(1); main::is $c[3], "Foo::$key", "correct name for Foo sub $key"; return $self->SUPER::get($key); } __PACKAGE__->mk_accessors(qw( foo )); package Tricky; require Tie::Hash; our @ISA = qw(Tie::StdHash); sub FETCH { my ($self, $key) = @_; my @c = caller(1); main::is $c[3], "Bar::$key", "correct name for Bar sub $key"; return $self->SUPER::FETCH($key); } package Bar; our @ISA = qw(Class::Accessor::Fast); sub new { my ($class, $init) = @_; my %store; tie %store, "Tricky"; %store = %$init; bless \%store, $class; } __PACKAGE__->mk_accessors(qw( bar )); package main; my $foo = Foo->new({ foo => 12345 }); is $foo->foo, 12345, "get initial foo"; my $bar = Bar->new({ bar => 54321 }); is $bar->bar, 54321, "get initial bar"; Class-Accessor-0.51/MANIFEST0000644000175000017500000000052613173147305014456 0ustar martymartyChanges examples/benchmark INSTALL lib/Class/Accessor.pm lib/Class/Accessor/Fast.pm lib/Class/Accessor/Faster.pm Makefile.PL MANIFEST This list of files META.yml README t/accessors.t t/aliases.t t/antlers.t t/bestpractice.t t/croak.t t/getset.t t/caller.t META.json Module JSON meta-data (added by MakeMaker) Class-Accessor-0.51/README0000644000175000017500000000307513173110550014177 0ustar martymartyNAMES Class::Accessor - automated accessor generation Class::Accessor::Fast - faster automated accessor generation Class::Accessor::Faster - even faster, using an array DESCRIPTION This module automagically generates accessors/mutators for your class. Most of the time, writing accessors is an exercise in cutting and pasting. You usually wind up with a series of almost identical methods, one for each piece of data in your object. While some will be unique, doing value checks and special storage tricks, most will simply be exercises in repetition. If you make your module a subclass of Class::Accessor and declare your accessor fields with mk_accessors() then you'll find yourself with a set of automatically generated accessors, which can even be customized! The basic set up is very simple: package My::Class; use base qw(Class::Accessor); My::Class->mk_accessors( qw(foo bar car) ); Done. My::Class now has simple foo(), bar() and car() accessors defined. If you prefer a Moose-like interface you can do this instead: package My::Class; use Class::Accessor "moose-like"; has foo => ( is => "rw" ); has bar => ( is => "rw" ); has car => ( is => "rw" ); Done, again. AUTHOR Copyright 2017 Marty Pauley This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. That means either (a) the GNU General Public License or (b) the Artistic License. Class-Accessor-0.51/examples/0000700000175000017500000000000013173147305015126 5ustar martymartyClass-Accessor-0.51/examples/benchmark0000644000175000017500000000247313031754332017020 0ustar martymarty#!/usr/bin/perl -w BEGIN { $ENV{MOO_XS_DISABLE} = "no cheating"; $ENV{MOUSE_PUREPERL} = "no cheating"; } package Bench::Base; sub new { my($class) = shift; bless { test => 23 }, $class; } package Bench::Direct; use base qw(Bench::Base); package Bench::Normal; use Class::Accessor "moose-like"; has test => (is => "rw"); package Bench::Fast; use Class::Accessor::Fast "moose-like"; has test => (is => "rw"); package Bench::Faster; use Class::Accessor::Faster "antlers"; has test => (is => "rw"); package Bench::Moose; use Moose; has test => (is => "rw"); package Bench::Mouse; use Mouse; has test => (is => "rw"); package Bench::Moo; use Moo; has test => (is => "rw"); package main; use strict; use Benchmark 'cmpthese'; use Test::More tests => 12; my $tmp; my $direct = Bench::Direct->new({ test => 23 }); my %accessor = ( Direct => sub { $tmp = $direct->{test}; } ); my %mutator = ( Direct => sub { $direct->{test} = 42; } ); for my $p (qw/Normal Fast Faster Moose Mouse Moo/) { my $o = "Bench::$p"->new({ test => 23 }); is $o->test, 23, "$p init"; $o->test(24); is $o->test, 24, "$p set"; $accessor{$p} = sub { $tmp = $o->test; }; $mutator{$p} = sub { $o->test(42); }; } print "accessors:\n"; cmpthese( -1, \%accessor ); print "\n"; print "mutators:\n"; cmpthese( -1, \%mutator ); Class-Accessor-0.51/lib/0000700000175000017500000000000013173147305014056 5ustar martymartyClass-Accessor-0.51/lib/Class/0000700000175000017500000000000013173147305015123 5ustar martymartyClass-Accessor-0.51/lib/Class/Accessor/0000700000175000017500000000000013173147305016665 5ustar martymartyClass-Accessor-0.51/lib/Class/Accessor/Fast.pm0000644000175000017500000000474113173126337020142 0ustar martymartypackage Class::Accessor::Fast; use base 'Class::Accessor'; use strict; use B 'perlstring'; $Class::Accessor::Fast::VERSION = '0.51'; sub make_accessor { my ($class, $field) = @_; eval sprintf q{ sub { return $_[0]{%s} if scalar(@_) == 1; return $_[0]{%s} = scalar(@_) == 2 ? $_[1] : [@_[1..$#_]]; } }, map { perlstring($_) } $field, $field; } sub make_ro_accessor { my($class, $field) = @_; eval sprintf q{ sub { return $_[0]{%s} if @_ == 1; my $caller = caller; $_[0]->_croak(sprintf "'$caller' cannot alter the value of '%%s' on objects of class '%%s'", %s, %s); } }, map { perlstring($_) } $field, $field, $class; } sub make_wo_accessor { my($class, $field) = @_; eval sprintf q{ sub { if (@_ == 1) { my $caller = caller; $_[0]->_croak(sprintf "'$caller' cannot access the value of '%%s' on objects of class '%%s'", %s, %s); } else { return $_[0]{%s} = $_[1] if @_ == 2; return (shift)->{%s} = \@_; } } }, map { perlstring($_) } $field, $class, $field, $field; } 1; __END__ =head1 NAME Class::Accessor::Fast - Faster, but less expandable, accessors =head1 SYNOPSIS package Foo; use base qw(Class::Accessor::Fast); # The rest is the same as Class::Accessor but without set() and get(). =head1 DESCRIPTION This is a faster but less expandable version of Class::Accessor. Class::Accessor's generated accessors require two method calls to accomplish their task (one for the accessor, another for get() or set()). Class::Accessor::Fast eliminates calling set()/get() and does the access itself, resulting in a somewhat faster accessor. The downside is that you can't easily alter the behavior of your accessors, nor can your subclasses. Of course, should you need this later, you can always swap out Class::Accessor::Fast for Class::Accessor. Read the documentation for Class::Accessor for more info. =head1 EFFICIENCY L for an efficiency comparison. =head1 AUTHORS Copyright 2017 Marty Pauley This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. That means either (a) the GNU General Public License or (b) the Artistic License. =head2 ORIGINAL AUTHOR Michael G Schwern =head1 SEE ALSO L =cut Class-Accessor-0.51/lib/Class/Accessor/Faster.pm0000644000175000017500000000526213173145076020471 0ustar martymartypackage Class::Accessor::Faster; use base 'Class::Accessor'; use strict; use B 'perlstring'; $Class::Accessor::Faster::VERSION = '0.51'; my %slot; sub _slot { my($class, $field) = @_; my $n = $slot{$class}->{$field}; return $n if defined $n; $n = keys %{$slot{$class}}; $slot{$class}->{$field} = $n; return $n; } sub new { my($proto, $fields) = @_; my($class) = ref $proto || $proto; my $self = bless [], $class; $fields = {} unless defined $fields; for my $k (keys %$fields) { my $n = $class->_slot($k); $self->[$n] = $fields->{$k}; } return $self; } sub make_accessor { my($class, $field) = @_; my $n = $class->_slot($field); eval sprintf q{ sub { return $_[0][%d] if scalar(@_) == 1; return $_[0][%d] = scalar(@_) == 2 ? $_[1] : [@_[1..$#_]]; } }, $n, $n; } sub make_ro_accessor { my($class, $field) = @_; my $n = $class->_slot($field); eval sprintf q{ sub { return $_[0][%d] if @_ == 1; my $caller = caller; $_[0]->_croak(sprintf "'$caller' cannot alter the value of '%%s' on objects of class '%%s'", %s, %s); } }, $n, map(perlstring($_), $field, $class); } sub make_wo_accessor { my($class, $field) = @_; my $n = $class->_slot($field); eval sprintf q{ sub { if (@_ == 1) { my $caller = caller; $_[0]->_croak(sprintf "'$caller' cannot access the value of '%%s' on objects of class '%%s'", %s, %s); } else { return $_[0][%d] = $_[1] if @_ == 2; return (shift)->[%d] = \@_; } } }, map(perlstring($_), $field, $class), $n, $n; } 1; __END__ =head1 NAME Class::Accessor::Faster - Even faster, but less expandable, accessors =head1 SYNOPSIS package Foo; use base qw(Class::Accessor::Faster); =head1 DESCRIPTION This is a faster but less expandable version of Class::Accessor::Fast. Class::Accessor's generated accessors require two method calls to accomplish their task (one for the accessor, another for get() or set()). Class::Accessor::Fast eliminates calling set()/get() and does the access itself, resulting in a somewhat faster accessor. Class::Accessor::Faster uses an array reference underneath to be faster. Read the documentation for Class::Accessor for more info. =head1 AUTHORS Copyright 2017 Marty Pauley This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. That means either (a) the GNU General Public License or (b) the Artistic License. =head1 SEE ALSO L =cut Class-Accessor-0.51/lib/Class/Accessor.pm0000644000175000017500000005023613173112070017232 0ustar martymartypackage Class::Accessor; require 5.00502; use strict; $Class::Accessor::VERSION = '0.51'; sub new { return bless defined $_[1] ? {%{$_[1]}} # make a copy of $fields. : {}, ref $_[0] || $_[0]; } sub mk_accessors { my($self, @fields) = @_; $self->_mk_accessors('rw', @fields); } if (eval { require Sub::Name }) { Sub::Name->import; } { no strict 'refs'; sub import { my ($class, @what) = @_; my $caller = caller; for (@what) { if (/^(?:antlers|moose-?like)$/i) { *{"${caller}::has"} = sub { my ($f, %args) = @_; $caller->_mk_accessors(($args{is}||"rw"), $f); }; *{"${caller}::extends"} = sub { @{"${caller}::ISA"} = @_; unless (grep $_->can("_mk_accessors"), @_) { push @{"${caller}::ISA"}, $class; } }; # we'll use their @ISA as a default, in case it happens to be # set already &{"${caller}::extends"}(@{"${caller}::ISA"}); } } } sub follow_best_practice { my($self) = @_; my $class = ref $self || $self; *{"${class}::accessor_name_for"} = \&best_practice_accessor_name_for; *{"${class}::mutator_name_for"} = \&best_practice_mutator_name_for; } sub _mk_accessors { my($self, $access, @fields) = @_; my $class = ref $self || $self; my $ra = $access eq 'rw' || $access eq 'ro'; my $wa = $access eq 'rw' || $access eq 'wo'; foreach my $field (@fields) { my $accessor_name = $self->accessor_name_for($field); my $mutator_name = $self->mutator_name_for($field); if( $accessor_name eq 'DESTROY' or $mutator_name eq 'DESTROY' ) { $self->_carp("Having a data accessor named DESTROY in '$class' is unwise."); } if ($accessor_name eq $mutator_name) { my $accessor; if ($ra && $wa) { $accessor = $self->make_accessor($field); } elsif ($ra) { $accessor = $self->make_ro_accessor($field); } else { $accessor = $self->make_wo_accessor($field); } my $fullname = "${class}::$accessor_name"; my $subnamed = 0; unless (defined &{$fullname}) { subname($fullname, $accessor) if defined &subname; $subnamed = 1; *{$fullname} = $accessor; } if ($accessor_name eq $field) { # the old behaviour my $alias = "${class}::_${field}_accessor"; subname($alias, $accessor) if defined &subname and not $subnamed; *{$alias} = $accessor unless defined &{$alias}; } } else { my $fullaccname = "${class}::$accessor_name"; my $fullmutname = "${class}::$mutator_name"; if ($ra and not defined &{$fullaccname}) { my $accessor = $self->make_ro_accessor($field); subname($fullaccname, $accessor) if defined &subname; *{$fullaccname} = $accessor; } if ($wa and not defined &{$fullmutname}) { my $mutator = $self->make_wo_accessor($field); subname($fullmutname, $mutator) if defined &subname; *{$fullmutname} = $mutator; } } } } } sub mk_ro_accessors { my($self, @fields) = @_; $self->_mk_accessors('ro', @fields); } sub mk_wo_accessors { my($self, @fields) = @_; $self->_mk_accessors('wo', @fields); } sub best_practice_accessor_name_for { my ($class, $field) = @_; return "get_$field"; } sub best_practice_mutator_name_for { my ($class, $field) = @_; return "set_$field"; } sub accessor_name_for { my ($class, $field) = @_; return $field; } sub mutator_name_for { my ($class, $field) = @_; return $field; } sub set { my($self, $key) = splice(@_, 0, 2); if(@_ == 1) { $self->{$key} = $_[0]; } elsif(@_ > 1) { $self->{$key} = [@_]; } else { $self->_croak("Wrong number of arguments received"); } } sub get { my $self = shift; if(@_ == 1) { return $self->{$_[0]}; } elsif( @_ > 1 ) { return @{$self}{@_}; } else { $self->_croak("Wrong number of arguments received"); } } sub make_accessor { my ($class, $field) = @_; return sub { my $self = shift; if(@_) { return $self->set($field, @_); } else { return $self->get($field); } }; } sub make_ro_accessor { my($class, $field) = @_; return sub { my $self = shift; if (@_) { my $caller = caller; $self->_croak("'$caller' cannot alter the value of '$field' on objects of class '$class'"); } else { return $self->get($field); } }; } sub make_wo_accessor { my($class, $field) = @_; return sub { my $self = shift; unless (@_) { my $caller = caller; $self->_croak("'$caller' cannot access the value of '$field' on objects of class '$class'"); } else { return $self->set($field, @_); } }; } use Carp (); sub _carp { my ($self, $msg) = @_; Carp::carp($msg || $self); return; } sub _croak { my ($self, $msg) = @_; Carp::croak($msg || $self); return; } 1; __END__ =head1 NAME Class::Accessor - Automated accessor generation =head1 SYNOPSIS package Foo; use base qw(Class::Accessor); Foo->follow_best_practice; Foo->mk_accessors(qw(name role salary)); # or if you prefer a Moose-like interface... package Foo; use Class::Accessor "antlers"; has name => ( is => "rw", isa => "Str" ); has role => ( is => "rw", isa => "Str" ); has salary => ( is => "rw", isa => "Num" ); # Meanwhile, in a nearby piece of code! # Class::Accessor provides new(). my $mp = Foo->new({ name => "Marty", role => "JAPH" }); my $job = $mp->role; # gets $mp->{role} $mp->salary(400000); # sets $mp->{salary} = 400000 # I wish # like my @info = @{$mp}{qw(name role)} my @info = $mp->get(qw(name role)); # $mp->{salary} = 400000 $mp->set('salary', 400000); =head1 DESCRIPTION This module automagically generates accessors/mutators for your class. Most of the time, writing accessors is an exercise in cutting and pasting. You usually wind up with a series of methods like this: sub name { my $self = shift; if(@_) { $self->{name} = $_[0]; } return $self->{name}; } sub salary { my $self = shift; if(@_) { $self->{salary} = $_[0]; } return $self->{salary}; } # etc... One for each piece of data in your object. While some will be unique, doing value checks and special storage tricks, most will simply be exercises in repetition. Not only is it Bad Style to have a bunch of repetitious code, but it's also simply not lazy, which is the real tragedy. If you make your module a subclass of Class::Accessor and declare your accessor fields with mk_accessors() then you'll find yourself with a set of automatically generated accessors which can even be customized! The basic set up is very simple: package Foo; use base qw(Class::Accessor); Foo->mk_accessors( qw(far bar car) ); Done. Foo now has simple far(), bar() and car() accessors defined. Alternatively, if you want to follow Damian's I guidelines you can use: package Foo; use base qw(Class::Accessor); Foo->follow_best_practice; Foo->mk_accessors( qw(far bar car) ); B you must call C before calling C. =head2 Moose-like By popular demand we now have a simple Moose-like interface. You can now do: package Foo; use Class::Accessor "antlers"; has far => ( is => "rw" ); has bar => ( is => "rw" ); has car => ( is => "rw" ); Currently only the C attribute is supported. =head1 CONSTRUCTOR Class::Accessor provides a basic constructor, C. It generates a hash-based object and can be called as either a class method or an object method. =head2 new my $obj = Foo->new; my $obj = $other_obj->new; my $obj = Foo->new(\%fields); my $obj = $other_obj->new(\%fields); It takes an optional %fields hash which is used to initialize the object (handy if you use read-only accessors). The fields of the hash correspond to the names of your accessors, so... package Foo; use base qw(Class::Accessor); Foo->mk_accessors('foo'); my $obj = Foo->new({ foo => 42 }); print $obj->foo; # 42 however %fields can contain anything, new() will shove them all into your object. =head1 MAKING ACCESSORS =head2 follow_best_practice In Damian's Perl Best Practices book he recommends separate get and set methods with the prefix set_ and get_ to make it explicit what you intend to do. If you want to create those accessor methods instead of the default ones, call: __PACKAGE__->follow_best_practice B you call any of the accessor-making methods. =head2 accessor_name_for / mutator_name_for You may have your own crazy ideas for the names of the accessors, so you can make those happen by overriding C and C in your subclass. (I copied that idea from Class::DBI.) =head2 mk_accessors __PACKAGE__->mk_accessors(@fields); This creates accessor/mutator methods for each named field given in @fields. Foreach field in @fields it will generate two accessors. One called "field()" and the other called "_field_accessor()". For example: # Generates foo(), _foo_accessor(), bar() and _bar_accessor(). __PACKAGE__->mk_accessors(qw(foo bar)); See L for details. =head2 mk_ro_accessors __PACKAGE__->mk_ro_accessors(@read_only_fields); Same as mk_accessors() except it will generate read-only accessors (ie. true accessors). If you attempt to set a value with these accessors it will throw an exception. It only uses get() and not set(). package Foo; use base qw(Class::Accessor); Foo->mk_ro_accessors(qw(foo bar)); # Let's assume we have an object $foo of class Foo... print $foo->foo; # ok, prints whatever the value of $foo->{foo} is $foo->foo(42); # BOOM! Naughty you. =head2 mk_wo_accessors __PACKAGE__->mk_wo_accessors(@write_only_fields); Same as mk_accessors() except it will generate write-only accessors (ie. mutators). If you attempt to read a value with these accessors it will throw an exception. It only uses set() and not get(). B I'm not entirely sure why this is useful, but I'm sure someone will need it. If you've found a use, let me know. Right now it's here for orthogonality and because it's easy to implement. package Foo; use base qw(Class::Accessor); Foo->mk_wo_accessors(qw(foo bar)); # Let's assume we have an object $foo of class Foo... $foo->foo(42); # OK. Sets $self->{foo} = 42 print $foo->foo; # BOOM! Can't read from this accessor. =head1 Moose! If you prefer a Moose-like interface to create accessors, you can use C by importing this module like this: use Class::Accessor "antlers"; or use Class::Accessor "moose-like"; Then you can declare accessors like this: has alpha => ( is => "rw", isa => "Str" ); has beta => ( is => "ro", isa => "Str" ); has gamma => ( is => "wo", isa => "Str" ); Currently only the C attribute is supported. And our C also supports the "wo" value to make a write-only accessor. If you are using the Moose-like interface then you should use the C rather than tweaking your C<@ISA> directly. Basically, replace @ISA = qw/Foo Bar/; with extends(qw/Foo Bar/); =head1 DETAILS An accessor generated by Class::Accessor looks something like this: # Your foo may vary. sub foo { my($self) = shift; if(@_) { # set return $self->set('foo', @_); } else { return $self->get('foo'); } } Very simple. All it does is determine if you're wanting to set a value or get a value and calls the appropriate method. Class::Accessor provides default get() and set() methods which your class can override. They're detailed later. =head2 Modifying the behavior of the accessor Rather than actually modifying the accessor itself, it is much more sensible to simply override the two key methods which the accessor calls. Namely set() and get(). If you -really- want to, you can override make_accessor(). =head2 set $obj->set($key, $value); $obj->set($key, @values); set() defines how generally one stores data in the object. override this method to change how data is stored by your accessors. =head2 get $value = $obj->get($key); @values = $obj->get(@keys); get() defines how data is retrieved from your objects. override this method to change how it is retrieved. =head2 make_accessor $accessor = __PACKAGE__->make_accessor($field); Generates a subroutine reference which acts as an accessor for the given $field. It calls get() and set(). If you wish to change the behavior of your accessors, try overriding get() and set() before you start mucking with make_accessor(). =head2 make_ro_accessor $read_only_accessor = __PACKAGE__->make_ro_accessor($field); Generates a subroutine reference which acts as a read-only accessor for the given $field. It only calls get(). Override get() to change the behavior of your accessors. =head2 make_wo_accessor $write_only_accessor = __PACKAGE__->make_wo_accessor($field); Generates a subroutine reference which acts as a write-only accessor (mutator) for the given $field. It only calls set(). Override set() to change the behavior of your accessors. =head1 EXCEPTIONS If something goes wrong Class::Accessor will warn or die by calling Carp::carp or Carp::croak. If you don't like this you can override _carp() and _croak() in your subclass and do whatever else you want. =head1 EFFICIENCY Class::Accessor does not employ an autoloader, thus it is much faster than you'd think. Its generated methods incur no special penalty over ones you'd write yourself. accessors: Rate Basic Fast Faster Direct Basic 367589/s -- -51% -55% -89% Fast 747964/s 103% -- -9% -77% Faster 819199/s 123% 10% -- -75% Direct 3245887/s 783% 334% 296% -- mutators: Rate Acc Fast Faster Direct Acc 265564/s -- -54% -63% -91% Fast 573439/s 116% -- -21% -80% Faster 724710/s 173% 26% -- -75% Direct 2860979/s 977% 399% 295% -- Class::Accessor::Fast is faster than methods written by an average programmer (where "average" is based on Schwern's example code). Class::Accessor is slower than average, but more flexible. Class::Accessor::Faster is even faster than Class::Accessor::Fast. It uses an array internally, not a hash. This could be a good or bad feature depending on your point of view. Direct hash access is, of course, much faster than all of these, but it provides no encapsulation. Of course, it's not as simple as saying "Class::Accessor is slower than average". These are benchmarks for a simple accessor. If your accessors do any sort of complicated work (such as talking to a database or writing to a file) the time spent doing that work will quickly swamp the time spend just calling the accessor. In that case, Class::Accessor and the ones you write will be roughly the same speed. =head1 EXAMPLES Here's an example of generating an accessor for every public field of your class. package Altoids; use base qw(Class::Accessor Class::Fields); use fields qw(curiously strong mints); Altoids->mk_accessors( Altoids->show_fields('Public') ); sub new { my $proto = shift; my $class = ref $proto || $proto; return fields::new($class); } my Altoids $tin = Altoids->new; $tin->curiously('Curiouser and curiouser'); print $tin->{curiously}; # prints 'Curiouser and curiouser' # Subclassing works, too. package Mint::Snuff; use base qw(Altoids); my Mint::Snuff $pouch = Mint::Snuff->new; $pouch->strong('Blow your head off!'); print $pouch->{strong}; # prints 'Blow your head off!' Here's a simple example of altering the behavior of your accessors. package Foo; use base qw(Class::Accessor); Foo->mk_accessors(qw(this that up down)); sub get { my $self = shift; # Note every time someone gets some data. print STDERR "Getting @_\n"; $self->SUPER::get(@_); } sub set { my ($self, $key) = splice(@_, 0, 2); # Note every time someone sets some data. print STDERR "Setting $key to @_\n"; $self->SUPER::set($key, @_); } =head1 CAVEATS AND TRICKS Class::Accessor has to do some internal wackiness to get its job done quickly and efficiently. Because of this, there's a few tricks and traps one must know about. Hey, nothing's perfect. =head2 Don't make a field called DESTROY This is bad. Since DESTROY is a magical method it would be bad for us to define an accessor using that name. Class::Accessor will carp if you try to use it with a field named "DESTROY". =head2 Overriding autogenerated accessors You may want to override the autogenerated accessor with your own, yet have your custom accessor call the default one. For instance, maybe you want to have an accessor which checks its input. Normally, one would expect this to work: package Foo; use base qw(Class::Accessor); Foo->mk_accessors(qw(email this that whatever)); # Only accept addresses which look valid. sub email { my($self) = shift; my($email) = @_; if( @_ ) { # Setting require Email::Valid; unless( Email::Valid->address($email) ) { carp("$email doesn't look like a valid address."); return; } } return $self->SUPER::email(@_); } There's a subtle problem in the last example, and it's in this line: return $self->SUPER::email(@_); If we look at how Foo was defined, it called mk_accessors() which stuck email() right into Foo's namespace. There *is* no SUPER::email() to delegate to! Two ways around this... first is to make a "pure" base class for Foo. This pure class will generate the accessors and provide the necessary super class for Foo to use: package Pure::Organic::Foo; use base qw(Class::Accessor); Pure::Organic::Foo->mk_accessors(qw(email this that whatever)); package Foo; use base qw(Pure::Organic::Foo); And now Foo::email() can override the generated Pure::Organic::Foo::email() and use it as SUPER::email(). This is probably the most obvious solution to everyone but me. Instead, what first made sense to me was for mk_accessors() to define an alias of email(), _email_accessor(). Using this solution, Foo::email() would be written with: return $self->_email_accessor(@_); instead of the expected SUPER::email(). =head1 AUTHORS Copyright 2017 Marty Pauley This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. That means either (a) the GNU General Public License or (b) the Artistic License. =head2 ORIGINAL AUTHOR Michael G Schwern =head2 THANKS Liz and RUZ for performance tweaks. Tels, for his big feature request/bug report. Various presenters at YAPC::Asia 2009 for criticising the non-Moose interface. =head1 SEE ALSO See L and L if speed is more important than flexibility. These are some modules which do similar things in different ways L, L, L, L, L, L, L See L for an example of this module in use. =cut Class-Accessor-0.51/Makefile.PL0000644000175000017500000000051213173110550015262 0ustar martymartyuse ExtUtils::MakeMaker; require 5.00502; WriteMakefile( NAME => 'Class::Accessor', VERSION_FROM => 'lib/Class/Accessor.pm', AUTHOR => 'Marty Pauley ', LICENSE => 'perl', PREREQ_PM => { base => $] == 5.006 ? 1.02 : 1.01, }, ); Class-Accessor-0.51/INSTALL0000644000175000017500000000044612360511642014353 0ustar martymartyWHAT IS THIS? This is Class::Accessor, a Perl module. Please see the README that comes with this distribution. HOW DO I INSTALL IT? To install this module, cd to the directory that contains this README file and type the following: perl Makefile.PL make make test make install Class-Accessor-0.51/Changes0000644000175000017500000000662713173107454014631 0ustar martymarty0.50 2017-10-20 Thanks to Jonas B. Nielsen for working through the RT queue. - patch for speed increase RT#84838 - patch for faster constructor RT#57353 - fixed typos for RT#61304 and RT#86422 0.34 Sat Sep 12 21:50:26 JST 2009 - add a Moose-like interface: I can haz "has" 0.33 Tue May 5 00:15:09 JST 2009 - small cleanups to fix RT#45592 and RT#43493 0.32 Tue Jun 10 10:31:06 JST 2008 - use Sub::Name to give names to anon subs to fix RT#17856 0.31 Wed Jul 11 23:03:47 JST 2007 - applied performance patch from RUZ 0.30 Sun Nov 26 13:03:47 JST 2006 - added version numbers back into each class to fix RT#21746 0.26 Wed Jul 19 01:20:23 BST 2006 - added Class::Accessor::Faster that uses an array internally. Thanks to Tina Mueller for prompting me to do this. 0.25 Fri Mar 31 18:28:17 JST 2006 - added a 'follow_best_practice' class method that causes the accessors to be called 'get_foo' and 'set_foo' instead of just 'foo' - added 'accessor_name_for' and 'mutator_name_for' methods that you can override in your subclass to create your own names for accessors. (That idea taken from Class::DBI.) 0.23 Sat Feb 25 19:46:08 GMT 2006 - rewrote the tests. Now using Test::More 0.21 Thu Sep 1 16:58:31 BST 2005 - added _croak and _carp methods so you can override these in subclasses if you want to change the default behaviour when something bad happens. 0.20 Thu Sep 1 12:25:23 BST 2005 - add a copyright statement - tidy some documentation (more to do later) 0.19 Tue Mar 2 23:10:48 GMT 2004 * Performance patch from Elizabeth Mattijsen - minor layout and doc changes 0.18 Mon Apr 2 11:52:41 BST 2003 * First change in 2 years: Marty Pauley is new maintainer. - changed the dependency for base to cope with the broken version shipped with perl 5.6.0. 0.17 Mon Apr 2 11:52:41 BST 2001 - Now requiring Class::Fields's base (because 5.005_03's is busted) 0.16 Wed Dec 13 21:19:21 EST 2000 * Added Class::Accessor::Fast * Added simple new() method - Added EFFICIENCY, SEE ALSO and THANKS doc sections - Added docs about why this module is interesting * Added read-only and write-only accessors (thanks Tels) - Fixed the Altoids example (thanks again, Tels) - Added a simple example of overiding accessors 0.15 Thu Aug 17 20:39:02 EDT 2000 - Removed use of Carp::Assert to speed loading time - Added an EXAMPLE section. 0.14 Sun Jul 2 20:05:51 EDT 2000 - Rolled the accessor overriding caveats from the TPC paper into the docs. 0.13 Mon Apr 24 20:33:40 EDT 2000 - Left a dependency on Class::Fields in the tests. 0.12 Tue Apr 18 13:22:17 EDT 2000 - Left a dependency on Class::Fields in Accessor.pm - Removed PREREQ_PM on base.pm 0.11 Mon Apr 17 20:11:00 EDT 2000 - Removed a few "use public"s from the docs. 0.10 Fri Apr 14 23:29:01 EDT 2000 * Removed the Autoloader. * Instead of wraping public data accessors around public data (how silly) Class::Accessor now simply generates the accessors you request. * Added mk_accessors() - Removed the docs about the accessor autoloader. - Removed the autoloading caveat. * Removed make_static_accessors() - It will now only warn if you try to make an accessor called DESTROY() - detabbed everything 0.02 Sun Dec 12 02:22:12 EST 1999 * Class::Fields::Accessor is now Class::Accessor Class-Accessor-0.51/META.yml0000600000175000017500000000103313173147305014560 0ustar martymarty--- abstract: unknown author: - 'Marty Pauley ' build_requires: ExtUtils::MakeMaker: '0' configure_requires: ExtUtils::MakeMaker: '0' dynamic_config: 1 generated_by: 'ExtUtils::MakeMaker version 7.1002, CPAN::Meta::Converter version 2.150010' license: perl meta-spec: url: http://module-build.sourceforge.net/META-spec-v1.4.html version: '1.4' name: Class-Accessor no_index: directory: - t - inc requires: base: '1.01' version: '0.51' x_serialization_backend: 'CPAN::Meta::YAML version 0.018' Class-Accessor-0.51/META.json0000600000175000017500000000163313173147305014736 0ustar martymarty{ "abstract" : "unknown", "author" : [ "Marty Pauley " ], "dynamic_config" : 1, "generated_by" : "ExtUtils::MakeMaker version 7.1002, CPAN::Meta::Converter version 2.150010", "license" : [ "perl_5" ], "meta-spec" : { "url" : "http://search.cpan.org/perldoc?CPAN::Meta::Spec", "version" : "2" }, "name" : "Class-Accessor", "no_index" : { "directory" : [ "t", "inc" ] }, "prereqs" : { "build" : { "requires" : { "ExtUtils::MakeMaker" : "0" } }, "configure" : { "requires" : { "ExtUtils::MakeMaker" : "0" } }, "runtime" : { "requires" : { "base" : "1.01" } } }, "release_status" : "stable", "version" : "0.51", "x_serialization_backend" : "JSON::PP version 2.27300_01" }