From 647083b83c8e22adf6aca3b696f9ad42422c50d4 Mon Sep 17 00:00:00 2001 From: Packit Date: Sep 30 2020 10:28:21 +0000 Subject: Class-Accessor-Chained-0.01 base --- diff --git a/Build.PL b/Build.PL new file mode 100644 index 0000000..c681835 --- /dev/null +++ b/Build.PL @@ -0,0 +1,15 @@ +use strict; +use Module::Build; + +Module::Build + ->new( module_name => "Class::Accessor::Chained", + license => 'perl', + build_requires => { + 'Test::More' => 0, + }, + requires => { + 'Class::Accessor' => 0, + }, + create_makefile_pl => 'traditional', + ) + ->create_build_script; diff --git a/Changes b/Changes new file mode 100644 index 0000000..5086c5f --- /dev/null +++ b/Changes @@ -0,0 +1,2 @@ +0.01 Monday 24th November, 2003 + initial CPAN release diff --git a/MANIFEST b/MANIFEST new file mode 100644 index 0000000..62c0aae --- /dev/null +++ b/MANIFEST @@ -0,0 +1,10 @@ +Build.PL +Changes +MANIFEST +Makefile.PL +README +META.yml +lib/Class/Accessor/Chained.pm +lib/Class/Accessor/Chained/Fast.pm +t/00compile.t +t/chained.t diff --git a/META.yml b/META.yml new file mode 100644 index 0000000..470285b --- /dev/null +++ b/META.yml @@ -0,0 +1,18 @@ +--- #YAML:1.0 +name: Class-Accessor-Chained +version: 0.01 +license: perl +distribution_type: module +requires: + Class::Accessor: 0 +recommends: {} +build_requires: + Test::More: 0 +conflicts: {} +provides: + Class::Accessor::Chained: + file: lib/Class/Accessor/Chained.pm + version: 0.01 + Class::Accessor::Chained::Fast: + file: lib/Class/Accessor/Chained/Fast.pm +generated_by: Module::Build version 0.21 diff --git a/Makefile.PL b/Makefile.PL new file mode 100644 index 0000000..7333547 --- /dev/null +++ b/Makefile.PL @@ -0,0 +1,14 @@ +# Generated by Module::Build::Compat->create_makefile_pl + +use ExtUtils::MakeMaker; +WriteMakefile + ( + NAME => 'Class::Accessor::Chained', + VERSION => '0.01', + PL_FILES => {}, + INSTALLDIRS => 'site', + PREREQ_PM => { + 'Test::More' => '0', + 'Class::Accessor' => '0', + }, + ); diff --git a/README b/README new file mode 100644 index 0000000..6443345 --- /dev/null +++ b/README @@ -0,0 +1,57 @@ +README for Class::Accessor::Chained 0.01 + +=head1 NAME + +Class::Accessor::Chained - make chained accessors + +=head1 SYNOPSIS + + package Foo; + use base qw( Class::Accessor::Chained ); + __PACKAGE__->mk_accessors(qw( foo bar baz )); + + my $foo = Foo->new->foo(1)->bar(2)->baz(4); + print $foo->bar; # prints 2 + + +=head1 DEPENDENCIES + +This module has external dependencies on the following modules: + + Class::Accessor + +=head1 INSTALLATION + + perl Build.PL + perl Build test + +and if all goes well + + perl Build install + +=head1 HISTORY + +What changed over the last 3 revisions + +=over + +=item 0.01 Monday 24th November, 2003 + + initial CPAN release +=back + +=head1 AUTHOR + +Richard Clamp + +=head1 COPYRIGHT + +Copyright (C) 2003 Richard Clamp. All Rights Reserved. + +This module is free software; you can redistribute it and/or modify it +under the same terms as Perl itself. + +=head1 SEE ALSO + +L, L + diff --git a/lib/Class/Accessor/Chained.pm b/lib/Class/Accessor/Chained.pm new file mode 100644 index 0000000..2c99070 --- /dev/null +++ b/lib/Class/Accessor/Chained.pm @@ -0,0 +1,82 @@ +use strict; +package Class::Accessor::Chained; +use base 'Class::Accessor'; +our $VERSION = '0.01'; + +sub make_accessor { + my($class, $field) = @_; + + # Build a closure around $field. + return sub { + my($self) = shift; + + if (@_) { + $self->set($field, @_); + return $self; + } + else { + return $self->get($field); + } + }; +} + +sub make_wo_accessor { + my($class, $field) = @_; + + return sub { + my($self) = shift; + + unless (@_) { + my $caller = caller; + require Carp; + Carp::croak("'$caller' cannot access the value of '$field' on ". + "objects of class '$class'"); + } + else { + $self->set($field, @_); + return $self; + } + }; +} + +1; +__END__ + +=head1 NAME + +Class::Accessor::Chained - make chained accessors + +=head1 SYNOPSIS + + package Foo; + use base qw( Class::Accessor::Chained ); + __PACKAGE__->mk_accessors(qw( foo bar baz )); + + my $foo = Foo->new->foo(1)->bar(2)->baz(4); + print $foo->bar; # prints 2 + +=head1 DESCRIPTION + +A chained accessor is one that always returns the object when called +with parameters (to set), and the value of the field when called with +no arguments. + +This module subclasses Class::Accessor in order to provide the same +mk_accessors interface. + +=head1 AUTHOR + +Richard Clamp + +=head1 COPYRIGHT + +Copyright (C) 2003 Richard Clamp. All Rights Reserved. + +This module is free software; you can redistribute it and/or modify it +under the same terms as Perl itself. + +=head1 SEE ALSO + +L, L + +=cut diff --git a/lib/Class/Accessor/Chained/Fast.pm b/lib/Class/Accessor/Chained/Fast.pm new file mode 100644 index 0000000..d52f89f --- /dev/null +++ b/lib/Class/Accessor/Chained/Fast.pm @@ -0,0 +1,70 @@ +use strict; +package Class::Accessor::Chained::Fast; +use base 'Class::Accessor::Fast'; + +sub make_accessor { + my($class, $field) = @_; + + return sub { + my $self = shift; + if(@_) { + $self->{$field} = (@_ == 1 ? $_[0] : [@_]); + return $self; + } + return $self->{$field}; + }; +} + +sub make_wo_accessor { + my($class, $field) = @_; + + return sub { + my($self) = shift; + + unless (@_) { + my $caller = caller; + require Carp; + Carp::croak("'$caller' cannot access the value of '$field' on ". + "objects of class '$class'"); + } + else { + $self->{$field} = (@_ == 1 ? $_[0] : [@_]); + return $self; + } + }; +} + +1; + +=head1 NAME + +Class::Accessor::Chained::Fast - Faster, but less expandable, chained accessors + +=head1 SYNOPSIS + + package Foo; + use base qw(Class::Accessor::Chained::Fast); + + # The rest as Class::Accessor::Chained except no set() or get(). + +=head1 DESCRIPTION + +By analogue to Class::Accessor and Class::Accessor::Fast this module +provides a faster less-flexible chained accessor maker. + +=head1 AUTHOR + +Richard Clamp + +=head1 COPYRIGHT + +Copyright (C) 2003 Richard Clamp. All Rights Reserved. + +This module is free software; you can redistribute it and/or modify it +under the same terms as Perl itself. + +=head1 SEE ALSO + +L, L + +=cut diff --git a/t/00compile.t b/t/00compile.t new file mode 100644 index 0000000..536d27a --- /dev/null +++ b/t/00compile.t @@ -0,0 +1,5 @@ +#!perl -w +use strict; +use Test::More tests => 2; +require_ok('Class::Accessor::Chained'); +require_ok('Class::Accessor::Chained::Fast'); diff --git a/t/chained.t b/t/chained.t new file mode 100644 index 0000000..ea41abf --- /dev/null +++ b/t/chained.t @@ -0,0 +1,24 @@ +#!perl -w +use strict; +use Test::More tests => 6; + +package Foo; +use base 'Class::Accessor::Chained'; +__PACKAGE__->mk_accessors(qw( foo bar baz )); +package main; + +my $foo = Foo->new->foo(1)->baz(2)->bar(4); +isa_ok( $foo, 'Foo' ); +is( $foo->bar, 4, "get gets the value" ); +is( $foo->foo( 5 ), $foo, "set gets the object" ); + +# and again, but with Fast accessors +package Bar; +use base 'Class::Accessor::Chained::Fast'; +__PACKAGE__->mk_accessors(qw( foo bar baz )); +package main; + +my $bar = Bar->new->foo(1)->baz(2)->bar(4); +isa_ok( $bar, 'Bar' ); +is( $bar->bar, 4, "get gets the value" ); +is( $bar->foo( 5 ), $bar, "set gets the object" );