From bcbf441e09fb502cf64924ff2530fa144bdf52c5 Mon Sep 17 00:00:00 2001 From: Andreas Brachold Date: Mon, 13 Aug 2007 18:41:27 +0000 Subject: * Move files to trunk --- lib/Class/MakeMethods/Attribute.pm | 143 ++ lib/Class/MakeMethods/Autoload.pm | 182 ++ lib/Class/MakeMethods/Basic.pm | 98 + lib/Class/MakeMethods/Basic/Array.pm | 422 ++++ lib/Class/MakeMethods/Basic/Global.pm | 298 +++ lib/Class/MakeMethods/Basic/Hash.pm | 362 ++++ lib/Class/MakeMethods/Composite.pm | 218 +++ lib/Class/MakeMethods/Composite/Array.pm | 794 ++++++++ lib/Class/MakeMethods/Composite/Global.pm | 588 ++++++ lib/Class/MakeMethods/Composite/Hash.pm | 719 +++++++ lib/Class/MakeMethods/Composite/Inheritable.pm | 613 ++++++ lib/Class/MakeMethods/Composite/Universal.pm | 150 ++ lib/Class/MakeMethods/Docs/Catalog.pod | 888 +++++++++ lib/Class/MakeMethods/Docs/Changes.pod | 661 +++++++ lib/Class/MakeMethods/Docs/Examples.pod | 554 ++++++ lib/Class/MakeMethods/Docs/ReadMe.pod | 279 +++ lib/Class/MakeMethods/Docs/RelatedModules.pod | 962 ++++++++++ lib/Class/MakeMethods/Docs/ToDo.pod | 296 +++ lib/Class/MakeMethods/Emulator.pm | 165 ++ lib/Class/MakeMethods/Emulator/AccessorFast.pm | 102 + lib/Class/MakeMethods/Emulator/Inheritable.pm | 162 ++ lib/Class/MakeMethods/Emulator/MethodMaker.pm | 676 +++++++ lib/Class/MakeMethods/Emulator/Singleton.pm | 85 + lib/Class/MakeMethods/Emulator/Struct.pm | 154 ++ lib/Class/MakeMethods/Emulator/accessors.pm | 122 ++ lib/Class/MakeMethods/Emulator/mcoder.pm | 116 ++ lib/Class/MakeMethods/Evaled.pm | 97 + lib/Class/MakeMethods/Evaled/Hash.pm | 349 ++++ lib/Class/MakeMethods/Standard.pm | 68 + lib/Class/MakeMethods/Standard/Array.pm | 555 ++++++ lib/Class/MakeMethods/Standard/Global.pm | 405 ++++ lib/Class/MakeMethods/Standard/Hash.pm | 501 +++++ lib/Class/MakeMethods/Standard/Inheritable.pm | 428 +++++ lib/Class/MakeMethods/Standard/Universal.pm | 336 ++++ lib/Class/MakeMethods/Template.pm | 1255 ++++++++++++ lib/Class/MakeMethods/Template/Array.pm | 102 + lib/Class/MakeMethods/Template/Class.pm | 103 + lib/Class/MakeMethods/Template/ClassInherit.pm | 144 ++ lib/Class/MakeMethods/Template/ClassName.pm | 330 ++++ lib/Class/MakeMethods/Template/ClassVar.pm | 178 ++ lib/Class/MakeMethods/Template/Flyweight.pm | 43 + lib/Class/MakeMethods/Template/Generic.pm | 2349 +++++++++++++++++++++++ lib/Class/MakeMethods/Template/Global.pm | 97 + lib/Class/MakeMethods/Template/Hash.pm | 229 +++ lib/Class/MakeMethods/Template/Inheritable.pm | 154 ++ lib/Class/MakeMethods/Template/InsideOut.pm | 218 +++ lib/Class/MakeMethods/Template/PackageVar.pm | 168 ++ lib/Class/MakeMethods/Template/Ref.pm | 207 ++ lib/Class/MakeMethods/Template/Scalar.pm | 80 + lib/Class/MakeMethods/Template/Static.pm | 41 + lib/Class/MakeMethods/Template/Struct.pm | 41 + lib/Class/MakeMethods/Template/StructBuiltin.pm | 148 ++ lib/Class/MakeMethods/Template/Universal.pm | 415 ++++ lib/Class/MakeMethods/Utility/ArraySplicer.pm | 243 +++ lib/Class/MakeMethods/Utility/DiskCache.pm | 165 ++ lib/Class/MakeMethods/Utility/Inheritable.pm | 126 ++ lib/Class/MakeMethods/Utility/Ref.pm | 171 ++ lib/Class/MakeMethods/Utility/TextBuilder.pm | 207 ++ 58 files changed, 19762 insertions(+) create mode 100644 lib/Class/MakeMethods/Attribute.pm create mode 100644 lib/Class/MakeMethods/Autoload.pm create mode 100644 lib/Class/MakeMethods/Basic.pm create mode 100644 lib/Class/MakeMethods/Basic/Array.pm create mode 100644 lib/Class/MakeMethods/Basic/Global.pm create mode 100644 lib/Class/MakeMethods/Basic/Hash.pm create mode 100644 lib/Class/MakeMethods/Composite.pm create mode 100644 lib/Class/MakeMethods/Composite/Array.pm create mode 100644 lib/Class/MakeMethods/Composite/Global.pm create mode 100644 lib/Class/MakeMethods/Composite/Hash.pm create mode 100644 lib/Class/MakeMethods/Composite/Inheritable.pm create mode 100644 lib/Class/MakeMethods/Composite/Universal.pm create mode 100644 lib/Class/MakeMethods/Docs/Catalog.pod create mode 100644 lib/Class/MakeMethods/Docs/Changes.pod create mode 100644 lib/Class/MakeMethods/Docs/Examples.pod create mode 100644 lib/Class/MakeMethods/Docs/ReadMe.pod create mode 100644 lib/Class/MakeMethods/Docs/RelatedModules.pod create mode 100644 lib/Class/MakeMethods/Docs/ToDo.pod create mode 100644 lib/Class/MakeMethods/Emulator.pm create mode 100644 lib/Class/MakeMethods/Emulator/AccessorFast.pm create mode 100644 lib/Class/MakeMethods/Emulator/Inheritable.pm create mode 100644 lib/Class/MakeMethods/Emulator/MethodMaker.pm create mode 100644 lib/Class/MakeMethods/Emulator/Singleton.pm create mode 100644 lib/Class/MakeMethods/Emulator/Struct.pm create mode 100644 lib/Class/MakeMethods/Emulator/accessors.pm create mode 100644 lib/Class/MakeMethods/Emulator/mcoder.pm create mode 100644 lib/Class/MakeMethods/Evaled.pm create mode 100644 lib/Class/MakeMethods/Evaled/Hash.pm create mode 100644 lib/Class/MakeMethods/Standard.pm create mode 100644 lib/Class/MakeMethods/Standard/Array.pm create mode 100644 lib/Class/MakeMethods/Standard/Global.pm create mode 100644 lib/Class/MakeMethods/Standard/Hash.pm create mode 100644 lib/Class/MakeMethods/Standard/Inheritable.pm create mode 100644 lib/Class/MakeMethods/Standard/Universal.pm create mode 100644 lib/Class/MakeMethods/Template.pm create mode 100644 lib/Class/MakeMethods/Template/Array.pm create mode 100644 lib/Class/MakeMethods/Template/Class.pm create mode 100644 lib/Class/MakeMethods/Template/ClassInherit.pm create mode 100644 lib/Class/MakeMethods/Template/ClassName.pm create mode 100644 lib/Class/MakeMethods/Template/ClassVar.pm create mode 100644 lib/Class/MakeMethods/Template/Flyweight.pm create mode 100644 lib/Class/MakeMethods/Template/Generic.pm create mode 100644 lib/Class/MakeMethods/Template/Global.pm create mode 100644 lib/Class/MakeMethods/Template/Hash.pm create mode 100644 lib/Class/MakeMethods/Template/Inheritable.pm create mode 100644 lib/Class/MakeMethods/Template/InsideOut.pm create mode 100644 lib/Class/MakeMethods/Template/PackageVar.pm create mode 100644 lib/Class/MakeMethods/Template/Ref.pm create mode 100644 lib/Class/MakeMethods/Template/Scalar.pm create mode 100644 lib/Class/MakeMethods/Template/Static.pm create mode 100644 lib/Class/MakeMethods/Template/Struct.pm create mode 100644 lib/Class/MakeMethods/Template/StructBuiltin.pm create mode 100644 lib/Class/MakeMethods/Template/Universal.pm create mode 100644 lib/Class/MakeMethods/Utility/ArraySplicer.pm create mode 100644 lib/Class/MakeMethods/Utility/DiskCache.pm create mode 100644 lib/Class/MakeMethods/Utility/Inheritable.pm create mode 100644 lib/Class/MakeMethods/Utility/Ref.pm create mode 100644 lib/Class/MakeMethods/Utility/TextBuilder.pm (limited to 'lib/Class/MakeMethods') diff --git a/lib/Class/MakeMethods/Attribute.pm b/lib/Class/MakeMethods/Attribute.pm new file mode 100644 index 0000000..b8fe71d --- /dev/null +++ b/lib/Class/MakeMethods/Attribute.pm @@ -0,0 +1,143 @@ +package Class::MakeMethods::Attribute; + +require 5.006; +use strict; +use Carp; +use Attribute::Handlers; + +use Class::MakeMethods; +use Class::MakeMethods::Utility::Inheritable 'get_vvalue'; + +our $VERSION = 1.005; + +our %DefaultMaker; + +sub import { + my $class = shift; + + if ( scalar @_ and $_[0] =~ m/^\d/ ) { + Class::MakeMethods::_import_version( $class, shift ); + } + + if ( scalar @_ == 1 ) { + my $target_class = ( caller(0) )[0]; + $DefaultMaker{ $target_class } = shift; + } +} + +sub UNIVERSAL::MakeMethod :ATTR(CODE) { + my ($package, $symbol, $referent, $attr, $data) = @_; + if ( $symbol eq 'ANON' or $symbol eq 'LEXICAL' ) { + croak "Can't apply MakeMethod attribute to $symbol declaration." + } + if ( ! $data ) { + croak "No method type provided for MakeMethod attribute." + } + my $symname = *{$symbol}{NAME}; + if ( ref $data eq 'ARRAY' ) { + local $_ = shift @$data; + $symname = [ @$data, $symname ]; + $data = $_; + } + unless ( $DefaultMaker{$package} ) { + local $_ = get_vvalue( \%DefaultMaker, $package ); + $DefaultMaker{$package} = $_ if ( $_ ); + } + Class::MakeMethods->make( + -TargetClass => $package, + -ForceInstall => 1, + ( $DefaultMaker{$package} ? ('-MakerClass'=>$DefaultMaker{$package}) : () ), + $data => $symname + ); +} + +1; + +__END__ + +=head1 NAME + +Class::MakeMethods::Attribute - Declare generated subs with attribute syntax + +=head1 SYNOPSIS + + package MyObject; + use Class::MakeMethods::Attribute 'Standard::Hash'; + + sub new :MakeMethod('new'); + sub foo :MakeMethod('scalar'); + sub bar :MakeMethod('scalar', { hashkey => 'bar_data' }); + sub debug :MakeMethod('Standard::Global:scalar'); + +=head1 DESCRIPTION + +This package allows common types of methods to be generated via a subroutine attribute declaration. (Available in Perl 5.6 and later.) + +Adding the :MakeMethod() attribute to a subroutine declaration causes Class::MakeMethods to create and install a subroutine based on the parameters given to the :MakeMethod attribute. + +You can declare a default method-generation class by passing the name of a MakeMethods subclass in the use Class::MakeMethods::Attribute statement. This default method-generation class will also apply as the default to any subclasses declared at compile time. If no default method-generation class is selected, you will need to fully-qualify all method type declarations. + +=head1 EXAMPLE + +Here's a typical use of Class::MakeMethods::Attribute: + + package MyObject; + use Class::MakeMethods::Attribute 'Standard::Hash'; + + sub new :MakeMethod('new'); + sub foo :MakeMethod('scalar'); + sub bar :MakeMethod('scalar', { hashkey => 'bar_data' }); + sub debug :MakeMethod('Standard::Global:scalar'); + + package MySubclass; + use base 'MyObject'; + + sub bazzle :MakeMethod('scalar'); + +This is equivalent to the following explicit Class::MakeMethods invocations: + + package MyObject; + + use Class::MakeMethods ( + -MakerClass => 'Standard::Hash', + new => 'new', + scalar => 'foo', + scalar => [ 'ba', { hashkey => 'bar_data' } ], + 'Standard::Global:scalar' => 'debug', + ); + + package MySubclass; + use base 'MyObject'; + + use Class::MakeMethods ( + -MakerClass => 'Standard::Hash', + scalar => 'bazzle', + ); + +=head1 DIAGNOSTICS + +The following warnings and errors may be produced when using +Class::MakeMethods::Attribute to generate methods. (Note that this +list does not include run-time messages produced by calling the +generated methods, or the standard messages produced by +Class::MakeMethods.) + +=over + +=item Can't apply MakeMethod attribute to %s declaration. + +You can not use the C<:MakeMethod> attribute with lexical or anonymous subroutine declarations. + +=item No method type provided for MakeMethod attribute. + +You called C<:MakeMethod()> without the required method-type argument. + +=back + +=head1 SEE ALSO + +See L byÊDamian Conway. + +See L for general information about this distribution. + +=cut diff --git a/lib/Class/MakeMethods/Autoload.pm b/lib/Class/MakeMethods/Autoload.pm new file mode 100644 index 0000000..ab1a6ca --- /dev/null +++ b/lib/Class/MakeMethods/Autoload.pm @@ -0,0 +1,182 @@ +package Class::MakeMethods::Autoload; + +use strict; +use Carp; +require Exporter; + +use Class::MakeMethods; +use Class::MakeMethods::Utility::Inheritable 'get_vvalue'; + +use vars qw( $VERSION @ISA @EXPORT_OK ); + +$VERSION = 1.000; +@ISA = qw(Exporter); +@EXPORT_OK = qw( AUTOLOAD ); + +######################################################################## + +use vars qw( $AUTOLOAD %DefaultType ); + +sub import { + my $class = shift; + my $target_class = ( caller(0) )[0]; + + if ( scalar @_ and $_[0] =~ m/^\d/ ) { + Class::MakeMethods::_import_version( $class, shift ); + } + + if ( scalar @_ == 1 ) { + $DefaultType{ $target_class } = shift; + } + + __PACKAGE__->Exporter::export_to_level(1, $class, 'AUTOLOAD'); +} + +sub AUTOLOAD { + my $sym = $AUTOLOAD; + my ($package, $func) = ($sym =~ /(.*)::([^:]+)$/); + + unless ( $DefaultType{$package} ) { + local $_ = get_vvalue( \%DefaultType, $package ); + $DefaultType{$package} = $_ if ( $_ ); + } + + my $type = $DefaultType{$package} + or croak(__PACKAGE__ . ": No default method type for $package; can't auto-generate $func"); + + if ( ref $type eq 'HASH' ) { + my $n_type = $type->{ $func } || + ( map $type->{$_}, grep { $func =~ m/\A$_\Z/ } sort { length($b) <=> length($a) } keys %$type )[0] || + $type->{ '' } + or croak(__PACKAGE__ . ": Can't find best match for '$func' in type map (" . join(', ', keys %$type ) . ")"); + $type = $n_type; + } elsif ( ref $type eq 'CODE' ) { + $type = &$type( $func ) + or croak(__PACKAGE__ . ": Can't find match for '$func' in type map ($type)"); + } + + # warn "Autoload $func ($type)"; + Class::MakeMethods->make( + -TargetClass => $package, + -ForceInstall => 1, + $type => $func + ); + + if ( my $sub = $package->can( $func ) ) { + goto &$sub; + } else { + croak(__PACKAGE__ . ": Construction of $type method ${package}::$func failed to produce usable method") + } +} + +1; + +__END__ + +=head1 NAME + +Class::MakeMethods::Autoload - Declare generated subs with AUTOLOAD + +=head1 SYNOPSIS + + package MyObject; + use Class::MakeMethods::Autoload 'Standard::Hash::scalar'; + + package main; + my $obj = bless {}, 'MyObject'; + + $obj->foo("Foozle"); + print $obj->foo(); + +=head1 DESCRIPTION + +This package provides a generate-on-demand interface to Class::MakeMethods. + +When your class uses this package, it imports an AUTOLOAD function that resolves missing methods by using Class::MakeMethods to generate and install a standard type of method. + +You must specify the type of method to be generated by passing a single argument to your use Class::MakeMethods::Autoload statement, which can take any of these forms: + +=over 4 + +=item * + +A Class::MakeMethods generator name and method type. + +Here are three examples: + + use Class::MakeMethods::Autoload 'Standard::Hash:scalar'; + + use Class::MakeMethods::Autoload 'Basic::Universal::no_op'; + + use Class::MakeMethods::Autoload + '::Class::MakeMethod::Composite::Global:array'; + +=item * + +A reference to a subroutine which will be called for each requested function name and which is expected to return the name of the method generator to use. + +Here's a contrived example which generates scalar accessors for methods except those with a digit in their name, which are treated as globals. + + use Class::MakeMethods::Autoload sub { + my $name = shift; + ( $name =~ /\d/ ) ? 'Standard::Global::scalar' + : 'Standard::Hash::scalar' + }; + +=item * + +A reference to a hash which defines which method type to use based on the name of the requested method. If a key exists which is an exact match for the requested function name, the associated value is used; otherwise, each of the keys is used as a regular expression, and the value of the first one that matches the function name is used. (For regular expression matching, the keys are tested in reverse length order, longest to shortest.) + +Here's an example which provides a new() constructor, a DESTROY() method that does nothing, and a wildcard match that provides scalar accessors for all other Autoloaded methods: + + use Class::MakeMethods::Autoload { + 'new' => 'Standard::Hash::new', + '.*' => 'Standard::Hash::scalar', + 'DESTROY' => 'Standard::Universal::no_op', + }; + +Here's a more sophisticated example which causes all-upper-case method names to be generated as globals, those with a leading upper-case letter to be generated as inheritable data methods, and others to be normal accessors: + + use Class::MakeMethods::Autoload { + 'new' => 'Standard::Hash::new', + '.*' => 'Standard::Hash::scalar', + '[A-Z].*' => 'Standard::Inheritable::scalar', + '[A-Z0-9]+' => 'Standard::Global::scalar', + 'DESTROY' => 'Standard::Universal::no_op', + }; + +=back + +=head1 DIAGNOSTICS + +The following warnings and errors may be produced when using +Class::MakeMethods::Attribute to generate methods. (Note that this +list does not include run-time messages produced by calling the +generated methods, or the standard messages produced by +Class::MakeMethods.) + +=over + +=item No default method type; can't autoload + +You must declare a default method type, generally by passing its +name to a C statement, prior to +autoloading any methods. + +=item Construction of %s method %s failed to produce usable method + +Indicates that Autoload succesfully called Class::MakeMethods->make +to generate the requested method, but afterwards was not able to +invoke the generated method. You may have selected an incompatible +method type, or the method may not have been installed sucesfully. + +=back + +=head1 SEE ALSO + +See L for general information about this distribution. + +For distribution, installation, support, copyright and license +information, see L. + +=cut diff --git a/lib/Class/MakeMethods/Basic.pm b/lib/Class/MakeMethods/Basic.pm new file mode 100644 index 0000000..f893f5b --- /dev/null +++ b/lib/Class/MakeMethods/Basic.pm @@ -0,0 +1,98 @@ +package Class::MakeMethods::Basic; + +use Class::MakeMethods '-isasubclass'; + +$VERSION = 1.000; + +1; + +__END__ + +######################################################################## + +=head1 NAME + +Class::MakeMethods::Basic - Make really simple methods + + +=head1 SYNOPSIS + + package MyObject; + use Class::MakeMethods::Basic::Hash ( + 'new' => [ 'new' ], + 'scalar' => [ 'foo', 'bar' ] + ); + + package main; + + my $obj = MyObject->new( foo => "Foozle", bar => "Bozzle" ); + print $obj->foo(); + $obj->bar("Barbados"); + + +=head1 DESCRIPTION + +This document describes the various subclasses of Class::MakeMethods +included under the Basic::* namespace, and the method types each +one provides. + +The Basic subclasses provide stripped-down method-generation implementations. + +Subroutines are generated as closures bound to each method name. + +=head2 Calling Conventions + +When you C a subclass of this package, the method declarations you provide +as arguments cause subroutines to be generated and installed in +your module. You can also omit the arguments to C and instead make methods +at runtime by passing the declarations to a subsequent call to +C. + +You may include any number of declarations in each call to C +or C. If methods with the same name already exist, earlier +calls to C or C win over later ones, but within each +call, later declarations superceed earlier ones. + +You can install methods in a different package by passing C<-TargetClass =E I> as your first arguments to C or C. + +See L for more details. + +=head2 Declaration Syntax + +The following types of declarations are supported: + +=over 4 + +=item * + +I => 'I' + +=item * + +I => 'I I...' + +=item * + +I => [ 'I', 'I', ...] + +=back + +For a list of the supported values of I, see +L, or the documentation +for each subclass. + +For each method name you provide, a subroutine of the indicated +type will be generated and installed under that name in your module. + +Method names should start with a letter, followed by zero or more +letters, numbers, or underscores. + + +=head1 SEE ALSO + +See L for general information about this distribution. + +For distribution, installation, support, copyright and license +information, see L. + +=cut diff --git a/lib/Class/MakeMethods/Basic/Array.pm b/lib/Class/MakeMethods/Basic/Array.pm new file mode 100644 index 0000000..c537866 --- /dev/null +++ b/lib/Class/MakeMethods/Basic/Array.pm @@ -0,0 +1,422 @@ +=head1 NAME + +Class::MakeMethods::Basic::Array - Basic array methods + +=head1 SYNOPSIS + + package MyObject; + use Class::MakeMethods::Basic::Array ( + new => 'new', + scalar => [ 'foo', 'bar' ], + array => 'my_list', + hash => 'my_index', + ); + ... + + # Constructor + my $obj = MyObject->new( foo => 'Foozle' ); + + # Scalar Accessor + print $obj->foo(); + + $obj->bar('Barbados'); + print $obj->bar(); + + # Array accessor + $obj->my_list(0 => 'Foozle', 1 => 'Bang!'); + print $obj->my_list(1); + + # Hash accessor + $obj->my_index('broccoli' => 'Blah!', 'foo' => 'Fiddle'); + print $obj->my_index('foo'); + + +=head1 DESCRIPTION + +The Basic::Array subclass of MakeMethods provides a basic +constructor and accessors for blessed-array object instances. + +=head2 Calling Conventions + +When you C this package, the method names you provide +as arguments cause subroutines to be generated and installed in +your module. + +See L for a summary, or L for full details. + +=head2 Declaration Syntax + +To declare methods, pass in pairs of a method-type name followed +by one or more method names. Valid method-type names for this +package are listed in L<"METHOD GENERATOR TYPES">. + +See L for more +syntax information. + +=cut + +package Class::MakeMethods::Basic::Array; + +$VERSION = 1.000; +use strict; +use Class::MakeMethods '-isasubclass'; + +######################################################################## + +=head2 About Positional Accessors + +Each accessor method claims the next available spot in the array +to store its value in. + +The mapping between method names and array positions is stored in +a hash named %FIELDS in the target package. When the first positional +accessor is defined for a package, its %FIELDS are initialized by +searching its inheritance tree. + +B: Subclassing packages that use positional accessors is +somewhat fragile, since you may end up with two distinct methods +assigned to the same position. Specific cases to avoid are: + +=over 4 + +=item * + +If you inherit from more than one class with positional accessors, +the positions used by the two sets of methods will overlap. + +=item * + +If your superclass adds additional positional accessors after you +declare your first, they will overlap yours. + +=back + +=cut + +sub _array_index { + my $class = shift; + my $name = shift; + no strict; + local $^W = 0; + if ( ! scalar %{$class . "::FIELDS"} ) { + my @classes = @{$class . "::ISA"}; + my @fields; + while ( @classes ) { + my $superclass = shift @classes; + if ( scalar %{$superclass . "::FIELDS"} ) { + push @fields, %{$superclass . "::FIELDS"}; + } else { + unshift @classes, @{$superclass . "::ISA"} + } + } + %{$class . "::FIELDS"} = @fields + } + my $field_hash = \%{$class . "::FIELDS"}; + $field_hash->{$name} or $field_hash->{$name} = scalar keys %$field_hash +} + +######################################################################## + +=head1 METHOD GENERATOR TYPES + +=head2 new - Constructor + +For each method name passed, returns a subroutine with the following characteristics: + +=over 4 + +=item * + +If called as a class method, makes a new array and blesses it into that class. + +=item * + +If called on an array-based instance, makes a copy of it and blesses the copy into the same class as the original instance. + +=item * + +If passed a list of method-value pairs, calls each named method with the associated value as an argument. + +=item * + +Returns the new instance. + +=back + +Sample declaration and usage: + + package MyObject; + use Class::MakeMethods::Basic::Array ( + new => 'new', + ); + ... + + # Bare constructor + my $empty = MyObject->new(); + + # Constructor with initial sequence of method calls + my $obj = MyObject->new( foo => 'Foozle', bar => 'Barbados' ); + + # Copy with overriding sequence of method calls + my $copy = $obj->new( bar => 'Bob' ); + +=cut + +sub new { + my $class = shift; + map { + my $name = $_; + $name => sub { + my $callee = shift; + my $self = ref($callee) ? bless( [@$callee], ref($callee) ) + : bless( [], $callee ); + while ( scalar @_ ) { + my $method = shift; + $self->$method( shift ); + } + return $self; + } + } @_; +} + +######################################################################## + +=head2 scalar - Instance Accessor + +For each method name passed, uses a closure to generate a subroutine with the following characteristics: + +=over 4 + +=item * + +Must be called on an array-based instance. + +=item * + +Determines the array position associated with the method name, and uses that as an index into each instance to access the related value. + +=item * + +If called without any arguments returns the current value (or undef). + +=item * + +If called with an argument, stores that as the value, and returns it, + +=back + +Sample declaration and usage: + + package MyObject; + use Class::MakeMethods::Basic::Array ( + scalar => 'foo', + ); + ... + + # Store value + $obj->foo('Foozle'); + + # Retrieve value + print $obj->foo; + +=cut + +sub scalar { + my $class = shift; + map { + my $name = $_; + my $index = _array_index( $class->_context('TargetClass'), $name ); + $name => sub { + my $self = shift; + if ( scalar @_ ) { + $self->[$index] = shift; + } else { + $self->[$index]; + } + } + } @_; +} + +######################################################################## + +=head2 array - Instance Ref Accessor + +For each method name passed, uses a closure to generate a subroutine with the following characteristics: + +=over 4 + +=item * + +Must be called on an array-based instance. + +=item * + +Determines the array position associated with the method name, and uses that as an index into each instance to access the related value. + +=item * + +The value for each instance will be a reference to an array (or undef). + +=item * + +If called without any arguments, returns the current array-ref value (or undef). + +=item * + +If called with one argument, uses that argument as an index to retrieve from the referenced array, and returns that value (or undef). If the single argument is an array ref, then a slice of the referenced array is returned. + +=item * + +If called with a list of index-value pairs, stores the value at the given index in the referenced array. If the instance's value was previously undefined, a new array is autovivified. The current value in each position will be overwritten, and later arguments with the same index will override earlier ones. Returns the current array-ref value. + +=back + +Sample declaration and usage: + + package MyObject; + use Class::MakeMethods::Basic::Array ( + array => 'bar', + ); + ... + + # Set values by position + $obj->bar(0 => 'Foozle', 1 => 'Bang!'); + + # Positions may be overwritten, and in any order + $obj->bar(2 => 'And Mash', 1 => 'Blah!'); + + # Retrieve value by position + print $obj->bar(1); + + # Retrieve slice of values by position + print join(', ', $obj->bar( [0, 2] ) ); + + # Direct access to referenced array + print scalar @{ $obj->bar() }; + + # Reset the array contents to empty + @{ $obj->bar() } = (); + +=cut + +sub array { + my $class = shift; + map { + my $name = $_; + my $index = _array_index( $class->_context('TargetClass'), $name ); + $name => sub { + my $self = shift; + if ( scalar(@_) == 0 ) { + return $self->[$index]; + } elsif ( scalar(@_) == 1 ) { + return $self->[$index]->[ shift() ]; + } elsif ( scalar(@_) % 2 ) { + Carp::croak "Odd number of items in assigment to $name"; + } else { + while ( scalar(@_) ) { + my $k = shift(); + $self->[$index]->[ $k ] = shift(); + } + return $self->[$index]; + } + } + } @_; +} + +######################################################################## + +=head2 hash - Instance Ref Accessor + +For each method name passed, uses a closure to generate a subroutine with the following characteristics: + +=over 4 + +=item * + +Must be called on an array-based instance. + +=item * + +Determines the array position associated with the method name, and uses that as an index into each instance to access the related value. + +=item * + +The value for each instance will be a reference to a hash (or undef). + +=item * + +If called without any arguments, returns the current hash-ref value (or undef). + +=item * + +If called with one argument, uses that argument as an index to retrieve from the referenced hash, and returns that value (or undef). If the single argument is an array ref, then a slice of the referenced hash is returned. + +=item * + +If called with a list of key-value pairs, stores the value under the given key in the referenced hash. If the instance's value was previously undefined, a new hash is autovivified. The current value under each key will be overwritten, and later arguments with the same key will override earlier ones. Returns the current hash-ref value. + +=back + +Sample declaration and usage: + + package MyObject; + use Class::MakeMethods::Basic::Array ( + hash => 'baz', + ); + ... + + # Set values by key + $obj->baz('foo' => 'Foozle', 'bar' => 'Bang!'); + + # Values may be overwritten, and in any order + $obj->baz('broccoli' => 'Blah!', 'foo' => 'Fiddle'); + + # Retrieve value by key + print $obj->baz('foo'); + + # Retrieve slice of values by position + print join(', ', $obj->baz( ['foo', 'bar'] ) ); + + # Direct access to referenced hash + print keys %{ $obj->baz() }; + + # Reset the hash contents to empty + @{ $obj->baz() } = (); + +=cut + +sub hash { + my $class = shift; + map { + my $name = $_; + my $index = _array_index( $class->_context('TargetClass'), $name ); + $name => sub { + my $self = shift; + if ( scalar(@_) == 0 ) { + return $self->[$index]; + } elsif ( scalar(@_) == 1 ) { + return $self->[$index]->{ shift() }; + } elsif ( scalar(@_) % 2 ) { + Carp::croak "Odd number of items in assigment to $name"; + } else { + while ( scalar(@_) ) { + my $k = shift(); + $self->[$index]->{ $k } = shift(); + } + return $self->[$index]; + } + } + } @_; +} + +######################################################################## + +=head1 SEE ALSO + +See L for general information about this distribution. + +See L for more about this family of subclasses. + +=cut + +1; diff --git a/lib/Class/MakeMethods/Basic/Global.pm b/lib/Class/MakeMethods/Basic/Global.pm new file mode 100644 index 0000000..21116c4 --- /dev/null +++ b/lib/Class/MakeMethods/Basic/Global.pm @@ -0,0 +1,298 @@ +=head1 NAME + +Class::MakeMethods::Basic::Global - Basic shared methods + +=head1 SYNOPSIS + + package MyObject; + use Class::MakeMethods::Basic::Global ( + scalar => [ 'foo', 'bar' ], + array => 'my_list', + hash => 'my_index', + ); + .... + + # Store and retrieve global values + MyObject->foo('Foobar'); + print MyObject->foo(); + + # All instances of your class access the same values + $my_object->bar('Barbados'); + print $other_one->bar(); + + # Array accessor + MyObject->my_list(0 => 'Foozle', 1 => 'Bang!'); + print MyObject->my_list(1); + + # Hash accessor + MyObject->my_index('broccoli' => 'Blah!', 'foo' => 'Fiddle'); + print MyObject->my_index('foo'); + + +=head1 DESCRIPTION + +The Basic::Global subclass of MakeMethods provides basic accessors for data shared by an entire class, sometimes called "static" or "class data." + +=head2 Calling Conventions + +When you C this package, the method names you provide +as arguments cause subroutines to be generated and installed in +your module. + +See L for a summary, or L for full details. + +=head2 Declaration Syntax + +To declare methods, pass in pairs of a method-type name followed +by one or more method names. Valid method-type names for this +package are listed in L<"METHOD GENERATOR TYPES">. + +See L for more +syntax information. + +=cut + +package Class::MakeMethods::Basic::Global; + +$VERSION = 1.000; +use Class::MakeMethods '-isasubclass'; + +######################################################################## + +=head1 METHOD GENERATOR TYPES + +=head2 scalar - Shared Accessor + +For each method name passed, uses a closure to generate a subroutine with the following characteristics: + +=over 4 + +=item * + +May be called as a class method, or equivalently, on any object instance. + +=item * + +Stores a global value accessible only through this method. + +=item * + +If called without any arguments returns the current value. + +=item * + +If called with an argument, stores that as the value, and returns it, + +=back + +Sample declaration and usage: + + package MyObject; + use Class::MakeMethods::Basic::Hash ( + scalar => 'foo', + ); + ... + + # Store value + MyObject->foo('Foozle'); + + # Retrieve value + print MyObject->foo; + +=cut + +sub scalar { + my $class = shift; + map { + my $name = $_; + $name => sub { + my $self = shift; + if ( scalar @_ ) { + $value = shift; + } else { + $value; + } + } + } @_; +} + +######################################################################## + +=head2 array - Shared Ref Accessor + +For each method name passed, uses a closure to generate a subroutine with the following characteristics: + +=over 4 + +=item * + +May be called as a class method, or equivalently, on any object instance. + +=item * + +Stores a global value accessible only through this method. + +=item * + +The value will be a reference to an array (or undef). + +=item * + +If called without any arguments, returns the current array-ref value (or undef). + +=item * + +If called with one argument, uses that argument as an index to retrieve from the referenced array, and returns that value (or undef). If the single argument is an array ref, then a slice of the referenced array is returned. + +=item * + +If called with a list of index-value pairs, stores the value at the given index in the referenced array. If the value was previously undefined, a new array is autovivified. The current value in each position will be overwritten, and later arguments with the same index will override earlier ones. Returns the current array-ref value. + +=back + +Sample declaration and usage: + + package MyObject; + use Class::MakeMethods::Basic::Hash ( + array => 'bar', + ); + ... + + # Set values by position + $obj->bar(0 => 'Foozle', 1 => 'Bang!'); + + # Positions may be overwritten, and in any order + $obj->bar(2 => 'And Mash', 1 => 'Blah!'); + + # Retrieve value by position + print $obj->bar(1); + + # Retrieve slice of values by position + print join(', ', $obj->bar( [0, 2] ) ); + + # Direct access to referenced array + print scalar @{ $obj->bar() }; + + # Reset the array contents to empty + @{ $obj->bar() } = (); + +=cut + +sub array { + my $class = shift; + map { + my $name = $_; + my $value = []; + $name => sub { + my $self = shift; + if ( scalar(@_) == 1 ) { + my $index = shift; + ref($index) ? @{$value}[ @$index ] : $value->[ $index ]; + } elsif ( scalar(@_) % 2 ) { + Carp::croak "Odd number of items in assigment to $name"; + } else { + while ( scalar(@_) ) { + $value->[ shift() ] = shift(); + } + return $value; + } + } + } @_; +} + +######################################################################## + +=head2 hash - Shared Ref Accessor + +For each method name passed, uses a closure to generate a subroutine with the following characteristics: + +=over 4 + +=item * + +May be called as a class method, or equivalently, on any object instance. + +=item * + +Stores a global value accessible only through this method. + +=item * + +The value will be a reference to a hash (or undef). + +=item * + +If called without any arguments, returns the current hash-ref value (or undef). + +=item * + +If called with one argument, uses that argument as an index to retrieve from the referenced hash, and returns that value (or undef). If the single argument is an array ref, then a slice of the referenced hash is returned. + +=item * + +If called with a list of key-value pairs, stores the value under the given key in the referenced hash. If the value was previously undefined, a new hash is autovivified. The current value under each key will be overwritten, and later arguments with the same key will override earlier ones. Returns the current hash-ref value. + +=back + +Sample declaration and usage: + + package MyObject; + use Class::MakeMethods::Basic::Hash ( + hash => 'baz', + ); + ... + + # Set values by key + $obj->baz('foo' => 'Foozle', 'bar' => 'Bang!'); + + # Values may be overwritten, and in any order + $obj->baz('broccoli' => 'Blah!', 'foo' => 'Fiddle'); + + # Retrieve value by key + print $obj->baz('foo'); + + # Retrieve slice of values by position + print join(', ', $obj->baz( ['foo', 'bar'] ) ); + + # Direct access to referenced hash + print keys %{ $obj->baz() }; + + # Reset the hash contents to empty + @{ $obj->baz() } = (); + +=cut + +sub hash { + my $class = shift; + map { + my $name = $_; + my $value = {}; + $name => sub { + my $self = shift; + if ( scalar(@_) == 1 ) { + my $index = shift; + ref($index) ? @{$value}{ @$index } : $value->{ $index }; + } elsif ( scalar(@_) % 2 ) { + Carp::croak "Odd number of items in assigment to $name"; + } else { + while ( scalar(@_) ) { + my $key = shift; + $value->{ $key } = shift(); + } + $value; + } + } + } @_; +} + +######################################################################## + +=head1 SEE ALSO + +See L for general information about this distribution. + +See L for more about this family of subclasses. + +=cut + +1; diff --git a/lib/Class/MakeMethods/Basic/Hash.pm b/lib/Class/MakeMethods/Basic/Hash.pm new file mode 100644 index 0000000..7a55106 --- /dev/null +++ b/lib/Class/MakeMethods/Basic/Hash.pm @@ -0,0 +1,362 @@ +=head1 NAME + +Class::MakeMethods::Basic::Hash - Basic hash methods + + +=head1 SYNOPSIS + + package MyObject; + use Class::MakeMethods::Basic::Hash ( + new => 'new', + scalar => [ 'foo', 'bar' ], + array => 'my_list', + hash => 'my_index', + ); + ... + + # Constructor + my $obj = MyObject->new( foo => 'Foozle' ); + + # Scalar Accessor + print $obj->foo(); + + $obj->bar('Barbados'); + print $obj->bar(); + + # Array accessor + $obj->my_list(0 => 'Foozle', 1 => 'Bang!'); + print $obj->my_list(1); + + # Hash accessor + $obj->my_index('broccoli' => 'Blah!', 'foo' => 'Fiddle'); + print $obj->my_index('foo'); + + +=head1 DESCRIPTION + +The Basic::Hash subclass of MakeMethods provides a basic constructor and accessors for blessed-hash object instances. + +=head2 Calling Conventions + +When you C this package, the method names you provide +as arguments cause subroutines to be generated and installed in +your module. + +See L for a summary, or L for full details. + +=head2 Declaration Syntax + +To declare methods, pass in pairs of a method-type name followed +by one or more method names. Valid method-type names for this +package are listed in L<"METHOD GENERATOR TYPES">. + +See L for more +syntax information. + + +=cut + +package Class::MakeMethods::Basic::Hash; + +$VERSION = 1.000; +use strict; +use Class::MakeMethods '-isasubclass'; + +######################################################################## + +=head1 METHOD GENERATOR TYPES + +=head2 new - Constructor + +For each method name passed, returns a subroutine with the following characteristics: + +=over 4 + +=item * + +If called as a class method, makes a new hash and blesses it into that class. + +=item * + +If called on a hash-based instance, makes a copy of it and blesses the copy into the same class as the original instance. + +=item * + +If passed a list of key-value pairs, appends them to the new hash. These arguments override any copied values, and later arguments with the same name will override earlier ones. + +=item * + +Returns the new instance. + +=back + +Sample declaration and usage: + + package MyObject; + use Class::MakeMethods::Basic::Hash ( + new => 'new', + ); + ... + + # Bare constructor + my $empty = MyObject->new(); + + # Constructor with initial values + my $obj = MyObject->new( foo => 'Foozle', bar => 'Barbados' ); + + # Copy with overriding value + my $copy = $obj->new( bar => 'Bob' ); + +=cut + +sub new { + my $class = shift; + map { + my $name = $_; + $name => sub { + my $callee = shift; + if ( ref $callee ) { + bless { %$callee, @_ }, ref $callee; + } else { + bless { @_ }, $callee; + } + } + } @_; +} + +######################################################################## + +=head2 scalar - Instance Accessor + +For each method name passed, uses a closure to generate a subroutine with the following characteristics: + +=over 4 + +=item * + +Must be called on a hash-based instance. + +=item * + +Uses the method name as a hash key to access the related value for each instance. + +=item * + +If called without any arguments returns the current value. + +=item * + +If called with an argument, stores that as the value, and returns it, + +=back + +Sample declaration and usage: + + package MyObject; + use Class::MakeMethods::Basic::Hash ( + scalar => 'foo', + ); + ... + + # Store value + $obj->foo('Foozle'); + + # Retrieve value + print $obj->foo; + +=cut + +sub scalar { + my $class = shift; + map { + my $name = $_; + $name => sub { + if ( scalar @_ > 1 ) { + $_[0]->{$name} = $_[1]; + } else { + $_[0]->{$name}; + } + } + } @_; +} + +######################################################################## + +=head2 array - Instance Ref Accessor + +For each method name passed, uses a closure to generate a subroutine with the following characteristics: + +=over 4 + +=item * + +Must be called on a hash-based instance. + +=item * + +Uses the method name as a hash key to access the related value for each instance. + +=item * + +The value for each instance will be a reference to an array (or undef). + +=item * + +If called without any arguments, returns the current array-ref value (or undef). + +=item * + +If called with one argument, uses that argument as an index to retrieve from the referenced array, and returns that value (or undef). + +=item * + +If called with a list of index-value pairs, stores the value at the given index in the referenced array. If the instance's value was previously undefined, a new array is autovivified. The current value in each position will be overwritten, and later arguments with the same index will override earlier ones. Returns the current array-ref value. + +=back + +Sample declaration and usage: + + package MyObject; + use Class::MakeMethods::Basic::Hash ( + array => 'bar', + ); + ... + + # Set values by position + $obj->bar(0 => 'Foozle', 1 => 'Bang!'); + + # Positions may be overwritten, and in any order + $obj->bar(2 => 'And Mash', 1 => 'Blah!'); + + # Retrieve value by position + print $obj->bar(1); + + # Direct access to referenced array + print scalar @{ $obj->bar() }; + + # Reset the array contents to empty + @{ $obj->bar() } = (); + +=cut + +sub array { + my $class = shift; + map { + my $name = $_; + $name => sub { + my $self = shift; + if ( scalar(@_) == 0 ) { + return $self->{$name}; + } elsif ( scalar(@_) == 1 ) { + $self->{$name}->[ shift() ]; + } elsif ( scalar(@_) % 2 ) { + Carp::croak "Odd number of items in assigment to $name"; + } else { + while ( scalar(@_) ) { + my $key = shift(); + $self->{$name}->[ $key ] = shift(); + } + return $self->{$name}; + } + } + } @_; +} + +######################################################################## + +=head2 hash - Instance Ref Accessor + +For each method name passed, uses a closure to generate a subroutine with the following characteristics: + +=over 4 + +=item * + +Must be called on a hash-based instance. + +=item * + +Uses the method name as a hash key to access the related value for each instance. + +=item * + +The value for each instance will be a reference to a hash (or undef). + +=item * + +If called without any arguments, returns the current hash-ref value (or undef). + +=item * + +If called with one argument, uses that argument as an index to retrieve from the referenced hash, and returns that value (or undef). + +=item * + +If called with a list of key-value pairs, stores the value under the given key in the referenced hash. If the instance's value was previously undefined, a new hash is autovivified. The current value under each key will be overwritten, and later arguments with the same key will override earlier ones. Returns the current hash-ref value. + +=back + +Sample declaration and usage: + + package MyObject; + use Class::MakeMethods::Basic::Hash ( + hash => 'baz', + ); + ... + + # Set values by key + $obj->baz('foo' => 'Foozle', 'bar' => 'Bang!'); + + # Values may be overwritten, and in any order + $obj->baz('broccoli' => 'Blah!', 'foo' => 'Fiddle'); + + # Retrieve value by key + print $obj->baz('foo'); + + # Direct access to referenced hash + print keys %{ $obj->baz() }; + + # Reset the hash contents to empty + @{ $obj->baz() } = (); + +=cut + +sub hash { + my $class = shift; + map { + my $name = $_; + $name => sub { + my $self = shift; + if ( scalar(@_) == 0 ) { + return $self->{$name}; + } elsif ( scalar(@_) == 1 ) { + $self->{$name}->{ shift() }; + } elsif ( scalar(@_) % 2 ) { + Carp::croak "Odd number of items in assigment to $name"; + } else { + while ( scalar(@_) ) { + $self->{$name}->{ shift() } = shift(); + } + return $self->{$name}; + } + } + } @_; +} + +######################################################################## + +=head1 SEE ALSO + +See L for general information about this distribution. + +See L for more about this family of subclasses. + +See L for equivalent functionality +based on blessed arrays. If all access to your object is through +constructors and accessors declared using this package, and your +class will not be extensively subclassed, consider switching to +Basic::Array to minimize resource consumption. + +=cut + +1; diff --git a/lib/Class/MakeMethods/Composite.pm b/lib/Class/MakeMethods/Composite.pm new file mode 100644 index 0000000..902c235 --- /dev/null +++ b/lib/Class/MakeMethods/Composite.pm @@ -0,0 +1,218 @@ +=head1 NAME + +Class::MakeMethods::Composite - Make extensible compound methods + + +=head1 SYNOPSIS + + package MyObject; + use Class::MakeMethods::Composite::Hash ( + new => 'new', + scalar => [ 'foo', 'bar' ], + array => 'my_list', + hash => 'my_index', + ); + + +=head1 DESCRIPTION + +This document describes the various subclasses of Class::MakeMethods +included under the Composite::* namespace, and the method types each +one provides. + +The Composite subclasses provide a parameterized set of method-generation +implementations. + +Subroutines are generated as closures bound to a hash containing +the method name and additional parameters, including the arrays of subroutine references that will provide the method's functionality. + + +=head2 Calling Conventions + +When you C this package, the method names you provide +as arguments cause subroutines to be generated and installed in +your module. + +See L for more information. + +=head2 Declaration Syntax + +To declare methods, pass in pairs of a method-type name followed +by one or more method names. + +Valid method-type names for this package are listed in L<"METHOD +GENERATOR TYPES">. + +See L and L for more information. + +=cut + +package Class::MakeMethods::Composite; + +$VERSION = 1.000; +use strict; +use Class::MakeMethods '-isasubclass'; +use Carp; + +######################################################################## + +=head2 About Composite Methods + +The methods generated by Class::MakeMethods::Composite are assembled +from groups of "fragment" subroutines, each of which provides some +aspect of the method's behavior. + +You can add pre- and post- operations to any composite method. + + package MyObject; + use Class::MakeMethods::Composite::Hash ( + new => 'new', + scalar => [ + 'foo' => { + 'pre_rules' => [ + sub { + # Don't automatically convert list to array-ref + croak "Too many arguments" if ( scalar @_ > 2 ); + } + ], + 'post_rules' => [ + sub { + # Don't let anyone see my credit card number! + ${(pop)->{result}} =~ s/\d{13,16}/****/g; + } + ], + } + ], + ); + +=cut + +use vars qw( $Method ); + +sub CurrentMethod { + $Method; +} + +sub CurrentResults { + my $package = shift; + if ( ! scalar @_ ) { + ( ! $Method->{result} ) ? () : + ( ref($Method->{result}) eq 'ARRAY' ) ? @{$Method->{result}} : + ${$Method->{result}}; + } elsif ( scalar @_ == 1) { + my $value = shift; + $Method->{result} = \$value; + $value + } else { + my @value = @_; + $Method->{result} = \@value; + @value; + } +} + +sub _build_composite { + my $class = shift; + my $fragments = shift; + map { + my $method = $_; + my @fragments = @{ $fragments->{''} }; + foreach my $flagname ( grep $method->{$_}, qw/ permit modifier / ) { + my $value = $method->{$flagname}; + my $fragment = $fragments->{$value} + or croak "Unsupported $flagname flag '$value'"; + push @fragments, @$fragment; + } + _bind_composite( $method, @fragments ); + } $class->_get_declarations(@_) +} + +sub _assemble_fragments { + my $method = shift; + my @fragments = @_; + while ( scalar @fragments ) { + my ($rule, $sub) = splice( @fragments, 0, 2 ); + if ( $rule =~ s/\A\+// ) { + unshift @{$method->{"${rule}_rules"}}, $sub + } elsif ( $rule =~ s/\+\Z// ) { + push @{$method->{"${rule}_rules"}}, $sub + } elsif ( $rule =~ /\A\w+\Z/ ) { + @{$method->{"${rule}_rules"}} = $sub; + } else { + croak "Unsupported rule type '$rule'" + } + } +} + +sub _bind_composite { + my $method = shift; + _assemble_fragments( $method, @_ ); + if ( my $subs = $method->{"init_rules"} ) { + foreach my $sub ( @$subs ) { + &$sub( $method ); + } + } + $method->{name} => sub { + local $Method = $method; + local $Method->{args} = [ @_ ]; + local $Method->{result}; + local $Method->{scratch}; + # Strange but true: you can local a hash-value in hash that's not + # a package variable. Confirmed in in 5.004, 5.005, 5.6.0. + + local $Method->{wantarray} = wantarray; + + if ( my $subs = $Method->{"pre_rules"} ) { + foreach my $sub ( @$subs ) { + &$sub( @{$Method->{args}}, $Method ); + } + } + + my $subs = $Method->{"do_rules"} + or Carp::confess("No operations provided for $Method->{name}"); + if ( ! defined $Method->{wantarray} ) { + foreach my $sub ( @$subs ) { + last if $Method->{result}; + &$sub( @{$Method->{args}}, $Method ); + } + } elsif ( ! $Method->{wantarray} ) { + foreach my $sub ( @$subs ) { + last if $Method->{result}; + my $value = &$sub( @{$Method->{args}}, $Method ); + if ( defined $value ) { + $Method->{result} = \$value; + } + } + } else { + foreach my $sub ( @$subs ) { + last if $Method->{result}; + my @value = &$sub( @{$Method->{args}}, $Method ); + if ( scalar @value ) { + $Method->{result} = \@value; + } + } + } + + if ( my $subs = $Method->{"post_rules"} ) { + foreach my $sub ( @$subs ) { + &$sub( @{$Method->{args}}, $Method ); + } + } + + ( ! $Method->{result} ) ? () : + ( ref($Method->{result}) eq 'ARRAY' ) ? @{$Method->{result}} : + ${$Method->{result}}; + } +} + +######################################################################## + +=head1 SEE ALSO + +See L for general information about this distribution. + +For distribution, installation, support, copyright and license +information, see L. + +=cut + +1; diff --git a/lib/Class/MakeMethods/Composite/Array.pm b/lib/Class/MakeMethods/Composite/Array.pm new file mode 100644 index 0000000..fe04eba --- /dev/null +++ b/lib/Class/MakeMethods/Composite/Array.pm @@ -0,0 +1,794 @@ +=head1 NAME + +Class::MakeMethods::Composite::Array - Basic array methods + +=head1 SYNOPSIS + + package MyObject; + use Class::MakeMethods::Composite::Array ( + new => 'new', + scalar => [ 'foo', 'bar' ], + array => 'my_list', + hash => 'my_index', + ); + ... + + my $obj = MyObject->new( foo => 'Foozle' ); + print $obj->foo(); + + $obj->bar('Barbados'); + print $obj->bar(); + + $obj->my_list(0 => 'Foozle', 1 => 'Bang!'); + print $obj->my_list(1); + + $obj->my_index('broccoli' => 'Blah!', 'foo' => 'Fiddle'); + print $obj->my_index('foo'); + +=head1 DESCRIPTION + +The Composite::Array suclass of MakeMethods provides a basic +constructor and accessors for blessed-array object instances. + +=head2 Class::MakeMethods Calling Conventions + +When you C this package, the method declarations you provide +as arguments cause subroutines to be generated and installed in +your module. + +You can also omit the arguments to C and instead make methods +at runtime by passing the declarations to a subsequent call to +C. + +You may include any number of declarations in each call to C +or C. If methods with the same name already exist, earlier +calls to C or C win over later ones, but within each +call, later declarations superceed earlier ones. + +You can install methods in a different package by passing C<-TargetClass =E I> as your first arguments to C or C. + +See L for more details. + +=head2 Class::MakeMethods::Basic Declaration Syntax + +The following types of Basic declarations are supported: + +=over 4 + +=item * + +I => "I" + +=item * + +I => "I I..." + +=item * + +I => [ "I", "I", ...] + +=back + +See the "METHOD GENERATOR TYPES" section below for a list of the supported values of I. + +For each method name you provide, a subroutine of the indicated +type will be generated and installed under that name in your module. + +Method names should start with a letter, followed by zero or more +letters, numbers, or underscores. + +=head2 Class::MakeMethods::Composite Declaration Syntax + +The Composite syntax also provides several ways to optionally +associate a hash of additional parameters with a given method +name. + +=over 4 + +=item * + +I => [ "I" => { I=>I... }, ... ] + +A hash of parameters to use just for this method name. + +(Note: to prevent confusion with self-contained definition hashes, +described below, parameter hashes following a method name must not +contain the key 'name'.) + +=item * + +I => [ [ "I", "I", ... ] => { I=>I... } ] + +Each of these method names gets a copy of the same set of parameters. + +=item * + +I => [ { "name"=>"I", I=>I... }, ... ] + +By including the reserved parameter C, you create a self +contained declaration with that name and any associated hash values. + +=back + +Basic declarations, as described above, are treated as having an empty parameter hash. + +=cut + +package Class::MakeMethods::Composite::Array; + +$VERSION = 1.000; +use strict; +use Class::MakeMethods::Composite '-isasubclass'; + +######################################################################## + +=head2 Positional Accessors and %FIELDS + +Each accessor method is assigned the next available array index at +which to store its value. + +The mapping between method names and array positions is stored in +a hash named %FIELDS in the declaring package. When a package +declares its first positional accessor, its %FIELDS are initialized +by searching its inheritance tree. + +B: Subclassing packages that use positional accessors is +somewhat fragile, since you may end up with two distinct methods assigned to the same position. Specific cases to avoid are: + +=over 4 + +=item * + +If you inherit from more than one class with positional accessors, +the positions used by the two sets of methods will overlap. + +=item * + +If your superclass adds additional positional accessors after you +declare your first, they will overlap yours. + +=back + +=cut + +sub _array_index { + my $class = shift; + my $name = shift; + no strict; + local $^W = 0; + if ( ! scalar %{$class . "::FIELDS"} ) { + my @classes = @{$class . "::ISA"}; + my @fields; + while ( @classes ) { + my $superclass = shift @classes; + if ( scalar %{$superclass . "::FIELDS"} ) { + push @fields, %{$superclass . "::FIELDS"}; + } else { + unshift @classes, @{$superclass . "::ISA"} + } + } + %{$class . "::FIELDS"} = @fields + } + my $field_hash = \%{$class . "::FIELDS"}; + $field_hash->{$name} or $field_hash->{$name} = scalar keys %$field_hash +} + +######################################################################## + +=head1 METHOD GENERATOR TYPES + +=head2 new - Constructor + +For each method name passed, returns a subroutine with the following characteristics: + +=over 4 + +=item * + +Has a reference to a sample item to copy. This defaults to a reference to an empty array, but you may override this with the C<'defaults' => I> method parameter. + +=item * + +If called as a class method, makes a new array containing values from the sample item, and blesses it into that class. + +=item * + +If called on an array-based instance, makes a copy of it and blesses the copy into the same class as the original instance. + +=item * + +If passed a list of method-value pairs, calls each named method with the associated value as an argument. + +=item * + +Returns the new instance. + +=back + +Sample declaration and usage: + + package MyObject; + use Class::MakeMethods::Composite::Array ( + new => 'new', + ); + ... + + # Bare constructor + my $empty = MyObject->new(); + + # Constructor with initial sequence of method calls + my $obj = MyObject->new( foo => 'Foozle', bar => 'Barbados' ); + + # Copy with overriding sequence of method calls + my $copy = $obj->new( bar => 'Bob' ); + +=cut + +use vars qw( %ConstructorFragments ); + +sub new { + (shift)->_build_composite( \%ConstructorFragments, @_ ); +} + +%ConstructorFragments = ( + '' => [ + '+init' => sub { + my $method = pop @_; + $method->{target_class} ||= $Class::MethodMaker::CONTEXT{TargetClass}; + $method->{defaults} ||= []; + }, + 'do' => sub { + my $method = pop @_; + my $self = shift @_; + my $obj = ref($self) ? bless( [ @$self ], ref $self ) + : bless( { @[$method->{defaults}] }, $self ); + @_ = %{$_[0]} + if ( scalar @_ == 1 and ref $_[0] eq 'HASH' ); + while ( scalar @_ ) { + my $method = shift @_; + $obj->$method( shift @_ ); + } + $obj; + }, + ], + 'with_values' => [ + 'do' => sub { + my $method = pop @_; + my $self = shift @_; + @_ = @[$_[0]] + if ( scalar @_ == 1 and ref $_[0] eq 'ARRAY' ); + bless( [ @_ ], ref($self) || $self ); + } + ], +); + +######################################################################## + +=head2 new_with_values - Constructor + +For each method name passed, returns a subroutine with the following characteristics: + +=over 4 + +=item * + +May be called as a class method, or (equivalently) on any existing object of that class. + +=item * + +Creates an array, blesses it into the class, and returns the new instance. + +=item * + +If no arguments are provided, the returned array will be empty. If passed a single array-ref argument, copies its contents into the new array. If called with multiple arguments, copies them into the new array. (Note that this is a "shallow" copy, not a "deep" clone.) + +=back + +Sample declaration and usage: + + package MyObject; + use Class::MakeMethods::Composite::Array ( + new => 'new', + ); + ... + + # Bare constructor + my $empty = MyObject->new(); + + # Constructor with initial sequence of method calls + my $obj = MyObject->new( foo => 'Foozle', bar => 'Barbados' ); + + # Copy with overriding sequence of method calls + my $copy = $obj->new( bar => 'Bob' ); + +=cut + +######################################################################## + +=head2 scalar - Instance Accessor + +For each method name passed, uses a closure to generate a subroutine with the following characteristics: + +=over 4 + +=item * + +Must be called on an array-based instance. + +=item * + +Determines the array position associated with the method name, and uses that as an index into each instance to access the related value. This defaults to the next available slot in %FIELDS, but you may override this with the C<'array_index' => I> method parameter, or by pre-filling the contents of %FIELDS. + +=item * + +If called without any arguments returns the current value (or undef). + +=item * + +If called with an argument, stores that as the value, and returns it, + +=item * + +If called with multiple arguments, stores a reference to a new array with those arguments as contents, and returns that array reference. + +=back + +Sample declaration and usage: + + package MyObject; + use Class::MakeMethods::Composite::Array ( + scalar => 'foo', + ); + ... + + # Store value + $obj->foo('Foozle'); + + # Retrieve value + print $obj->foo; + +=cut + +use vars qw( %ScalarFragments ); + +sub scalar { + (shift)->_build_composite( \%ScalarFragments, @_ ); +} + +%ScalarFragments = ( + '' => [ + '+init' => sub { + my ($method) = @_; + $method->{target_class} ||= $Class::MethodMaker::CONTEXT{TargetClass}; + $method->{array_index} ||= + _array_index( $method->{target_class}, $name ); + }, + 'do' => sub { + my $method = pop @_; + my $self = shift @_; + if ( scalar(@_) == 0 ) { + $self->[$method->{array_index}]; + } elsif ( scalar(@_) == 1 ) { + $self->[$method->{array_index}] = shift; + } else { + $self->[$method->{array_index}] = [@_]; + } + }, + ], + 'rw' => [], + 'p' => [ + '+pre' => sub { + my $method = pop @_; + my $self = shift @_; + my $args = \@_; + unless ( UNIVERSAL::isa((caller(1))[0], $method->{target_class}) ) { + croak "Method $method->{name} is protected"; + } + }, + ], + 'pp' => [ + '+pre' => sub { + my $method = pop @_; + my $self = shift @_; + my $args = \@_; + unless ( (caller(1))[0] eq $method->{target_class} ) { + croak "Method $method->{name} is private"; + } + }, + ], + 'pw' => [ + '+pre' => sub { + my $method = pop @_; + my $self = shift @_; + my $args = \@_; + unless ( @$args == 0 or UNIVERSAL::isa((caller(1))[0], $method->{target_class}) ) { + croak "Method $method->{name} is write-protected"; + } + }, + ], + 'ppw' => [ + '+pre' => sub { + my $method = pop @_; + my $self = shift @_; + my $args = \@_; + unless ( @$args == 0 or (caller(1))[0] eq $method->{target_class} ) { + croak "Method $method->{name} is write-private"; + } + }, + ], + 'r' => [ + '+pre' => sub { + my $method = pop @_; + my $self = shift @_; + my $args = \@_; + @$args = (); + }, + ], + 'ro' => [ + '+pre' => sub { + my $method = pop @_; + my $self = shift @_; + my $args = \@_; + unless ( @$args == 0 ) { + croak("Method $method->{name} is read-only"); + } + }, + ], + 'wo' => [ + '+pre' => sub { + my $method = pop @_; + my $self = shift @_; + my $args = \@_; + if ( @$args == 0 ) { + croak("Method $method->{name} is write-only"); + } + }, + ], + 'return_original' => [ + '+pre' => sub { + my $method = pop @_; + my $self = shift @_; + my $args = \@_; + $method->{scratch}{return_original} = $self->[$method->{array_index}]; + }, + '+post' => sub { + my $method = pop @_; + my $self = shift @_; + my $args = \@_; + $method->{result} = \{ $method->{scratch}{return_original} }; + }, + ], +); + +######################################################################## + +=head2 array - Instance Ref Accessor + +For each method name passed, uses a closure to generate a subroutine with the following characteristics: + +=over 4 + +=item * + +Must be called on an array-based instance. + +=item * + +Determines the array position associated with the method name, and uses that as an index into each instance to access the related value. This defaults to the next available slot in %FIELDS, but you may override this with the C<'array_index' => I> method parameter, or by pre-filling the contents of %FIELDS. + +=item * + +The value for each instance will be a reference to an array (or undef). + +=item * + +If called without any arguments, returns the current array-ref value (or undef). + +=item * + +If called with a single non-ref argument, uses that argument as an index to retrieve from the referenced array, and returns that value (or undef). + +=item * + +If called with a single array ref argument, uses that list to return a slice of the referenced array. + +=item * + +If called with a list of argument pairs, each with a non-ref index and an associated value, stores the value at the given index in the referenced array. If the instance's value was previously undefined, a new array is autovivified. The current value in each position will be overwritten, and later arguments with the same index will override earlier ones. Returns the current array-ref value. + +=item * + +If called with a list of argument pairs, each with the first item being a reference to an array of up to two numbers, loops over each pair and uses those numbers to splice the value array. + +The first controlling number is the position at which the splice will begin. Zero will start before the first item in the list. Negative numbers count backwards from the end of the array. + +The second number is the number of items to be removed from the list. If it is omitted, or undefined, or zero, no items are removed. If it is a positive integer, that many items will be returned. + +If both numbers are omitted, or are both undefined, they default to containing the entire value array. + +If the second argument is undef, no values will be inserted; if it is a non-reference value, that one value will be inserted; if it is an array-ref, its values will be copied. + +The method returns the items that removed from the array, if any. + +=back + +Sample declaration and usage: + + package MyObject; + use Class::MakeMethods::Composite::Array ( + array => 'bar', + ); + ... + + # Clear and set contents of list + print $obj->bar([ 'Spume', 'Frost' ] ); + + # Set values by position + $obj->bar(0 => 'Foozle', 1 => 'Bang!'); + + # Positions may be overwritten, and in any order + $obj->bar(2 => 'And Mash', 1 => 'Blah!'); + + # Retrieve value by position + print $obj->bar(1); + + # Direct access to referenced array + print scalar @{ $obj->bar() }; + +There are also calling conventions for slice and splice operations: + + # Retrieve slice of values by position + print join(', ', $obj->bar( undef, [0, 2] ) ); + + # Insert an item at position in the array + $obj->bar([3], 'Potatoes' ); + + # Remove 1 item from position 3 in the array + $obj->bar([3, 1], undef ); + + # Set a new value at position 2, and return the old value + print $obj->bar([2, 1], 'Froth' ); + +=cut + + +use vars qw( %ArrayFragments ); + +sub array { + (shift)->_build_composite( \%ArrayFragments, @_ ); +} + +%ArrayFragments = ( + '' => [ + '+init' => sub { + my ($method) = @_; + $method->{target_class} ||= $Class::MethodMaker::CONTEXT{TargetClass}; + $method->{array_index} ||= + _array_index( $method->{target_class}, $name ); + }, + 'do' => sub { + my $method = pop @_; + my $self = shift @_; + my $args = \@_; + if ( scalar(@$args) == 0 ) { + if ( $method->{auto_init} and + ! defined $self->[$method->{array_index}] ) { + $self->[$method->{array_index}] = []; + } + wantarray ? @{ $self->[$method->{array_index}] } : $self->[$method->{array_index}]; + } elsif ( scalar(@_) == 1 and ref $_[0] eq 'ARRAY' ) { + $self->[$method->{array_index}] = [ @{ $_[0] } ]; + wantarray ? @{ $self->[$method->{array_index}] } : $self->[$method->{array_index}]; + } else { + $self->[$method->{array_index}] ||= []; + Class::MakeMethods::Composite::__array_ops( + $self->[$method->{array_index}], @$args ); + } + }, + ], +); + +######################################################################## + +=head2 hash - Instance Ref Accessor + +For each method name passed, uses a closure to generate a subroutine with the following characteristics: + +=over 4 + +=item * + +Must be called on an array-based instance. + +=item * + +Determines the array position associated with the method name, and uses that as an index into each instance to access the related value. This defaults to the next available slot in %FIELDS, but you may override this with the C<'array_index' => I> method parameter, or by pre-filling the contents of %FIELDS. + +=item * + +The value for each instance will be a reference to a hash (or undef). + +=item * + +If called without any arguments, returns the contents of the hash in list context, or a hash reference in scalar context (or undef). + +=item * + +If called with one non-ref argument, uses that argument as an index to retrieve from the referenced hash, and returns that value (or undef). + +=item * + +If called with one array-ref argument, uses the contents of that array to retrieve a slice of the referenced hash. + +=item * + +If called with one hash-ref argument, sets the contents of the referenced hash to match that provided. + +=item * + +If called with a list of key-value pairs, stores the value under the given key in the referenced hash. If the instance's value was previously undefined, a new hash is autovivified. The current value under each key will be overwritten, and later arguments with the same key will override earlier ones. Returns the contents of the hash in list context, or a hash reference in scalar context. + +=back + +Sample declaration and usage: + + package MyObject; + use Class::MakeMethods::Composite::Array ( + hash => 'baz', + ); + ... + + # Set values by key + $obj->baz('foo' => 'Foozle', 'bar' => 'Bang!'); + + # Values may be overwritten, and in any order + $obj->baz('broccoli' => 'Blah!', 'foo' => 'Fiddle'); + + # Retrieve value by key + print $obj->baz('foo'); + + # Retrive slice of values by position + print join(', ', $obj->baz( ['foo', 'bar'] ) ); + + # Direct access to referenced hash + print keys %{ $obj->baz() }; + + # Reset the hash contents to empty + @{ $obj->baz() } = (); + +=cut + + +use vars qw( %HashFragments ); + +sub hash { + (shift)->_build_composite( \%HashFragments, @_ ); +} + +%HashFragments = ( + '' => [ + '+init' => sub { + my ($method) = @_; + $method->{hash_key} ||= $_->{name}; + }, + 'do' => sub { + my $method = pop @_; + my $self = shift @_; + my $args = \@_; + if ( scalar(@$args) == 0 ) { + if ( $method->{auto_init} and ! defined $self->[$method->{array_index}] ) { + $self->[$method->{array_index}] = {}; + } + wantarray ? %{ $self->[$method->{array_index}] } : $self->[$method->{array_index}]; + } elsif ( scalar(@$args) == 1 ) { + if ( ref($_[0]) eq 'HASH' ) { + %{$self->[$method->{array_index}]} = %{$_[0]}; + } elsif ( ref($_[0]) eq 'ARRAY' ) { + return @{$self->[$method->{array_index}]}{ @{$_[0]} } + } else { + return $self->[$method->{array_index}]->{ $_[0] } + } + } elsif ( scalar(@$args) % 2 ) { + croak "Odd number of items in assigment to $method->{name}"; + } else { + while ( scalar(@$args) ) { + my $key = shift @$args; + $self->[$method->{array_index}]->{ $key} = shift @$args; + } + wantarray ? %{ $self->[$method->{array_index}] } : $self->[$method->{array_index}]; + } + }, + ], +); + +######################################################################## + +=head2 object - Instance Ref Accessor + +For each method name passed, uses a closure to generate a subroutine with the following characteristics: + +=over 4 + +=item * + +Must be called on an array-based instance. + +=item * + +Determines the array position associated with the method name, and uses that as an index into each instance to access the related value. This defaults to the next available slot in %FIELDS, but you may override this with the C<'array_index' => I> method parameter, or by pre-filling the contents of %FIELDS. + +=item * + +The value for each instance will be a reference to an object (or undef). + +=item * + +If called without any arguments returns the current value. + +=item * + +If called with an argument, stores that as the value, and returns it, + +=back + +Sample declaration and usage: + + package MyObject; + use Class::MakeMethods::Composite::Hash ( + object => 'foo', + ); + ... + + # Store value + $obj->foo( Foozle->new() ); + + # Retrieve value + print $obj->foo; + +=cut + +use vars qw( %ObjectFragments ); + +sub object { + (shift)->_build_composite( \%ObjectFragments, @_ ); +} + +%ObjectFragments = ( + '' => [ + '+init' => sub { + my ($method) = @_; + $method->{hash_key} ||= $_->{name}; + }, + 'do' => sub { + my $method = pop @_; + my $self = shift; + if ( scalar @_ ) { + my $value = shift; + if ( $method->{class} and ! UNIVERSAL::isa( $value, $method->{class} ) ) { + croak "Wrong argument type ('$value') in assigment to $method->{name}"; + } + $self->[$method->{array_index}] = $value; + } else { + if ( $method->{auto_init} and ! defined $self->[$method->{array_index}] ) { + my $class = $method->{class} + or die "Can't auto_init without a class"; + my $new_method = $method->{new_method} || 'new'; + $self->[$method->{array_index}] = $class->$new_method(); + } + $self->[$method->{array_index}]; + } + }, + ], +); + +######################################################################## + +=head1 SEE ALSO + +See L for general information about this distribution. + +See L for more about this family of subclasses. + +=cut + +1; diff --git a/lib/Class/MakeMethods/Composite/Global.pm b/lib/Class/MakeMethods/Composite/Global.pm new file mode 100644 index 0000000..cf9af0b --- /dev/null +++ b/lib/Class/MakeMethods/Composite/Global.pm @@ -0,0 +1,588 @@ +=head1 NAME + +Class::MakeMethods::Composite::Global - Global data + +=head1 SYNOPSIS + + package MyClass; + use Class::MakeMethods::Composite::Global ( + scalar => [ 'foo' ], + array => [ 'my_list' ], + hash => [ 'my_index' ], + ); + ... + + MyClass->foo( 'Foozle' ); + print MyClass->foo(); + + print MyClass->new(...)->foo(); # same value for any instance + print MySubclass->foo(); # ... and for any subclass + + MyClass->my_list(0 => 'Foozle', 1 => 'Bang!'); + print MyClass->my_list(1); + + MyClass->my_index('broccoli' => 'Blah!', 'foo' => 'Fiddle'); + print MyClass->my_index('foo'); + + +=head1 DESCRIPTION + +The Composite::Global suclass of MakeMethods provides basic accessors for shared data. + +=head2 Class::MakeMethods Calling Interface + +When you C this package, the method declarations you provide +as arguments cause subroutines to be generated and installed in +your module. + +You can also omit the arguments to C and instead make methods +at runtime by passing the declarations to a subsequent call to +C. + +You may include any number of declarations in each call to C +or C. If methods with the same name already exist, earlier +calls to C or C win over later ones, but within each +call, later declarations superceed earlier ones. + +You can install methods in a different package by passing C<-TargetClass =E I> as your first arguments to C or C. + +See L for more details. + +=head2 Class::MakeMethods::Basic Declaration Syntax + +The following types of Basic declarations are supported: + +=over 4 + +=item * + +I => "I" + +=item * + +I => "I I..." + +=item * + +I => [ "I", "I", ...] + +=back + +See the "METHOD GENERATOR TYPES" section below for a list of the supported values of I. + +For each method name you provide, a subroutine of the indicated +type will be generated and installed under that name in your module. + +Method names should start with a letter, followed by zero or more +letters, numbers, or underscores. + +=head2 Class::MakeMethods::Composite Declaration Syntax + +The Composite syntax also provides several ways to optionally +associate a hash of additional parameters with a given method +name. + +=over 4 + +=item * + +I => [ "I" => { I=>I... }, ... ] + +A hash of parameters to use just for this method name. + +(Note: to prevent confusion with self-contained definition hashes, +described below, parameter hashes following a method name must not +contain the key 'name'.) + +=item * + +I => [ [ "I", "I", ... ] => { I=>I... } ] + +Each of these method names gets a copy of the same set of parameters. + +=item * + +I => [ { "name"=>"I", I=>I... }, ... ] + +By including the reserved parameter C, you create a self-contained declaration with that name and any associated hash values. + +=back + +Basic declarations, as described above, are given an empty parameter hash. + +=cut + +package Class::MakeMethods::Composite::Global; + +$VERSION = 1.000; +use strict; +use Class::MakeMethods::Composite '-isasubclass'; + +######################################################################## + +=head1 METHOD GENERATOR TYPES + +=head2 scalar - Global Accessor + +For each method name passed, uses a closure to generate a subroutine with the following characteristics: + +=over 4 + +=item * + +May be called as a class method, or on any instance or subclass, and behaves identically regardless of what it was called on. + +=item * + +If called without any arguments returns the current value. + +=item * + +If called with an argument, stores that as the value, and returns it, + +=item * + +If called with multiple arguments, stores a reference to a new array with those arguments as contents, and returns that array reference. + +=back + +Sample declaration and usage: + + package MyClass; + use Class::MakeMethods::Composite::Global ( + scalar => 'foo', + ); + ... + + # Store value + MyClass->foo('Foozle'); + + # Retrieve value + print MyClass->foo; + +=cut + +use vars qw( %ScalarFragments ); + +sub scalar { + (shift)->_build_composite( \%ScalarFragments, @_ ); +} + +%ScalarFragments = ( + '' => [ + '+init' => sub { + my ($method) = @_; + $method->{target_class} ||= $Class::MethodMaker::CONTEXT{TargetClass}; + $method->{array_index} ||= + _array_index( $method->{target_class}, $name ); + }, + 'do' => sub { + my $method = pop @_; + my $self = shift @_; + if ( scalar(@_) == 0 ) { + $method->{global_data}; + } elsif ( scalar(@_) == 1 ) { + $method->{global_data} = shift; + } else { + $method->{global_data} = [@_]; + } + }, + ], + 'rw' => [], + 'p' => [ + '+pre' => sub { + my $method = pop @_; + unless ( UNIVERSAL::isa((caller(1))[0], $method->{target_class}) ) { + croak "Method $method->{name} is protected"; + } + }, + ], + 'pp' => [ + '+pre' => sub { + my $method = pop @_; + unless ( (caller(1))[0] eq $method->{target_class} ) { + croak "Method $method->{name} is private"; + } + }, + ], + 'pw' => [ + '+pre' => sub { + my $method = pop @_; + my $self = shift @_; + my $args = \@_; + unless ( @$args == 0 or UNIVERSAL::isa((caller(1))[0], $method->{target_class}) ) { + croak "Method $method->{name} is write-protected"; + } + }, + ], + 'ppw' => [ + '+pre' => sub { + my $method = pop @_; + my $self = shift @_; + my $args = \@_; + unless ( @$args == 0 or (caller(1))[0] eq $method->{target_class} ) { + croak "Method $method->{name} is write-private"; + } + }, + ], + 'r' => [ + '+pre' => sub { + my $method = pop @_; + my $self = shift @_; + @{$method->{args}} = ($self) if ( scalar @_ ); + }, + ], + 'ro' => [ + '+pre' => sub { + my $method = pop @_; + my $self = shift @_; + my $args = \@_; + unless ( @$args == 0 ) { + croak("Method $method->{name} is read-only"); + } + }, + ], + 'wo' => [ + '+pre' => sub { + my $method = pop @_; + my $self = shift @_; + my $args = \@_; + if ( @$args == 0 ) { + croak("Method $method->{name} is write-only"); + } + }, + ], + 'return_original' => [ + '+pre' => sub { + my $method = pop @_; + my $self = shift @_; + my $args = \@_; + $method->{scratch}{return_original} = $method->{global_data}; + }, + '+post' => sub { + my $method = pop @_; + my $self = shift @_; + my $args = \@_; + $method->{result} = \{ $method->{scratch}{return_original} }; + }, + ], +); + +######################################################################## + +=head2 array - Global Ref Accessor + +For each method name passed, uses a closure to generate a subroutine with the following characteristics: + +=over 4 + +=item * + +May be called as a class method, or on any instance or subclass, and behaves identically regardless of what it was called on. + +=item * + +The global value will be a reference to an array (or undef). + +=item * + +If called without any arguments, returns the current array-ref value (or undef). + + +=item * + +If called with a single non-ref argument, uses that argument as an index to retrieve from the referenced array, and returns that value (or undef). + +=item * + +If called with a single array ref argument, uses that list to return a slice of the referenced array. + +=item * + +If called with a list of argument pairs, each with a non-ref index and an associated value, stores the value at the given index in the referenced array. If the global value was previously undefined, a new array is autovivified. The current value in each position will be overwritten, and later arguments with the same index will override earlier ones. Returns the current array-ref value. + +=item * + +If called with a list of argument pairs, each with the first item being a reference to an array of up to two numbers, loops over each pair and uses those numbers to splice the value array. + +The first controlling number is the position at which the splice will begin. Zero will start before the first item in the list. Negative numbers count backwards from the end of the array. + +The second number is the number of items to be removed from the list. If it is omitted, or undefined, or zero, no items are removed. If it is a positive integer, that many items will be returned. + +If both numbers are omitted, or are both undefined, they default to containing the entire value array. + +If the second argument is undef, no values will be inserted; if it is a non-reference value, that one value will be inserted; if it is an array-ref, its values will be copied. + +The method returns the items that removed from the array, if any. + +=back + +Sample declaration and usage: + + package MyClass; + use Class::MakeMethods::Composite::Global ( + array => 'bar', + ); + ... + + # Clear and set contents of list + print MyClass->bar([ 'Spume', 'Frost' ] ); + + # Set values by position + MyClass->bar(0 => 'Foozle', 1 => 'Bang!'); + + # Positions may be overwritten, and in any order + MyClass->bar(2 => 'And Mash', 1 => 'Blah!'); + + # Retrieve value by position + print MyClass->bar(1); + + # Direct access to referenced array + print scalar @{ MyClass->bar() }; + +There are also calling conventions for slice and splice operations: + + # Retrieve slice of values by position + print join(', ', MyClass->bar( undef, [0, 2] ) ); + + # Insert an item at position in the array + MyClass->bar([3], 'Potatoes' ); + + # Remove 1 item from position 3 in the array + MyClass->bar([3, 1], undef ); + + # Set a new value at position 2, and return the old value + print MyClass->bar([2, 1], 'Froth' ); + +=cut + + +use vars qw( %ArrayFragments ); + +sub array { + (shift)->_build_composite( \%ArrayFragments, @_ ); +} + +%ArrayFragments = ( + '' => [ + '+init' => sub { + my ($method) = @_; + $method->{hash_key} ||= $_->{name}; + }, + 'do' => sub { + my $method = pop @_; + my $self = shift @_; + my $args = \@_; + if ( scalar(@$args) == 0 ) { + if ( $method->{auto_init} and + ! defined $method->{global_data} ) { + $method->{global_data} = []; + } + wantarray ? @{ $method->{global_data} } : $method->{global_data} + } elsif ( scalar(@_) == 1 and ref $_[0] eq 'ARRAY' ) { + $method->{global_data} = [ @{ $_[0] } ]; + wantarray ? @{ $method->{global_data} } : $method->{global_data} + } else { + $method->{global_data} ||= []; + Class::MakeMethods::Composite::__array_ops( + $method->{global_data}, @$args ); + } + }, + ], +); + +######################################################################## + +=head2 hash - Global Ref Accessor + +For each method name passed, uses a closure to generate a subroutine with the following characteristics: + +=over 4 + +=item * + +May be called as a class method, or on any instance or subclass, and behaves identically regardless of what it was called on. + +=item * + +The global value will be a reference to a hash (or undef). + +=item * + +If called without any arguments, returns the contents of the hash in list context, or a hash reference in scalar context (or undef). + +=item * + +If called with one non-ref argument, uses that argument as an index to retrieve from the referenced hash, and returns that value (or undef). + +=item * + +If called with one array-ref argument, uses the contents of that array to retrieve a slice of the referenced hash. + +=item * + +If called with one hash-ref argument, sets the contents of the referenced hash to match that provided. + +=item * + +If called with a list of key-value pairs, stores the value under the given key in the referenced hash. If the global value was previously undefined, a new hash is autovivified. The current value under each key will be overwritten, and later arguments with the same key will override earlier ones. Returns the contents of the hash in list context, or a hash reference in scalar context. + +=back + +Sample declaration and usage: + + package MyClass; + use Class::MakeMethods::Composite::Global ( + hash => 'baz', + ); + ... + + # Set values by key + MyClass->baz('foo' => 'Foozle', 'bar' => 'Bang!'); + + # Values may be overwritten, and in any order + MyClass->baz('broccoli' => 'Blah!', 'foo' => 'Fiddle'); + + # Retrieve value by key + print MyClass->baz('foo'); + + # Retrive slice of values by position + print join(', ', MyClass->baz( ['foo', 'bar'] ) ); + + # Direct access to referenced hash + print keys %{ MyClass->baz() }; + + # Reset the hash contents to empty + @{ MyClass->baz() } = (); + +=cut + +use vars qw( %HashFragments ); + +sub hash { + (shift)->_build_composite( \%HashFragments, @_ ); +} + +%HashFragments = ( + '' => [ + '+init' => sub { + my ($method) = @_; + $method->{hash_key} ||= $_->{name}; + }, + 'do' => sub { + my $method = pop @_; + my $self = shift @_; + my $args = \@_; + if ( scalar(@$args) == 0 ) { + if ( $method->{auto_init} and ! defined $method->{global_data} ) { + $method->{global_data} = {}; + } + wantarray ? %{ $method->{global_data} } : $method->{global_data}; + } elsif ( scalar(@$args) == 1 ) { + if ( ref($_[0]) eq 'HASH' ) { + %{$method->{global_data}} = %{$_[0]}; + } elsif ( ref($_[0]) eq 'ARRAY' ) { + return @{$method->{global_data}}{ @{$_[0]} } + } else { + return $method->{global_data}->{ $_[0] } + } + } elsif ( scalar(@$args) % 2 ) { + croak "Odd number of items in assigment to $method->{name}"; + } else { + while ( scalar(@$args) ) { + my $key = shift @$args; + $method->{global_data}->{ $key} = shift @$args; + } + wantarray ? %{ $method->{global_data} } : $method->{global_data}; + } + }, + ], +); + +######################################################################## + +=head2 object - Global Ref Accessor + +For each method name passed, uses a closure to generate a subroutine with the following characteristics: + +=over 4 + +=item * + +May be called as a class method, or on any instance or subclass, and behaves identically regardless of what it was called on. + +=item * + +The global value will be a reference to an object (or undef). + +=item * + +If called without any arguments returns the current value. + +=item * + +If called with an argument, stores that as the value, and returns it, + +=back + +Sample declaration and usage: + + package MyClass; + use Class::MakeMethods::Composite::Global ( + object => 'foo', + ); + ... + + # Store value + MyClass->foo( Foozle->new() ); + + # Retrieve value + print MyClass->foo; + +=cut + +use vars qw( %ObjectFragments ); + +sub object { + (shift)->_build_composite( \%ObjectFragments, @_ ); +} + +%ObjectFragments = ( + '' => [ + '+init' => sub { + my ($method) = @_; + $method->{hash_key} ||= $_->{name}; + }, + 'do' => sub { + my $method = pop @_; + my $self = shift; + if ( scalar @_ ) { + my $value = shift; + if ( $method->{class} and ! UNIVERSAL::isa( $value, $method->{class} ) ) { + croak "Wrong argument type ('$value') in assigment to $method->{name}"; + } + $method->{global_data} = $value; + } else { + if ( $method->{auto_init} and ! defined $method->{global_data} ) { + my $class = $method->{class} + or die "Can't auto_init without a class"; + my $new_method = $method->{new_method} || 'new'; + $method->{global_data} = $class->$new_method(); + } + $method->{global_data}; + } + }, + ], +); + +######################################################################## + +=head1 SEE ALSO + +See L for general information about this distribution. + +See L for more about this family of subclasses. + +=cut + +1; diff --git a/lib/Class/MakeMethods/Composite/Hash.pm b/lib/Class/MakeMethods/Composite/Hash.pm new file mode 100644 index 0000000..969bdd0 --- /dev/null +++ b/lib/Class/MakeMethods/Composite/Hash.pm @@ -0,0 +1,719 @@ +=head1 NAME + +Class::MakeMethods::Composite::Hash - Composite hash methods + +=head1 SYNOPSIS + + package MyObject; + use Class::MakeMethods::Composite::Hash ( + new => 'new', + scalar => [ 'foo', 'bar' ], + array => 'my_list', + hash => 'my_index', + ); + ... + + my $obj = MyObject->new( foo => 'Foozle' ); + print $obj->foo(); + + $obj->bar('Barbados'); + print $obj->bar(); + + $obj->my_list(0 => 'Foozle', 1 => 'Bang!'); + print $obj->my_list(1); + + $obj->my_index('broccoli' => 'Blah!', 'foo' => 'Fiddle'); + print $obj->my_index('foo'); + +=head1 DESCRIPTION + +The Composite::Hash suclass of MakeMethods provides a basic constructor and accessors for blessed-hash object instances. + +=head2 Class::MakeMethods Calling Interface + +When you C this package, the method declarations you provide +as arguments cause subroutines to be generated and installed in +your module. + +You can also omit the arguments to C and instead make methods +at runtime by passing the declarations to a subsequent call to +C. + +You may include any number of declarations in each call to C +or C. If methods with the same name already exist, earlier +calls to C or C win over later ones, but within each +call, later declarations superceed earlier ones. + +You can install methods in a different package by passing C<-TargetClass =E I> as your first arguments to C or C. + +See L for more details. + +=head2 Class::MakeMethods::Basic Declaration Syntax + +The following types of Basic declarations are supported: + +=over 4 + +=item * + +I => "I" + +=item * + +I => "I I..." + +=item * + +I => [ "I", "I", ...] + +=back + +See the "METHOD GENERATOR TYPES" section below for a list of the supported values of I. + +For each method name you provide, a subroutine of the indicated +type will be generated and installed under that name in your module. + +Method names should start with a letter, followed by zero or more +letters, numbers, or underscores. + +=head2 Class::MakeMethods::Composite Declaration Syntax + +The Composite syntax also provides several ways to optionally +associate a hash of additional parameters with a given method +name. + +=over 4 + +=item * + +I => [ "I" => { I=>I... }, ... ] + +A hash of parameters to use just for this method name. + +(Note: to prevent confusion with self-contained definition hashes, +described below, parameter hashes following a method name must not +contain the key 'name'.) + +=item * + +I => [ [ "I", "I", ... ] => { I=>I... } ] + +Each of these method names gets a copy of the same set of parameters. + +=item * + +I => [ { "name"=>"I", I=>I... }, ... ] + +By including the reserved parameter C, you create a self-contained declaration with that name and any associated hash values. + +=back + +Basic declarations, as described above, are given an empty parameter hash. + +=cut + +package Class::MakeMethods::Composite::Hash; + +$VERSION = 1.000; +use strict; +use Class::MakeMethods::Composite '-isasubclass'; +use Carp; + +######################################################################## + +=head1 METHOD GENERATOR TYPES + +=head2 new - Constructor + +For each method name passed, returns a subroutine with the following characteristics: + +=over 4 + +=item * + +Has a reference to a sample item to copy. This defaults to a reference to an empty hash, but you may override this with the C<'defaults' =E I> method parameter. + +=item * + +If called as a class method, makes a new hash and blesses it into that class. + +=item * + +If called on a hash-based instance, makes a copy of it and blesses the copy into the same class as the original instance. + +=item * + +If passed a list of key-value pairs, appends them to the new hash. These arguments override any copied values, and later arguments with the same name will override earlier ones. + +=item * + +Returns the new instance. + +=back + +Sample declaration and usage: + + package MyObject; + use Class::MakeMethods::Composite::Hash ( + new => 'new', + ); + ... + + # Bare constructor + my $empty = MyObject->new(); + + # Constructor with initial values + my $obj = MyObject->new( foo => 'Foozle', bar => 'Barbados' ); + + # Copy with overriding value + my $copy = $obj->new( bar => 'Bob' ); + +=cut + +=head2 new --with_values - Constructor + +For each method name passed, returns a subroutine with the following characteristics: + +=over 4 + +=item * + +May be called as a class method, or (equivalently) on any existing object of that class. + +=item * + +Creates a hash, blesses it into the class, and returns the new instance. + +=item * + +If no arguments are provided, the returned hash will be empty. If passed a single hash-ref argument, copies its contents into the new hash. If called with multiple arguments, treats them as key-value pairs, and copies them into the new hash. (Note that this is a "shallow" copy, not a "deep" clone.) + +=back + +=cut + +use vars qw( %ConstructorFragments ); + +sub new { + (shift)->_build_composite( \%ConstructorFragments, @_ ); +} + +%ConstructorFragments = ( + '' => [ + '+init' => sub { + my $method = pop @_; + $method->{target_class} ||= $Class::MethodMaker::CONTEXT{TargetClass}; + $method->{defaults} ||= {}; + }, + 'do' => sub { + my $method = pop @_; + my $self = shift @_; + my $obj = ref($self) ? bless( { %$self }, ref $self ) + : bless( { %{$method->{defaults}} }, $self ); + @_ = %{$_[0]} + if ( scalar @_ == 1 and ref $_[0] eq 'HASH' ); + while ( scalar @_ ) { + my $method = shift @_; + my $value = shift @_; + $obj->$method( $value ); + } + $obj; + }, + ], + 'with_values' => [ + 'do' => sub { + my $method = pop @_; + my $self = shift @_; + @_ = %{$_[0]} + if ( scalar @_ == 1 and ref $_[0] eq 'HASH' ); + bless( { @_ }, ref($self) || $self ); + } + ], +); + +######################################################################## + +=head2 scalar - Instance Accessor + +For each method name passed, uses a closure to generate a subroutine with the following characteristics: + +=over 4 + +=item * + +Must be called on a hash-based instance. + +=item * + +Has a specific hash key to use to access the related value for each instance. +This defaults to the method name, but you may override this with the C<'hash_key' => I> method parameter. + +=item * + +If called without any arguments returns the current value. + +=item * + +If called with an argument, stores that as the value, and returns it. + +=item * + +If called with multiple arguments, stores a reference to a new array with those arguments as contents, and returns that array reference. + +=back + +Sample declaration and usage: + + package MyObject; + use Class::MakeMethods::Composite::Hash ( + scalar => 'foo', + ); + ... + + # Store value + $obj->foo('Foozle'); + + # Retrieve value + print $obj->foo; + +=cut + +use vars qw( %ScalarFragments ); + +sub scalar { + (shift)->_build_composite( \%ScalarFragments, @_ ); +} + +%ScalarFragments = ( + '' => [ + '+init' => sub { + my ($method) = @_; + $method->{hash_key} ||= $method->{name}; + $method->{target_class} ||= $Class::MethodMaker::CONTEXT{TargetClass}; + }, + 'do' => sub { + my $method = pop @_; + my $self = shift @_; + if ( scalar(@_) == 0 ) { + $self->{$method->{hash_key}}; + } elsif ( scalar(@_) == 1 ) { + $self->{$method->{hash_key}} = shift; + } else { + $self->{$method->{hash_key}} = [@_]; + } + }, + ], + 'rw' => [], + 'p' => [ + '+pre' => sub { + my $method = pop @_; + my $self = shift @_; + unless ( UNIVERSAL::isa((caller(1))[0], $method->{target_class}) ) { + croak "Method $method->{name} is protected"; + } + }, + ], + 'pp' => [ + '+pre' => sub { + my $method = pop @_; + my $self = shift @_; + unless ( (caller(1))[0] eq $method->{target_class} ) { + croak "Method $method->{name} is private"; + } + }, + ], + 'pw' => [ + '+pre' => sub { + my $method = pop @_; + my $self = shift @_; + my $args = \@_; + unless ( @$args == 0 or UNIVERSAL::isa((caller(1))[0], $method->{target_class}) ) { + croak "Method $method->{name} is write-protected"; + } + }, + ], + 'ppw' => [ + '+pre' => sub { + my $method = pop @_; + my $self = shift @_; + my $args = \@_; + unless ( @$args == 0 or (caller(1))[0] eq $method->{target_class} ) { + croak "Method $method->{name} is write-private"; + } + }, + ], + 'r' => [ + '+pre' => sub { + my $method = pop @_; + my $self = shift @_; + my $args = \@_; + @$args = (); + }, + ], + 'ro' => [ + '+pre' => sub { + my $method = pop @_; + my $self = shift @_; + my $args = \@_; + unless ( @$args == 0 ) { + croak("Method $method->{name} is read-only"); + } + }, + ], + 'wo' => [ + '+pre' => sub { + my $method = pop @_; + my $self = shift @_; + my $args = \@_; + if ( @$args == 0 ) { + croak("Method $method->{name} is write-only"); + } + }, + ], + 'return_original' => [ + '+pre' => sub { + my $method = pop @_; + my $self = shift @_; + my $args = \@_; + $method->{scratch}{return_original} = $self->{$method->{hash_key}}; + }, + '+post' => sub { + my $method = pop @_; + my $self = shift @_; + my $args = \@_; + ${ $method->{result} } = $method->{scratch}{return_original}; + }, + ], +); + +######################################################################## + +=head2 array - Instance Ref Accessor + +For each method name passed, uses a closure to generate a subroutine with the following characteristics: + +=over 4 + +=item * + +Must be called on a hash-based instance. + +=item * + +Has a specific hash key to use to access the related value for each instance. +This defaults to the method name, but you may override this with the C<'hash_key' => I> method parameter. + +=item * + +The value for each instance will be a reference to an array (or undef). + +=item * + +If called without any arguments, returns the current array-ref value (or undef). + + +=item * + +If called with a single non-ref argument, uses that argument as an index to retrieve from the referenced array, and returns that value (or undef). + +=item * + +If called with a single array ref argument, uses that list to return a slice of the referenced array. + +=item * + +If called with a list of argument pairs, each with a non-ref index and an associated value, stores the value at the given index in the referenced array. If the instance's value was previously undefined, a new array is autovivified. The current value in each position will be overwritten, and later arguments with the same index will override earlier ones. Returns the current array-ref value. + +=item * + +If called with a list of argument pairs, each with the first item being a reference to an array of up to two numbers, loops over each pair and uses those numbers to splice the value array. + +The first controlling number is the position at which the splice will begin. Zero will start before the first item in the list. Negative numbers count backwards from the end of the array. + +The second number is the number of items to be removed from the list. If it is omitted, or undefined, or zero, no items are removed. If it is a positive integer, that many items will be returned. + +If both numbers are omitted, or are both undefined, they default to containing the entire value array. + +If the second argument is undef, no values will be inserted; if it is a non-reference value, that one value will be inserted; if it is an array-ref, its values will be copied. + +The method returns the items that removed from the array, if any. + +=back + +Sample declaration and usage: + + package MyObject; + use Class::MakeMethods::Composite::Hash ( + array => 'bar', + ); + ... + + # Clear and set contents of list + print $obj->bar([ 'Spume', 'Frost' ] ); + + # Set values by position + $obj->bar(0 => 'Foozle', 1 => 'Bang!'); + + # Positions may be overwritten, and in any order + $obj->bar(2 => 'And Mash', 1 => 'Blah!'); + + # Retrieve value by position + print $obj->bar(1); + + # Direct access to referenced array + print scalar @{ $obj->bar() }; + +There are also calling conventions for slice and splice operations: + + # Retrieve slice of values by position + print join(', ', $obj->bar( undef, [0, 2] ) ); + + # Insert an item at position in the array + $obj->bar([3], 'Potatoes' ); + + # Remove 1 item from position 3 in the array + $obj->bar([3, 1], undef ); + + # Set a new value at position 2, and return the old value + print $obj->bar([2, 1], 'Froth' ); + +=cut + +use vars qw( %ArrayFragments ); + +sub array { + (shift)->_build_composite( \%ArrayFragments, @_ ); +} + +%ArrayFragments = ( + '' => [ + '+init' => sub { + my ($method) = @_; + $method->{hash_key} ||= $_->{name}; + }, + 'do' => sub { + my $method = pop @_; + my $self = shift @_; + my $args = \@_; + if ( scalar(@$args) == 0 ) { + if ( $method->{auto_init} and + ! defined $self->{$method->{hash_key}} ) { + $self->{$method->{hash_key}} = []; + } + wantarray ? @{ $self->{$method->{hash_key}} } : $self->{$method->{hash_key}}; + } elsif ( scalar(@_) == 1 and ref $_[0] eq 'ARRAY' ) { + $self->{$method->{hash_key}} = [ @{ $_[0] } ]; + wantarray ? @{ $self->{$method->{hash_key}} } : $self->{$method->{hash_key}}; + } else { + $self->{$method->{hash_key}} ||= []; + array_splicer( $self->{$method->{hash_key}}, @$args ); + } + }, + ], +); + +######################################################################## + +=head2 hash - Instance Ref Accessor + +For each method name passed, uses a closure to generate a subroutine with the following characteristics: + +=over 4 + +=item * + +Must be called on a hash-based instance. + +=item * + +Has a specific hash key to use to access the related value for each instance. +This defaults to the method name, but you may override this with the C<'hash_key' => I> method parameter. + +=item * + +The value for each instance will be a reference to a hash (or undef). + +=item * + +If called without any arguments, returns the contents of the hash in list context, or a hash reference in scalar context (or undef). + +=item * + +If called with one non-ref argument, uses that argument as an index to retrieve from the referenced hash, and returns that value (or undef). + +=item * + +If called with one array-ref argument, uses the contents of that array to retrieve a slice of the referenced hash. + +=item * + +If called with one hash-ref argument, sets the contents of the referenced hash to match that provided. + +=item * + +If called with a list of key-value pairs, stores the value under the given key in the referenced hash. If the instance's value was previously undefined, a new hash is autovivified. The current value under each key will be overwritten, and later arguments with the same key will override earlier ones. Returns the contents of the hash in list context, or a hash reference in scalar context. + +=back + +Sample declaration and usage: + + package MyObject; + use Class::MakeMethods::Composite::Hash ( + hash => 'baz', + ); + ... + + # Set values by key + $obj->baz('foo' => 'Foozle', 'bar' => 'Bang!'); + + # Values may be overwritten, and in any order + $obj->baz('broccoli' => 'Blah!', 'foo' => 'Fiddle'); + + # Retrieve value by key + print $obj->baz('foo'); + + # Retrive slice of values by position + print join(', ', $obj->baz( ['foo', 'bar'] ) ); + + # Direct access to referenced hash + print keys %{ $obj->baz() }; + + # Reset the hash contents to empty + @{ $obj->baz() } = (); + +=cut + +use vars qw( %HashFragments ); + +sub hash { + (shift)->_build_composite( \%HashFragments, @_ ); +} + +%HashFragments = ( + '' => [ + '+init' => sub { + my ($method) = @_; + $method->{hash_key} ||= $_->{name}; + }, + 'do' => sub { + my $method = pop @_; + my $self = shift @_; + my $args = \@_; + if ( scalar(@$args) == 0 ) { + if ( $method->{auto_init} and ! defined $self->{$method->{hash_key}} ) { + $self->{$method->{hash_key}} = {}; + } + wantarray ? %{ $self->{$method->{hash_key}} } : $self->{$method->{hash_key}}; + } elsif ( scalar(@$args) == 1 ) { + if ( ref($_[0]) eq 'HASH' ) { + %{$self->{$method->{hash_key}}} = %{$_[0]}; + } elsif ( ref($_[0]) eq 'ARRAY' ) { + return @{$self->{$method->{hash_key}}}{ @{$_[0]} } + } else { + return $self->{$method->{hash_key}}->{ $_[0] } + } + } elsif ( scalar(@$args) % 2 ) { + croak "Odd number of items in assigment to $method->{name}"; + } else { + while ( scalar(@$args) ) { + my $key = shift @$args; + $self->{$method->{hash_key}}->{ $key} = shift @$args; + } + wantarray ? %{ $self->{$method->{hash_key}} } : $self->{$method->{hash_key}}; + } + }, + ], +); + +######################################################################## + +=head2 object - Instance Ref Accessor + +For each method name passed, uses a closure to generate a subroutine with the following characteristics: + +=over 4 + +=item * + +Must be called on a hash-based instance. + +=item * + +Has a specific hash key to use to access the related value for each instance. +This defaults to the method name, but you may override this with the C<'hash_key' => I> method parameter. + +=item * + +The value for each instance will be a reference to an object (or undef). + +=item * + +If called without any arguments returns the current value. + +=item * + +If called with an argument, stores that as the value, and returns it, + +=back + +Sample declaration and usage: + + package MyObject; + use Class::MakeMethods::Composite::Hash ( + object => 'foo', + ); + ... + + # Store value + $obj->foo( Foozle->new() ); + + # Retrieve value + print $obj->foo; + +=cut + +use vars qw( %ObjectFragments ); + +sub object { + (shift)->_build_composite( \%ObjectFragments, @_ ); +} + +%ObjectFragments = ( + '' => [ + '+init' => sub { + my ($method) = @_; + $method->{hash_key} ||= $_->{name}; + }, + 'do' => sub { + my $method = pop @_; + my $self = shift; + if ( scalar @_ ) { + my $value = shift; + if ( $method->{class} and ! UNIVERSAL::isa( $value, $method->{class} ) ) { + croak "Wrong argument type ('$value') in assigment to $method->{name}"; + } + $self->{$method->{hash_key}} = $value; + } else { + if ( $method->{auto_init} and ! defined $self->{$method->{hash_key}} ) { + my $class = $method->{class} + or die "Can't auto_init without a class"; + my $new_method = $method->{new_method} || 'new'; + $self->{$method->{hash_key}} = $class->$new_method(); + } + $self->{$method->{hash_key}}; + } + }, + ], +); + +######################################################################## + +=head1 SEE ALSO + +See L for general information about this distribution. + +See L for more about this family of subclasses. + +=cut + +1; diff --git a/lib/Class/MakeMethods/Composite/Inheritable.pm b/lib/Class/MakeMethods/Composite/Inheritable.pm new file mode 100644 index 0000000..ca4be3e --- /dev/null +++ b/lib/Class/MakeMethods/Composite/Inheritable.pm @@ -0,0 +1,613 @@ +=head1 NAME + +Class::MakeMethods::Composite::Inheritable - Overridable data + +=head1 SYNOPSIS + + package MyClass; + + use Class::MakeMethods( 'Composite::Inheritable:scalar' => 'foo' ); + # We now have an accessor method for an "inheritable" scalar value + + MyClass->foo( 'Foozle' ); # Set a class-wide value + print MyClass->foo(); # Retrieve class-wide value + + my $obj = MyClass->new(...); + print $obj->foo(); # All instances "inherit" that value... + + $obj->foo( 'Foible' ); # until you set a value for an instance. + print $obj->foo(); # This now finds object-specific value. + ... + + package MySubClass; + @ISA = 'MyClass'; + + print MySubClass->foo(); # Intially same as superclass, + MySubClass->foo('Foobar'); # but overridable per subclass, + print $subclass_obj->foo(); # and shared by its instances + $subclass_obj->foo('Fosil');# until you override them... + ... + + # Similar behaviour for hashes and arrays is currently incomplete + package MyClass; + use Class::MakeMethods::Composite::Inheritable ( + array => 'my_list', + hash => 'my_index', + ); + + MyClass->my_list(0 => 'Foozle', 1 => 'Bang!'); + print MyClass->my_list(1); + + MyClass->my_index('broccoli' => 'Blah!', 'foo' => 'Fiddle'); + print MyClass->my_index('foo'); + + +=head1 DESCRIPTION + +The MakeMethods subclass provides accessor methods that search an inheritance tree to find a value. This allows you to set a shared or default value for a given class, optionally override it in a subclass, and then optionally override it on a per-instance basis. + +Note that all MakeMethods methods are inheritable, in the sense that they work as expected for subclasses. These methods are different in that the I accessed by each method can be inherited or overridden in each subclass or instance. See L< Class::MakeMethods::Utility::Inheritable> for more about this type of "inheritable" or overridable" data. + + +=head2 Class::MakeMethods Calling Interface + +When you C this package, the method declarations you provide +as arguments cause subroutines to be generated and installed in +your module. + +See L for more information. + +=head2 Class::MakeMethods::Standard Declaration Syntax + +To declare methods, pass in pairs of a method-type name followed +by one or more method names. + +See the "METHOD GENERATOR TYPES" section below for a list of the supported values of I. + +See L and L for more information. + +=cut + +package Class::MakeMethods::Composite::Inheritable; + +$VERSION = 1.000; +use strict; +use Carp; + +use Class::MakeMethods::Composite '-isasubclass'; +use Class::MakeMethods::Utility::Inheritable qw(get_vvalue set_vvalue find_vself ); + +######################################################################## + +=head1 METHOD GENERATOR TYPES + +=head2 scalar - Overrideable Accessor + +For each method name passed, uses a closure to generate a subroutine with the following characteristics: + +=over 4 + +=item * + +May be called as a class or instance method, on the declaring class or any subclass. + +=item * + +If called without any arguments returns the current value for the callee. If the callee has not had a value defined for this method, searches up from instance to class, and from class to superclass, until a callee with a value is located. + +=item * + +If called with an argument, stores that as the value associated with the callee, whether instance or class, and returns it, + +=item * + +If called with multiple arguments, stores a reference to a new array with those arguments as contents, and returns that array reference. + +=back + +Sample declaration and usage: + + package MyClass; + use Class::MakeMethods::Composite::Inheritable ( + scalar => 'foo', + ); + ... + + # Store value + MyClass->foo('Foozle'); + + # Retrieve value + print MyClass->foo; + +=cut + +use vars qw( %ScalarFragments ); + +sub scalar { + (shift)->_build_composite( \%ScalarFragments, @_ ); +} + +%ScalarFragments = ( + '' => [ + '+init' => sub { + my ($method) = @_; + $method->{target_class} ||= $Class::MethodMaker::CONTEXT{TargetClass}; + $method->{data} ||= {}; + }, + 'do' => sub { + my $method = pop @_; + my $self = shift @_; + if ( scalar(@_) == 0 ) { + return get_vvalue($method->{data}, $self); + } else { + my $value = (@_ == 1 ? $_[0] : [@_]); + set_vvalue($method->{data}, $self, $value); + } + }, + ], + 'rw' => [], + 'p' => [ + '+pre' => sub { + my $method = pop @_; + my $self = shift @_; + unless ( UNIVERSAL::isa((caller(1))[0], $method->{target_class}) ) { + croak "Method $method->{name} is protected"; + } + }, + ], + 'pp' => [ + '+pre' => sub { + my $method = pop @_; + my $self = shift @_; + unless ( (caller(1))[0] eq $method->{target_class} ) { + croak "Method $method->{name} is private"; + } + }, + ], + 'pw' => [ + '+pre' => sub { + my $method = pop @_; + my $self = shift @_; + unless ( @_ == 0 or UNIVERSAL::isa((caller(1))[0], $method->{target_class}) ) { + croak "Method $method->{name} is write-protected"; + } + }, + ], + 'ppw' => [ + '+pre' => sub { + my $method = pop @_; + my $self = shift @_; + unless ( @_ == 0 or (caller(1))[0] eq $method->{target_class} ) { + croak "Method $method->{name} is write-private"; + } + }, + ], + 'r' => [ + '+pre' => sub { + my $method = pop @_; + my $self = shift @_; + @{ $method->{args} } = (); + }, + ], + 'ro' => [ + '+pre' => sub { + my $method = pop @_; + my $self = shift @_; + unless ( @_ == 0 ) { + croak("Method $method->{name} is read-only"); + } + }, + ], + 'wo' => [ + '+pre' => sub { + my $method = pop @_; + my $self = shift @_; + if ( @_ == 0 ) { + croak("Method $method->{name} is write-only"); + } + }, + ], + 'return_original' => [ + '+pre' => sub { + my $method = pop @_; + my $self = shift @_; + my $v_self = find_vself($method->{data}, $self); + $method->{scratch}{return_original} = + $v_self ? $method->{data}{$v_self} : (); + }, + '+post' => sub { + my $method = pop @_; + $method->{result} = \{ $method->{scratch}{return_original} }; + }, + ], +); + +######################################################################## + +=head2 array - Overrideable Ref Accessor + +For each method name passed, uses a closure to generate a subroutine with the following characteristics: + +=over 4 + +=item * + +May be called as a class method, or on any instance or subclass, Must be called on a hash-based instance. + +=item * + +The class value will be a reference to an array (or undef). + +=item * + +If called without any arguments, returns the current array-ref value (or undef). + + +=item * + +If called with a single non-ref argument, uses that argument as an index to retrieve from the referenced array, and returns that value (or undef). + +=item * + +If called with a single array ref argument, uses that list to return a slice of the referenced array. + +=item * + +If called with a list of argument pairs, each with a non-ref index and an associated value, stores the value at the given index in the referenced array. If the class value was previously undefined, a new array is autovivified. The current value in each position will be overwritten, and later arguments with the same index will override earlier ones. Returns the current array-ref value. + +=item * + +If called with a list of argument pairs, each with the first item being a reference to an array of up to two numbers, loops over each pair and uses those numbers to splice the value array. + +The first controlling number is the position at which the splice will begin. Zero will start before the first item in the list. Negative numbers count backwards from the end of the array. + +The second number is the number of items to be removed from the list. If it is omitted, or undefined, or zero, no items are removed. If it is a positive integer, that many items will be returned. + +If both numbers are omitted, or are both undefined, they default to containing the entire value array. + +If the second argument is undef, no values will be inserted; if it is a non-reference value, that one value will be inserted; if it is an array-ref, its values will be copied. + +The method returns the items that removed from the array, if any. + +=back + +Sample declaration and usage: + + package MyClass; + use Class::MakeMethods::Composite::Inheritable ( + array => 'bar', + ); + ... + + # Clear and set contents of list + print MyClass->bar([ 'Spume', 'Frost' ] ); + + # Set values by position + MyClass->bar(0 => 'Foozle', 1 => 'Bang!'); + + # Positions may be overwritten, and in any order + MyClass->bar(2 => 'And Mash', 1 => 'Blah!'); + + # Retrieve value by position + print MyClass->bar(1); + + # Direct access to referenced array + print scalar @{ MyClass->bar() }; + +There are also calling conventions for slice and splice operations: + + # Retrieve slice of values by position + print join(', ', MyClass->bar( undef, [0, 2] ) ); + + # Insert an item at position in the array + MyClass->bar([3], 'Potatoes' ); + + # Remove 1 item from position 3 in the array + MyClass->bar([3, 1], undef ); + + # Set a new value at position 2, and return the old value + print MyClass->bar([2, 1], 'Froth' ); + +B + +=cut + +use vars qw( %ArrayFragments ); + +sub array { + (shift)->_build_composite( \%ArrayFragments, @_ ); +} + +%ArrayFragments = ( + '' => [ + '+init' => sub { + my ($method) = @_; + $method->{hash_key} ||= $_->{name}; + $method->{data} ||= {}; + }, + 'do' => sub { + my $method = pop @_; + my $self = shift @_; + + if ( scalar(@_) == 0 ) { + my $v_self = find_vself($method->{data}, $self); + my $value = $v_self ? $method->{data}{$v_self} : (); + if ( $method->{auto_init} and ! $value ) { + $value = $method->{data}{$self} = []; + } + ( ! $value ) ? () : wantarray ? @$value : $value; + + } elsif ( scalar(@_) == 1 and ref $_[0] eq 'ARRAY' ) { + $method->{data}{$self} = [ @{ $_[0] } ]; + wantarray ? @{ $method->{data}{$self} } : $method->{data}{$self} + + } else { + if ( ! exists $method->{data}{$self} ) { + my $v_self = find_vself($method->{data}, $self); + $method->{data}{$self} = [ $v_self ? @{$method->{data}{$v_self}} : () ]; + } + return array_splicer( $method->{data}{$self}, @_ ); + } + }, + ], +); + +######################################################################## + +=head2 hash - Overrideable Ref Accessor + +For each method name passed, uses a closure to generate a subroutine with the following characteristics: + +=over 4 + +=item * + +May be called as a class method, or on any instance or subclass, Must be called on a hash-based instance. + +=item * + +The class value will be a reference to a hash (or undef). + +=item * + +If called without any arguments returns the contents of the hash in list context, or a hash reference in scalar context for the callee. If the callee has not had a value defined for this method, searches up from instance to class, and from class to superclass, until a callee with a value is located. + +=item * + +If called with one non-ref argument, uses that argument as an index to retrieve from the referenced hash, and returns that value (or undef). If the callee has not had a value defined for this method, searches up from instance to class, and from class to superclass, until a callee with a value is located. + +=item * + +If called with one array-ref argument, uses the contents of that array to retrieve a slice of the referenced hash. If the callee has not had a value defined for this method, searches up from instance to class, and from class to superclass, until a callee with a value is located. + +=item * + +If called with one hash-ref argument, sets the contents of the referenced hash to match that provided. + +=item * + +If called with a list of key-value pairs, stores the value under the given key in the hash associated with the callee, whether instance or class. If the callee did not previously have a hash-ref value associated with it, searches up instance to class, and from class to superclass, until a callee with a value is located, and copies that hash before making the assignments. The current value under each key will be overwritten, and later arguments with the same key will override earlier ones. Returns the contents of the hash in list context, or a hash reference in scalar context. + +=back + +Sample declaration and usage: + + package MyClass; + use Class::MakeMethods::Composite::Inheritable ( + hash => 'baz', + ); + ... + + # Set values by key + MyClass->baz('foo' => 'Foozle', 'bar' => 'Bang!'); + + # Values may be overwritten, and in any order + MyClass->baz('broccoli' => 'Blah!', 'foo' => 'Fiddle'); + + # Retrieve value by key + print MyClass->baz('foo'); + + # Retrive slice of values by position + print join(', ', MyClass->baz( ['foo', 'bar'] ) ); + + # Direct access to referenced hash + print keys %{ MyClass->baz() }; + + # Reset the hash contents to empty + @{ MyClass->baz() } = (); + +B + +=cut + +use vars qw( %HashFragments ); + +sub hash { + (shift)->_build_composite( \%HashFragments, @_ ); +} + +%HashFragments = ( + '' => [ + '+init' => sub { + my ($method) = @_; + $method->{hash_key} ||= $_->{name}; + $method->{data} ||= {}; + }, + 'do' => sub { + my $method = pop @_; + my $self = shift @_; + + if ( scalar(@_) == 0 ) { + my $value = get_vvalue($method->{data}, $self); + if ( $method->{auto_init} and ! $value ) { + $value = set_vvalue( $method->{data}, $self, {} ); + } + wantarray ? %$value : $value; + } elsif ( scalar(@_) == 1 ) { + if ( ref($_[0]) eq 'HASH' ) { + %{$method->{data}{$self}} = %{$_[0]}; + } elsif ( ref($_[0]) eq 'ARRAY' ) { + my $v_self = find_vself($method->{data}, $self) or return; + return @{ $method->{data}{$v_self} }{ @{$_[0]} } + } else { + my $v_self = find_vself($method->{data}, $self) or return; + return $method->{data}{$v_self}{ $_[0] } + } + + } elsif ( scalar(@_) % 2 ) { + Carp::croak "Odd number of items in assigment to $method->{name}"; + } else { + if ( ! exists $method->{data}{$self} ) { + my $v_self = find_vself($method->{data}, $self); + $method->{data}{$self} = { $v_self ? %{ $method->{data}{$v_self} } : () }; + } + while ( scalar(@_) ) { + my $key = shift(); + $method->{data}{$self}->{ $key } = shift(); + } + wantarray ? %{$method->{data}{$self}} : $method->{data}{$self}; + } + }, + ], +); + +######################################################################## + +=head2 hook - Overrideable array of subroutines + +A hook method is called from the outside as a normal method. However, internally, it contains an array of subroutine references, each of which are called in turn to produce the method's results. + +Subroutines may be added to the hook's array by calling it with a blessed subroutine reference, as shown below. Subroutines may be added on a class-wide basis or on an individual object. + +You might want to use this type of method to provide an easy way for callbacks to be registered. + + package MyClass; + use Class::MakeMethods::Composite::Inheritable ( 'hook' => 'init' ); + + MyClass->init( Class::MakeMethods::Composite::Inheritable->Hook( sub { + my $callee = shift; + warn "Init..."; + } ); + + my $obj = MyClass->new; + $obj->init(); + +=cut + +use vars qw( %HookFragments ); + +sub hook { + (shift)->_build_composite( \%HookFragments, @_ ); +} + +%HookFragments = ( + '' => [ + '+init' => sub { + my ($method) = @_; + $method->{data} ||= {}; + }, + 'do' => sub { + my $method = pop @_; + my $self = shift @_; + + if ( scalar(@_) and + ref($_[0]) eq 'Class::MakeMethods::Composite::Inheritable::Hook' ) { + if ( ! exists $method->{data}{$self} ) { + my $v_self = find_vself($method->{data}, $self); + $method->{data}{$self} = [ $v_self ? @{ $method->{data}{$v_self} } : () ]; + } + push @{ $method->{data}{$self} }, map $$_, @_; + } else { + my $v_self = find_vself($method->{data}, $self); + my $subs = $v_self ? $method->{data}{$v_self} : (); + my @subs = ( ( ! $subs ) ? () : @$subs ); + + if ( ! defined $method->{wantarray} ) { + foreach my $sub ( @subs ) { + &$sub( @{$method->{args}} ); + } + } elsif ( ! $method->{wantarray} ) { + foreach my $sub ( @subs ) { + my $value = &$sub( @{$method->{args}} ); + if ( defined $value ) { + $method->{result} = \$value; + } + } + } else { + foreach my $sub ( @subs ) { + my @value = &$sub( @{$method->{args}} ); + if ( scalar @value ) { + push @{ $method->{result} }, @value; + } + } + } + + } + return Class::MakeMethods::Composite->CurrentResults(); + }, + ], +); + +sub Hook (&) { + my $package = shift; + my $sub = shift; + bless \$sub, 'Class::MakeMethods::Composite::Inheritable::Hook'; +} + +######################################################################## + +=head2 object - Overrideable Ref Accessor + +For each method name passed, uses a closure to generate a subroutine with the following characteristics: + +=over 4 + +=item * + +May be called as a class method, or on any instance or subclass, Must be called on a hash-based instance. + +=item * + +The class value will be a reference to an object (or undef). + +=item * + +If called without any arguments returns the current value for the callee. If the callee has not had a value defined for this method, searches up from instance to class, and from class to superclass, until a callee with a value is located. + +=item * + +If called with an argument, stores that as the value associated with the callee, whether instance or class, and returns it, + +=back + +Sample declaration and usage: + + package MyClass; + use Class::MakeMethods::Composite::Inheritable ( + object => 'foo', + ); + ... + + # Store value + MyClass->foo( Foozle->new() ); + + # Retrieve value + print MyClass->foo; + +B + +=cut + +sub object { } + +######################################################################## + +=head1 SEE ALSO + +See L for general information about this distribution. + +See L for more about this family of subclasses. + +=cut + +1; diff --git a/lib/Class/MakeMethods/Composite/Universal.pm b/lib/Class/MakeMethods/Composite/Universal.pm new file mode 100644 index 0000000..e53e76d --- /dev/null +++ b/lib/Class/MakeMethods/Composite/Universal.pm @@ -0,0 +1,150 @@ +=head1 NAME + +Class::MakeMethods::Composite::Universal - Composite Method Tricks + +=head1 SYNOPSIS + + Class::MakeMethods::Composite::Universal->make_patch( + -TargetClass => 'SomeClass::OverYonder', + name => 'foo', + pre_rules => [ + sub { + my $method = pop; + warn "Arguments for foo:", @_ + } + ] + post_rules => [ + sub { + warn "Result of foo:", Class::MakeMethods::Composite->CurrentResults + } + ] + ); + +=head1 DESCRIPTION + +The Composite::Universal suclass of MakeMethods provides some generally-applicable types of methods based on Class::MakeMethods::Composite. + +=cut + +package Class::MakeMethods::Composite::Universal; + +$VERSION = 1.000; +use strict; +use Class::MakeMethods::Composite '-isasubclass'; +use Carp; + +######################################################################## + +=head1 METHOD GENERATOR TYPES + +=head2 patch + +The patch ruleset generates composites whose core behavior is based on an existing subroutine. + +Here's a sample usage: + + sub foo { + my $count = shift; + return 'foo' x $count; + } + + Class::MakeMethods::Composite::Universal->make( + -ForceInstall => 1, + patch => { + name => 'foo', + pre_rules => [ + sub { + my $method = pop @_; + if ( ! scalar @_ ) { + @{ $method->{args} } = ( 2 ); + } + }, + sub { + my $method = pop @_; + my $count = shift; + if ( $count > 99 ) { + Carp::confess "Won't foo '$count' -- that's too many!" + } + }, + ], + post_rules => [ + sub { + my $method = pop @_; + if ( ref $method->{result} eq 'SCALAR' ) { + ${ $method->{result} } =~ s/oof/oozle-f/g; + } elsif ( ref $method->{result} eq 'ARRAY' ) { + map { s/oof/oozle-f/g } @{ $method->{result} }; + } + } + ], + }, + ); + +=cut + +use vars qw( %PatchFragments ); + +sub patch { + (shift)->_build_composite( \%PatchFragments, @_ ); +} + +%PatchFragments = ( + '' => [ + '+init' => sub { + my $method = pop @_; + my $origin = ( $Class::MethodMaker::CONTEXT{TargetClass} || '' ) . + '::' . $method->{name}; + no strict 'refs'; + $method->{patch_original} = *{ $origin }{CODE} + or croak "No subroutine $origin() to patch"; + }, + 'do' => sub { + my $method = pop @_; + my $sub = $method->{patch_original}; + &$sub( @_ ); + }, + ], +); + +=head2 make_patch + +A convenient wrapper for C and the C method generator. + +Provides the '-ForceInstall' flag, which is required to ensure that the patched subroutine replaces the original. + +For example, one could add logging to an existing method as follows: + + Class::MakeMethods::Composite::Universal->make_patch( + -TargetClass => 'SomeClass::OverYonder', + name => 'foo', + pre_rules => [ + sub { + my $method = pop; + warn "Arguments for foo:", @_ + } + ] + post_rules => [ + sub { + warn "Result of foo:", Class::MakeMethods::Composite->CurrentResults + } + ] + ); + +=cut + +sub make_patch { + (shift)->make( -ForceInstall => 1, patch => { @_ } ); +} + + +######################################################################## + +=head1 SEE ALSO + +See L for general information about this distribution. + +See L for more about this family of subclasses. + +=cut + +1; diff --git a/lib/Class/MakeMethods/Docs/Catalog.pod b/lib/Class/MakeMethods/Docs/Catalog.pod new file mode 100644 index 0000000..ba17349 --- /dev/null +++ b/lib/Class/MakeMethods/Docs/Catalog.pod @@ -0,0 +1,888 @@ +=head1 NAME + +Class::MakeMethods::Docs::Catalog - List of Makable Method Types + + +=head1 DESCRIPTION + +This document lists the various subclasses of Class::MakeMethods included +in this distribution, and the method types each one provides. + +See the documentation for each implementation for more details +about the features it provides. + +For each class, a parenthetical comment indicates whether the methods it generates are applicable to individual blessed objects (Instances), to class data (Global), or both (Any) +=head2 Scoping + +The final part of the name of a method-generating subclass typically indicates the scope or applicability of the methods it generates + +=over 4 + +=item Hash + +For object instances based on blessed hashes with named values. + +=item Array + +For object instances based on blessed arrays with positional values. + +=item Scalar + +For object instances based on blessed scalars with a single value. + +=item InsideOut + +For any object instance regardless of underlying data type. + +=item Ref + +For any object instance regardless of underlying data type. + +=item Inheritable + +For data which can be set at the class, subclass, or instance level. + +=item Class + +For class data shared by all instances but different for each subclass + +=item ClassVar + +For class data shared by all instances but different for each subclass + +=item ClassInherit + +For class data shared by all instances but different for each subclass + +=item Global + +For global data shared by a class and all its instances and subclasses + +=item PackageVar + +For global data shared by a class and all its instances and subclasses + +=item Universal + +# General method types that are widely applicable + +=back + +=head2 Summary Charts + +This table shows which scopes are available in each generator family: + + SCOPING Basic Standard Evaled Composite Template + Hash + + + + + + Array + + + + + Scalar + + InsideOut + + Ref + + Inheritable + + + + Class + + ClassVar + + ClassInherit + + Global + + + + + PackageVar + + Universal + + + +This table shows which types of methods are typically available in each generator family: + + METHOD Basic Standard Evaled Composite Template + new + + + + + + scalar + + + + + string + + string_index + + number + + boolean + + boolean_index + + bits + + + array + + + + + struct + + + hash + + + + + hash_of_arrays + + tiedhash + + + object + + + + instance + + array_of_objects + + + code + + code_or_scalar + + + +=head1 BASIC CLASSES + +=head2 Basic::Hash (Instances) + +Methods for objects based on blessed hashes. See L for details. + +=over 4 + +=item * + +new: create and copy instances + +=item * + +scalar: get and set scalar values in each instance + +=item * + +array: get and set values stored in an array refered to in each +instance + +=item * + +hash: get and set values in a hash refered to in each instance + +=back + +=head2 Basic::Array (Instances) + +Methods for manipulating positional values in arrays. See L for details. + +=over 4 + +=item * + +new: create and copy instances + +=item * + +scalar: get and set scalar values in each instance + +=item * + +array: get and set values stored in an array refered to in each +instance + +=item * + +hash: get and set values in a hash refered to in each instance + +=back + +=head2 Basic::Global (Global) + +Global methods are not instance-dependent; calling them by class +name or from any instance or subclass will consistently access the +same value. See L for details. + +=over 4 + +=item * + +scalar: get and set a global scalar value + +=item * + +array: get and set values in a global array + +=item * + +hash: get and set values in a global hash + +=back + + +=head1 STANDARD CLASSES + +=head2 Standard::Hash (Instances) + +Methods for objects based on blessed hashes. See L for details. + +=over 4 + +=item * + +new: create and copy instances + +=item * + +scalar: get and set scalar values in each instance + +=item * + +array: get and set values stored in an array refered to in each +instance + +=item * + +hash: get and set values in a hash refered to in each instance + +=item * + +object: access an object refered to by each instance + +=back + +=head2 Standard::Array (Instances) + +Methods for manipulating positional values in arrays. See L for details. + +=over 4 + +=item * + +new: create and copy instances + +=item * + +scalar: get and set scalar values in each instance + +=item * + +array: get and set values stored in an array refered to in each +instance + +=item * + +hash: get and set values in a hash refered to in each instance + +=item * + +object: access an object refered to by each instance + +=back + +=head2 Standard::Global (Global) + +Methods for manipulating global data. See L for details. + +=over 4 + +=item * + +scalar: get and set global scalar + +=item * + +array: get and set values stored in a global array + +=item * + +hash: get and set values in a global hash + +=item * + +object: global access to an object ref + +=back + + +=head2 Standard::Inheritable (Any) + +Methods for manipulating data which may be overridden per class or instance. Uses external data storage, so it works with objects of any underlying data type. See L for details. + +=over 4 + +=item * + +scalar: get and set scalar values for each instance or class + +=back + + +=head1 COMPOSITE CLASSES + +=head2 Composite::Hash (Instances) + +Methods for objects based on blessed hashes. See L for details. + +=over 4 + +=item * + +new: create and copy instances + +=item * + +scalar: get and set scalar values in each instance + +=item * + +array: get and set values stored in an array refered to in each +instance + +=item * + +hash: get and set values in a hash refered to in each instance + +=item * + +object: access an object refered to by each instance + +=back + +=head2 Composite::Array (Instances) + +Methods for manipulating positional values in arrays. See L for details. + +=over 4 + +=item * + +new: create and copy instances + +=item * + +scalar: get and set scalar values in each instance + +=item * + +array: get and set values stored in an array refered to in each +instance + +=item * + +hash: get and set values in a hash refered to in each instance + +=item * + +object: access an object refered to by each instance + +=back + +=head2 Composite::Global (Global) + +Methods for manipulating global data. See L for details. + +=over 4 + +=item * + +scalar: get and set global scalar + +=item * + +array: get and set values stored in a global array + +=item * + +hash: get and set values in a global hash + +=item * + +object: global access to an object ref + +=back + + +=head2 Composite::Inheritable (Any) + +Methods for manipulating data which may be overridden per class or instance. Uses external data storage, so it works with objects of any underlying data type. See L for details. + +=over 4 + +=item * + +scalar: get and set scalar values for each instance or class + +=item * + +hook: create a subroutine intended to have operations added to it + +=back + + +=head2 Composite::Universal (Any) + +Methods for padding pre- and post-conditions to any class. See L for details. + +=over 4 + +=item * + +patch: add pre and post operations to an existing subroutine + +=back + + +=head1 TEMPLATE CLASSES + +=head2 Template::Universal (Any) + +Meta-methods for any type of object. See L. + +=over 4 + +=item * + +no_op - a method with an empty body + +=item * + +croak - a method which will croak if called + +=item * + +method_init - calls other methods from a list of method name => argument pairs + +=item * + +forward_methods - delegates to an object provided by another method + +=back + +=head2 Template::Ref (Any Instance) + +Methods for deep copies and comparisons. See L. + +=over 4 + +=item * + +clone: make a deep copy of an object instance + +=item * + +prototype: make new objects by cloning a typical instance + +=item * + +compare: compare one object to another + +=back + + +=head2 Template::Generic (Abstract) + +The remaining subclasses inherit a similar collection of templates from Template::Generic, and provide a different type of scoping or binding for the functionality defined by the Generic template. See L for details. + + +=head2 Template::Hash (Instances) + +The most commonly used implementation, for objects based on blessed hashes. See L. + +=over 4 + +=item * + +new: create and copy instances + +=item * + +scalar: get and set scalar values in each instance + +=item * + +string: get and set string values in each instance + +=item * + +number: get and set numeric values in each instance + +=item * + +boolean: get and set boolean values in each instance + +=item * + +bits: get and set boolean values stored in a single value in each +instance + +=item * + +array: get and set values stored in an array refered to in each +instance + +=item * + +struct - methods for acccessing values which are stored by +position in an array + +=item * + +hash: get and set values in a hash refered to in each instance + +=item * + +tiedhash: get and set values in a tied hash refered to in each +instance + +=item * + +hash_of_arrays: for references to hashes of arrays contained in each +instance + +=item * + +object: set or access a reference to an object contained in each +instance + +=item * + +array_of_objects: manipulate an array of object references within in +each instance + +=item * + +code: set or call a function reference contained in each instance + +=back + + +=head2 Template::Array (Instances) + +Methods for manipulating positional values in arrays. See L. + +=over 4 + +=item * + +new: create and copy array instances + +=item * + +scalar: get and set scalar values in a given array position + +=item * + +string: get and set string values in a given array position + +=item * + +number: get and set numeric values in a given array position + +=item * + +boolean: get and set boolean values in a given array position + +=item * + +builtin_isa: generates a wrapper around some builtin function, +cacheing the results in the object and providing a by-name interface + +=back + + + +=head2 Template::Scalar (Instances) + +For objects based on blessed scalars. See L. + +Note that these objects can generally only have one value accessor method, as all such accessors will refer to the same value. + +=over 4 + +=item * + +new: create and copy instances + +=item * + +scalar: get and set scalar values in each instance + +=item * + +string: get and set a string value in each instance + +=item * + +number: get and set a numeric value in each instance + +=item * + +boolean: get and set a boolean value in each instance + +=item * + +bits: get and set boolean values stored in a single value in each +instance + +=item * + +code: set or call a function reference contained in each instance + +=back + + +=head2 Template::InsideOut (Instances) + +Stores values for objects in an external location hashed by identity. See L. + +Note that while the below constructor creates and returns scalar +references, accessor methods can be created with this implementation +for use with any type of object. + +=over 4 + +=item * + +new: create and copy instances + +=item * + +scalar: get and set scalar values associated with each instance + +=item * + +string: get and set string values associated with each instance + +=item * + +string_index: get and set string values associated with each +instance, and maintain an index of instances by value + +=item * + +number: get and set numeric values associated with each instance + +=item * + +boolean: get and set boolean values associated with each instance + +=item * + +boolean_index: get and set boolean values associated with each instance, and maintain a list of items which have the flag set + +=item * + +bits: get and set boolean values stored in a single value associated with each +instance + +=item * + +array: get and set values stored in an array associated with each +instance + +=item * + +hash: get and set values in a hash associated with each instance + +=item * + +code: set or call a function reference associated with each instance + +=back + + +=head2 Template::Global (Global) + +Global methods are not instance-dependent; calling them by class +name or from any instance will consistently access the same value. See L. + +=over 4 + +=item * + +scalar: get and set a global scalar value + +=item * + +string: get and set a global string value + +=item * + +number: get and set a global number value + +=item * + +boolean: get and set a global boolean value + +=item * + +array: get and set values in a global array + +=item * + +hash: get and set values in a global hash + +=item * + +tiedhash: get and set values in a global tied hash + +=item * + +hash_of_arrays: get and set values in a global hash of arrays + +=item * + +object: set and access a global reference to an object + +=item * + +instance: set and access a global reference to an object of the declaring class + +=item * + +code: set and access a global reference to a subroutine. + +=back + + +=head2 Template::PackageVar (Global) + +PackageVar methods access a variable in the declaring package. Thus, +they have the same effect as Static methods, while keeping their +value accessible via the symbol table. See L. + +=over 4 + +=item * + +scalar: get and set a global scalar value + +=item * + +string: get and set a global string value + +=item * + +number: get and set a global number value + +=item * + +boolean: get and set a global boolean value + +=item * + +array: get and set values in a global array + +=item * + +hash: get and set values in a global hash + +=back + + +=head2 Template::Class (Global) + +Class methods are similar to Static methods, except that each subclass and its instances will access a distinct value. See L. + +=over 4 + +=item * + +scalar: get and set a class-specific scalar value + +=item * + +string: get and set a class-specific string value + +=item * + +number: get and set a class-specific number value + +=item * + +boolean: get and set a class-specific boolean value + +=item * + +array: get and set values in a class-specific array + +=item * + +hash: get and set values in a class-specific hash + +=back + + +=head2 Template::ClassVar (Global) + +ClassVar methods access a variable in the package on which they +are called. Thus, they have the same effect as Class methods, +while keeping their value accessible via the symbol table, like +PackageVar. See L. + +=over 4 + +=item * + +scalar: get and set a class-specific scalar value + +=item * + +string: get and set a class-specific string value + +=item * + +number: get and set a class-specific number value + +=item * + +boolean: get and set a class-specific boolean value + +=item * + +array: get and set values in a class-specific array + +=item * + +hash: get and set values in a class-specific hash + +=back + + +=head2 Template::ClassInherit (Global) + +ClassInherit methods are an intermediate point between Static and Class methods; subclasses inherit their superclass's value until they set their own value, after which they become distinct. See L. + +=over 4 + +=item * + +scalar: get and set an inheritable class-specific scalar value + +=item * + +string: get and set an inheritable class-specific string value + +=item * + +number: get and set an inheritable class-specific number value + +=item * + +boolean: get and set an inheritable class-specific boolean value + +=item * + +array: get and set values in an inheritable class-specific array + +=item * + +hash: get and set values in an inheritable class-specific hash + +=back + + +=head2 Template::Inheritable (Any) + +Methods for manipulating data which may be overridden per class or instance. Uses external data storage, so it works with objects of any underlying data type. See L for details. + +=over 4 + +=item * + +scalar: get and set scalar values for each instance or class + +=item * + +string: get and set string values for each instance or class + +=item * + +number: get and set numeric values for each instance or class + +=item * + +boolean: get and set boolean values for each instance or class + +=item * + +hash: get and set values in a hash refered to in each instance + +=back + + +=head1 SEE ALSO + +See L for general information about this distribution. + +=cut diff --git a/lib/Class/MakeMethods/Docs/Changes.pod b/lib/Class/MakeMethods/Docs/Changes.pod new file mode 100644 index 0000000..8b01f0a --- /dev/null +++ b/lib/Class/MakeMethods/Docs/Changes.pod @@ -0,0 +1,661 @@ +=head1 NAME + +Class::MakeMethods::Docs::Changes - History of Class::MakeMethods + +=head1 SYNOPSIS + +Revision history for Class::MakeMethods. + +=head1 CHANGES + +=head2 Version 1.010 + +=over 4 + +=item 2004/09/06 + +Moved get_declarations() and associated documentation from Standard module to superclass. + +=item 2004/09/03 + +Developed test.pl test harness with recursive file search to fix Windows "command line too long" errors. + +=item 2004/09/01 + +Moved Template::TextBuilder and Template::DiskCache into Utility:: namespace. + +Added support for defaults with -- and -param to Standard get_declarations(). + +=item 2004/08/12 + +Began testing and integrating tied-value patches from Dominique Dumont. + +=item 2004/04/27 + +Added method types to Evaled::Hash. + +=item 2004/04/23 + +Added skeleton of a new Evaled::Hash class. + +=back + +=head2 Version 1.009 + +=over 4 + +=item 2003/09/25 + +Added Emulator::mcoder and compatibility tests. + +Released to CPAN as Class-MakeMethods-1.009.tar.gz. + +=item 2003/09/22 + +Added support for lvalue methods to Template and Template::Generic. Added a few tests to demonstrate they're working. Added an example to Docs::Examples. + +Added Emulator::accessors and compatibility tests. + +Minor documentation improvements. + +=back + +=head2 Version 1.008 + +=over 4 + +=item 2003/09/05 + +Adjusted layout of test directories in another attempt to solve a MakeMaker/shell-glob issue on Windows that was preventing make test from running correctly. + +Removed Template::PseudoHash, since this package never really worked, as pointed out by a question from Mike Castle. Management of array-based objects can be handled by any of the existing ::Array subclasses, and support for pseudo-hashes would not provide any useful new capabilities. + +Added support for "Template::Universal:forward_methods -delegate" and "Template::Generic:object --soft_delegate" based on a suggestion from Peter Chen. Extended behavior of Template -subs handling to make it easy to add such functionality in the future. + +Released to CPAN as Class-MakeMethods-1.008.tar.gz. + +=item 2003/09/02 + +Adjusted DESTROY behavior of Template::InsideOut and cleaned up documentation. + +=back + +=head2 Version 1.007 + +=over 4 + +=item 2003/09/01 + +Made Template definitions automatically import their class's generic +definitions, if present. This eliminates the need for Generic +subclasses to explicitly redeclare every method it inherits, and +should obviate the "missing declaration" problems referenced below. + +Updated the names of several Template subclasses, with stubs at +the old names for backwards compatibility: Flyweight becomes +InsideOut, Static becomes Global, and Struct becomes Array. + +Added Template::Inheritable and basic tests for it. + +Eliminated use of legacy Test.pm from remaining tests, except for +MethodMaker emulation. + +Rearranged test directories in an effort to avoid a reported bug +with the test path of t/*/*.t under Windows. + +Released to CPAN as Class-MakeMethods-1.007.tar.gz. + +=item 2003/08/27 + +Added section to Class::MakeMethods/EXTENDING documentation based +on question from Terrence Brannon. + +=item 2003/02/07 + +Fixed missing declaration of Template::Hash:instance, reported via RT. + +=back + +=head2 Version 1.006 + +=over 4 + +=item 2003/01/26 + +Additional documentation touch-ups. Moved miscellaneous POD files into the Docs directory. + +Added new test scripts from Class-MethodMaker-1.08, although we +don't yet pass them. In particular, I need to add support for the +new method types added in 1.04: tie_scalar, tie_list, object_tie_list, +object_tie_hash + +Also need to compare against the changes included in Class-MethodMaker-1.09 and 1.10, which don't seem to include any new test code, but do include functionality changes. + +=item 2002/12/12 + +Re-integrated Template and Emulator packages; the separate distribution +turned out to be a hastle rather than a convenience. However, in +order to keep test scripts for each subsystem separate, I'm now +using a Makefile.PL attribute to specify a two-level deep directory +tree of test scripts; I hope this doesn't break on Windows... + +Fixed possible "use of undefined as a reference" problem in +Standard::*::array method generators, reported by Jared Rhine. + +Tried to improve documentation, based on feedback from Jared Rhine. +Expunged ReadMe.pod. Extracted method catalogs into Catalog.pod. +Moved examples to new Example.pod, although that underlines how +few examples there are. + +=back + + +=head2 Version 1.005 + +=over 4 + +=item 2002/06/06 + +Added Autoload interface. + +Modifed Attribute interface to add "inheritable" default logic for +Maker class parameter. (Suggested by Malcolm Cook.) + +Fixed typo in documentation for Standard::Universal. (Spotted by +Malcolm Cook.) + +=back + + +=head2 Version 1.004 + +=over 4 + +=item 2002/03/23 + +Released to CPAN as Class-MakeMethods-1.004.tar.gz. + +=item 2002/03/16 + +Allow double-colons between package name and method generator name. + +=item 2002/02/19 + +Fixed related use of undef in Standard::*:hash methods. + +=item 2002/02/14 + +Adjusted Standard::*:hash methods to avoid assuming that the hashref +already exists. + +=item 2002/02/07 + +Added missing *_reset => clear to Template number --counter interface. + +=item 2002/02/02 + +Adjusted error message in Utility::ArraySplicer + +=item 2002/01/26 + +Applied small documentation corrections suggested by Adam Spiers. + +Added Standard::Universal:alias. + +=back + +=head2 Version 1.003 + +=over 4 + +=item 2002/01/24 + +Folded "Getting Started Guide" POD into main module documentation. + +Renamed Utility::TakeName to Emulator. + +Split Template and Emulator packages into their own distributions. + +B This means that to fully upgrade you must retrieve +all three of these files: + + Class-MakeMethods-1.003.tar.gz + Class-MakeMethods-Template-1.003.tar.gz + Class-MakeMethods-Emulator-1.003.tar.gz + +Of course, if you're not using the Template or Emulator classes, +there's no need to download them... + + +=item 2002/01/21 + +Started bumping sub-version numbers and not using sub-sub-versions, +to shorten distribution names and more closely match standard +practice. + +Added Composite::Inheritable:hook and matching test. Added +Composite->CurrentResults method to easily access, update composite +method results. + +=back + +=head2 Version 1.000.* + +=over 4 + +=item v1.000.16 - 2002/01/21 + +Released to CPAN as v1.000.016. + +=item v1.000.16 - 2002/01/20 + +Adjusted the hash and array methods in the Standard::* and Composite::* +packages to properly accept a set-contents call with a single +reference argument, and to return contents rather than ref in list +context. + +=item v1.000.16 - 2002/01/14 + +Fixed a subtle bug in a test script inherited from Class::MethodMaker: +4_Template_hash_hash_array.t and 7_MethodMaker_hash_of_lists.t both +relied on "keys %hash" returning the keys in a particular order, +which *almost* always worked, but caused failures on one or more +Perl version/platform combinations. + + +=item v1.000.15 - 2002/01/14 + +Released to CPAN as v1.000.015. + +=item v1.000.15 - 2002/01/12 + +Renamed Basic::Static to Basic::Global for consistency with Standard +and Composite. Hopefully, there aren't many users of this module +yet; please accept my apologies if this breaks your code. + +Eliminated "local @_ = ...", which appears to cause a scoping +problem on Perl 5.6. Thanks to Adam Spiers for a thorough bug +report. (See http://www.perlmonks.org/index.pl?node_id=138370 for +details.) + +Extended Template::Generic to support "array --get_set_ref" method +style requested by Adam Spiers. + +Various documentation tweaks, including feedback from Adam Spiers: +Adjusted documentation to downplay Basic::* modules as a starting +point, in favor of Standard::* ones. Trimmed out some duplicated +documentation in favor of more "See LE...E" links. Adjusted +documentation of *::Inheritable packages in an attempt to clarify +the way in which the inheritance tree is searched for a value. + +Factored out common code from Standard::Inheritable and +Composite::Inheritable to new module, Utility::Inheritable. Factored +out common code from Standard::Hash and Standard::Array to new +module, Utility::ArraySplicer. Factored out common code from +Template::Universal to new module, Utility::Ref. Renamed +Emulator::TakeName to Utility::TakeName (this is internal use only, +so there should be no public impact). + + +=item v1.000.15 - 2001/12/01 + +Adjusted Template::Universal's code for _CALL_METHODS_FROM_HASH_, +to ensure that method/arg pairs are called in order they were passed +in. + +=item v1.000.15 - 2001/07/04, 2001/07/19 + +Minor additions to documentation of various method types. + + +=item v1.000.14 - 2001/07/01 + +Released as v1.000.014. + + +=item v1.000.14 - 2001/06/25, 2001/06/29, 2001/07/01 + +Removed Makefile rule from Makefile.PL to avoid warnings when used +with recent versions of ExtUtils::MakeMaker, which also define a +similar rule. (Based on bug report from Ron Savage.) + +Fixed test failure for machines with P5.6 but no Attribute::Handlers. +(Reported by Ron Savage, Jay Lawrence.) + +Added Template::Flyweight:string_index. (But still needs test +script.) + +Added Standard::Universal. (But still needs test scripts.) + +Minor touch-ups to ReadMe and Guide documentation. + + +=item v1.000.13 - 2001/05/16, 2001/05/18, 2001/05/20, 2001/06/02, 2001/06/22, 2001/06/24 + +To date, this module has been circulated under several provisional +names: it was originally floated as a possible version-2 rewrite +of Class::MethodMaker, then renamed to Class::Methods when it forked +from that project, and then briefly to Class::MethodGenerator. +(Note that it can be surprisingly difficult to comply with both of +these L guidelines: "To be portable each component of +a module name should be limited to 11 characters. [...] Always +try to use two or more whole words.") In the end, I selected +Class::MakeMethods, as it is two whole words, and is reminiscent +of Class::MethodMaker without being confusing (I hope!), and I +believe this issue is now settled. + +Standardized syntax for global options; renamed -implementation to +-MakerClass and -target_class to -TargetClass. Moved $TargetClass +and other context information into %CONTEXT with _context accessor. +Added ForceInstall. + +Completed re-simplification of build directories; we're back to a +single Makefile, which avoids a warning in P5.6.0. + +Added Attribute interface for use with P5.6 and later, based on +Attribute::Handlers. + +Renamed "Simple" subclasses to "Basic". Added documentation and +initial tests. + +Added Standard subclasses with parameter parsing and more powerful +accessors. + +Modified Emulator::Struct to use Standard::* methods. Found struct +test from P5.7, and added auto_init functionality to match. + +Added Composite::* subclasses. + +Added Emulator::AccessorFast. + +Added Class::MakeMethods::Guide with introduction and examples. + +Continued clean-up effort on Template documentation. Renamed Template +"attributes" to "method parameters" to avoid confusion with Perl +attributes. Retitled Template naming rules from "templates" to +"interfaces". + +Changed initialization code expressions of Template::Class in hopes +of P5.6.1 compatibility. (Problem reported by M Schwern.) + +Added 'Template::Generic:new --and_then_init' based on feedback +from Jay Lawrence. + +=back + +=head2 Early 1.000 versions + +=over 4 + +=item v1.000.12 - 2001/05/14 + +Renamed module to Class::MethodGenerator, although naming questions +remain. + +Moved Template subclasses into Template::* namespace. Simplified +build directory and makefile structure. + +Changed initialization code expressions of Template::PackageVar, +ClassVar for P5.6.0 compatibility. (Reported by M Schwern.) + + +=item v1.000.11 - 2001/05/07, 2001/05/12 + +Eliminated Bundle file. Moved general documentation to cm_base. + +Renamed Class::Methods::Base to Class::Methods::Generator. + +Extracted code for Template declarations to new Class::Methods::Template +module. Extracted disk-caching to new Template::DiskCache module. +Moved TextBuilder into the Template:: tree. + +Moved _namespace_capture code to new package +Class::Methods::Emulator::TakeName. + +Added Simple::Hash subclass. + + +=item v1.000.10 - 2001/04/26, 2001/05/02, 2001/05/04 + +Moved _namespace_capture and _namespace_release to Class::Methods::Base. + +Additional doc tweakage. Moved ReadMe documentation to +Bundle::ClassMethods. Merged Extending documentation into Base. + +Removed spurious uses of -default => 'default' in templates. + +Added new ClassInherit subclass and Emulator::Inheritable. + +Expunged Index subclass in favor of boolean_index and string_index +types on Generic. + +Moved Struct:builtin_isa type to new package, StructBuiltin. + +Refactored code templating function as Class::Methods::Base::TextBuilder. + + +=item v1.000.9 - 2001/03/24 + +Reversed sense of - and --, as it was in 1.000.1. + +Separated source files into separate directories with distinct +Makefiles and test hierarchies. This should clarify the boundaries +between the core method-generation code, the common constructor/accessor +methods, and the various emulator and experimental packages. + + +=item v1.000.8 - 2001/01/19 + +Following receipt of a suggestion to fork from the maintainer of +Class::MethodMaker, renamed packge from Class::MethodMaker v2.0 to +Class::Methods v1.000. + +Adjusted documentation to reflect fork, although additional cleanup +is still needed. + +Moved backward compatibility to Emulator::MethodMaker subclass. + +Added Generic -compatibility array index_* and hash_of_arrays *_last +and *_set methods to match changes in Class::MethodMaker v1.02. +Added Emulator::MethodMaker support for the '-static' flag. The +emulator now completely satisfies the enclosed test suites, from +Class::MethodMaker v0.92 and v1.02. + + +=item v1.000.7 - 2001/01/05, 2001/01/06, 2001/01/07 + +Moved core code and internal code to Internals.pm. MethodMaker.pm +now contains only some require statements and the general user +guide documentation. + +Moved ReadMe.pod, Changes.pod, and ToDo.pod into MethodMaker +directory. Separated Catalog.pod, Extending.pod, RelatedModules.pod. + +Included version 1 docs as Class::Methods::OriginalDocs; minor +revisions for clarity. + +Renamed Package subclass to PackageVar, Class to ClassVar. + +Added Emulation::Struct subclass. + +Added support for shifting targets with make( -target_class => +Package, ... ). + +Extended ClassName subclass to handle requiring, rather than creating +subclases. + + +=item v1.000.6 - 2000/12/29, 2001/01/02, 2001/01/04 + +Restored -sugar import option for compatibility with earlier +versions. + +Added plural names to "Generic:hash -compatibility" to support +v0.92 usage. + +Replaced use of substr(..., 0, 1) with ... =~ s/^-// for P5.004 +compatibility; problem found by Scott Godin. + +Copy @_ before splicing and pushing on to it for P5.004 compatibility. + +Expunged duplicate lines from Generic.pm's array_of_objects; found +by Ron Savage. + +Renamed Hash.pm's delete and exists behaviors to avoid possible +run-time import conflict with Generic.pm's behaviors; failure +reported by Ron Savage. + +Added _STATIC_ATTR_{return_value_undefined} attributes to Generic +string and number to allow overrides of this functionality. + +Minor doc touchups and expanded examples section. + + +=item v1.000.5 - 2000/11/28, 2000/12/16, 2000/12/28 + +Added Universal -warn_calls modifier. + +Folded various pod files together into main module's inline +documentation. Updated catalog of existing implementations in +documentation. Added pointers to some tutorials and books which +discuss Class::Methods. + +Standardized naming of test scripts. + +Can now specify default template name, via -default=>"foo". + + +=item v1.000.4 - 2000/11/22 + +Separated string, number, and boolean from the Generic scalar +methods. + +Provide _disk_cache to shortcut the lengthy _interpret_text_builder +process. + +Fixes to ClassName implementation. + +Change to forward methods to provide better error messages when +object is empty. + + +=item v1.000.3 - 2000/11/03 + +Rearranged documentation into separate files in the pod/ directory. + +Collapsed find_target_class and make functionality into import; +moved support for the old functions to the Compatibility module. + +Adjusted tests to generally use standard syntax, and not Compatibility +hooks. + + +=item v1.000.2.1 - 2000/10/23 + +Moved commonly-accessible information to Universal. + +Added block{...} replacement for enhanced behavior templating. + +Added modifier mechanism to support -private and -protected. + +May need to be able to specify import ordering so that modifiers +are applied in the right order. This hasn't bit me yet, but it's +there. Darn. + + +=item v1.000.2 - 2000/10/22 + +Completed generalization of Generic methods from Static and Hash. +Rewrote ClassVar and PackageVar to use Generic framework. + +Attribute expansion can now substitute values besides name, using +*{attr}. + +Added _diagnostics function and documentation of all failure +messages. + +Added SEE ALSO section to documentation, brief review of Class::* +on CPAN. Stumbled across Damian Conway's very nice Class::Contract +module. + +Added Scalar and Flyweight implementations. + + +=item v1.000.1.1 - 2000/10/21 + +Rolled back change from yesterday; can still pick templates like +'-java'. Allow attributes to be specified as '--foo'=>'bar' or +'--'=>{foo=>'bar'} + +Automated caching for meta-method definition hashes. + +Generalized several Static and Hash interfaces into Generic templates. +Added Static:array and Static:code support. + +Allow global -import to set default sources for templates, exprs, +behaviors. + + +=item v1.000.1 - 2000/10/19 + +Support inheritance of templates between meta-methods with -import. + +Made "template" an attribute, rather than a special state variable. + +Allow any attribute to be specified as -foo=>'bar'. Changed +selection of standard templates from '-java' to '--java'. + +Initial support for string-eval behaviors and code_exprs, and +Generic.pm + + +=item v1.000.0 - 2000/10/14, 2000/10/15 + +Completed initial pass of full rewrite. + +Assorted cleanup of syntax and documentation. + +Moved Hash, Static, and Index implementations into separate packages. + + +=item v0.9.3 - 2000/09/30 + +Refactored subclass_name and class_registry. + +Folded in some misc improvements from Class::MethodMaker 1.0. + + +=item v0.97x - 2000/08/04 to 2000/08/13 + +Forked from Class::MethodMaker 0.96. Substantial rewrite started + +Created build_meta_method and refactored many methods to use it. + +Added new_hash, hash_init, new_from_prototype. + +Extended arg format. Added -template=>behavior_name. Added support +for array-of-names arguments. + +Performance tuning. Additional refactoring to support AutoSplit +functionality. + +Also folded in some older changes and additions from Evolution's +internal collection of MethodMaker subclasses: + +=back + +=head2 Class::MethodMaker::Extensions + +Change notes from unreleased collection of extensions to Class::MethodMaker that were later folded into Class::MakeMethods: + + 2000/01/12 Added set_foo, clear_foo to class_var hashes. + 1999/07/27 Added subclass_name. + 1999/04/15 Changed class_var to use symbol table lookups, not eval "". + 1999/04/05 Changed determine_once to check again if undefined. + 1999/03/25 Added singleton method. + 1998/09/18 Finished integration of class_registry handlers. + 1998/07/31 Added class_var and classnames handlers. + 1998/06/12 Added lookup handlers. + 1998/05/09 Created no_op and determine_once method groups. + +=cut diff --git a/lib/Class/MakeMethods/Docs/Examples.pod b/lib/Class/MakeMethods/Docs/Examples.pod new file mode 100644 index 0000000..787ace7 --- /dev/null +++ b/lib/Class/MakeMethods/Docs/Examples.pod @@ -0,0 +1,554 @@ +=head1 NAME + +Class::MakeMethods::Docs::Examples - Sample Declarations and Usage + +=head1 EXAMPLES + +The following examples indicate some of the capabilities of +Class::MakeMethods. + +=head2 A Contrived Example + +Object-oriented Perl code is widespread -- you've probably seen code like the below a million times: + + my $obj = MyStruct->new( foo=>"Foozle", bar=>"Bozzle" ); + if ( $obj->foo() =~ /foo/i ) { + $obj->bar("Barbados!"); + } + +Here's a possible implementation for the class whose interface is +shown above: + + package MyStruct; + + sub new { + my $callee = shift; + my $self = bless { @_ }, (ref $callee || $callee); + return $self; + } + + sub foo { + my $self = shift; + if ( scalar @_ ) { + $self->{'foo'} = shift(); + } else { + $self->{'foo'} + } + } + + sub bar { + my $self = shift; + if ( scalar @_ ) { + $self->{'bar'} = shift(); + } else { + $self->{'bar'} + } + } + +Class::MakeMethods allows you to simply declare those methods to +be of a predefined type, and it generates and installs the necessary +methods in your package at compile-time. + +Here's the equivalent declaration for that same basic class: + + package MyStruct; + use Class::MakeMethods::Standard::Hash ( + 'new' => 'new', + 'scalar' => 'foo', + 'scalar' => 'bar', + ); + +=head2 A Typical Example + +The following example shows a common case of constructing a class with several types of accessor methods + + package MyObject; + use Class::MakeMethods::Standard::Hash ( + new => 'new', + scalar => [ 'foo', 'bar' ], + array => 'my_list', + hash => 'my_index', + ); + +This class now has a constructor named new, two scalar accessors named foo and bar, and a pair of reference accessors named my_list and my_index. Typical usage of the class might include calls like the following: + + my $obj = MyObject->new( foo => 'Foozle' ); + print $obj->foo(); + + $obj->bar('Barbados'); + print $obj->bar(); + + $obj->my_list(0 => 'Foozle', 1 => 'Bang!'); + print $obj->my_list(1); + + $obj->my_index('broccoli' => 'Blah!', 'foo' => 'Fiddle'); + print $obj->my_index('foo'); + +=head2 Lvalue Accessors + +The Template subclasses support an optional "--lvalue" modifer that causes your accessors method to be marked as returning an lvalue which can be assigned to. (This feature is only available on Perl 5.6 or later.) + + package MyStruct; + use Class::MakeMethods::Template::Hash ( + 'new' => 'new', + 'scalar --get --lvalue' => 'foo', + 'array --get --lvalue' => 'bar', + ); + + $obj->foo = "Foozle"; + print $obj->foo; + + $obj->bar = ( 'baz', 'beep', 'boop' ); + print $obj->bar->[1]; # beep + +=head2 String and Numeric Accessors + +In addition to the C accessor supported by the C classes, the Template subclasses also provide specialized accessors that can facilitate the use of specific types of data. + +For example, we could declare the following class to hold information +about available Perl packages: + + package MyVersionInfo; + use Class::MakeMethods::Template::Hash ( + 'new' => 'new', + 'string' => 'package', + 'number' => 'version', + ); + + sub summary { + my $self = shift; + return $self->package() . " is at version " . $self->version() + } + +You could use this class as follows: + + package main; + use MyVersionInfo; + + my $obj = MyVersionInfo->new( package=>"Class::MakeMethods"); + $obj->version( 2.0 ); + print $obj->summary(); + +These accessors will provide a bit of diagnostic type checking; +an attempt to call C<$obj-Eversion("foo")> will cause your +program to croak. + + +=head2 String Concatenation Interface + +The following defines a get_concat method C, and specifies +a string to use when joining additional values when this method is +called. + + use Class::MakeMethods::Template::Hash + 'string' => [ '--get_concat', 'i', { join => ' - ' } ]; + +(See L for information about the C C interface.) + + +=head2 Access Control Example + +The following defines a secret_password method, which will croak +if it is called from outside of the declaring package. + + use Class::MakeMethods::Composite::Hash + 'scalar' => [ 'secret_password' => { permit => 'pp' } ]; + +(See L for information +about the C modifier.) + +For template classes, the same thing is accomplished with '--private': + + use Class::MakeMethods::Template::Hash + 'scalar' => [ '--private', 'secret_password' ]; + +(See L for information +about the C modifier.) + + +=head2 Lazy-Init Interface + +Templapte scalar accessors declared with the "init_and_get" interface +can be used for "memoization" or lazy-evaluation for object +attributes. If the current accessor value is undefined, they will +first call a user-provided init_* method and save its value. + + package MyWidget; + use Class::MakeMethods::Template::Hash ( + 'new --with_values' => [ 'new' ], + 'scalar --init_and_get' => [ 'foo', 'count', 'result' ], + ); + + sub init_foo { + return 'foofle'; + } + + sub init_count { + return '3'; + } + + sub init_result { + my $self = shift; + return $self->foo x $self->count; + } + ... + + my $widget = MyWidget->new(); + print $widget->result; # output: fooflefooflefoofle + + # if values are predefined, the init methods are not used + my $other_widget = MyWidget->new( foo => 'bar', count => 2 ); + print $widget->result; # output: barbar + +(See L for more information about +C. This interface is also supported by all of Generic's +subclasses, so you can add lazy-init methods for global data, class +data, array objects, etc. Unfortunately, to date it is only supported +for scalar-value accessors...) + + +=head2 Helper Methods + +Template methods often include similarly-named "helper" methods. For example, specifying the "--with_clear" interface for Template::*:scalar methods creates an extra method for each accessor x named clear_x. + + package MyClass; + use Class::MakeMethods::Template::Hash('scalar --with_clear' => 'foo'); + + my $obj = MyClass->new; + $obj->foo(23); + $obj->clear_foo; + print $obj->foo(); + + +=head2 Reference Accessor and Helper Methods + +For references to arrays and hashes, the Template subclasses provide +accessors with extra "helper methods" to facilitate method-based +interaction. + +Here's a class whose instances each store a string and an array +reference, along with a method to search the directories: + + package MySearchPath; + use Class::MakeMethods::Template::Hash ( + 'new' => 'new', + 'string' => 'name', + 'array' => 'directories', + ); + + sub search { + my $self = shift; + my $target = shift; + foreach my $dir ( $self->directories ) { + my $candidate = $dir . '/' . $target; + return $candidate if ( -e $candidate ); + } + return; + } + +Note that the directories accessor returns the contents of the +array when called in a list context, making it easier to loop over. + +And here's a sample usage: + + package main; + use MySearchPath; + + my $libs = MySearchPath->new( name=>"libs", directories=>['/usr/lib'] ); + $libs->push_directories( '/usr/local/lib' ); + + print "Searching in " . $libs->count_directories() . "directories.\n"; + foreach ( 'libtiff', 'libjpeg' ) { + my $file = $libs->search("$_.so"); + print "Checking $_: " . ( $file || 'not found' ) . "\n"; + } + +Note the use of the push_* and count_* "helper" accessor methods, +which are defined by default for all 'Template::*:array' declarations. + +Consult L for more information about +the available types of reference accessors, and the various methods +they define. + + +=head2 Object Accessors + +There's also a specialized accessor for object references: + + package MyStruct; + use Class::MakeMethods::Template::Hash ( + 'new' => 'new', + 'object' => [ 'widget' => {class=>'MyWidgetClass', delegate=>"twiddle"} ], + ); + +(Note that the C and C values specified above are +method parameters, which provide additional information about the +C declaration; see L<"Standard Declaration Syntax"> for more information.) + +The above declaration creates methods equivalent to the following: + + package MyStruct; + + sub widget { + my $self = shift; + if ( scalar @_ ) { + if (ref $_[0] and UNIVERSAL::isa($_[0], 'MyWidgetClass')) { + $self->{widget} = shift; + } else { + $self->{widget} = MyWidgetClass->new(@_); + } + } else { + return $self->{widget}; + } + } + + sub clear_widget { + my $self = shift; + $self->{widget} = undef; + } + + sub twiddle { + my $self = shift; + my $obj = $self->widget() + or Carp::croak("Can't forward twiddle because widget is empty"); + $obj->twiddle(@_) + } + + +=head2 Mixing Object and Global Methods + +Here's a package declaration using two of the included subclasses, C, for creating and accessing hash-based objects, and C, for simple global-value accessors: + + package MyQueueItem; + + use Class::MakeMethods::Standard::Hash ( + new => { name => 'new', defaults=>{ foo => 'Foozle' } }, + scalar => [ 'foo', 'bar' ], + hash => 'history' + ); + + use Class::MakeMethods::Basic::Global ( + scalar => 'Debug', + array => 'InQueue', + ); + + sub AddQueueItem { + my $class = shift; + my $instance = shift; + $instance->history('AddQueueItem' => time()); + $class->InQueue([0, 0], $instance); + } + + sub GetQueueItem { + my $class = shift; + $class->InQueue([0, 1], []) or $class->new + } + +=head2 Adding Custom Initialization to Constructors + +Frequently you'll want to provide some custom code to initialize new objects of your class. Most of the C<*:new> constructor methods provides a way to ensure that this code is consistently called every time a new instance is created. + +=over 4 + +=item Composite::Hash:new { post_rules => [] } + +The Composite classes allow you to add pre- and post-operations to any method, so you can pass in a code-ref to be executed after the new() method. + + package MyClass; + + sub new_post_init { + my $self = ${(pop)->{result}}; # get result of original new() + length($self->foo) or $self->foo('FooBar'); # default value + warn "Initialized new object '$self'"; + } + + use Class::MakeMethods ( + 'Composite::Hash:new' => [ + 'new' => { post_rules=>[ \&new_post_init ] } + ], + 'Composite::Hash:scalar' => 'foo;, + ); + ... + package main; + my $self = MyClass->new( foo => 'Foozle' ) + +=item Template::Hash:new --and_then_init + +Use 'Template::Hash:new --and_then_init', which will first create the object and initialize it with the provided values, and then call an init() method on the new object before returning it. + + package MyClass; + use Class::MakeMethods::Template::Hash ( + 'new --and_then_init' => 'new' + 'string' => 'foo' + ); + sub init { + my $self = shift; + length($self->foo) or $self->foo('FooBar'); # default value + warn "Initialized new object '$self'"; + } + ... + package main; + my $self = MyClass->new( foo => 'Foozle' ) + +=item Template::Hash:new --with_init + +If you don't want your constructor to use the default hash-of-method-names style of initialization, use 'Template::Hash:new --with_init', which will create an empty object, pass its arguments to the init() method on the new object, and then return it. + + package MyClass; + use Class::MakeMethods::Template::Hash ( + 'new --with_init' => 'new' + 'string' => 'foo' + ); + sub init { + my $self = shift; + $self->foo( shift || 'FooBar' ); # init with arg or default + warn "Initialized new object '$self'"; + } + ... + package main; + my $self = MyClass->new( 'Foozle' ) + +=back + +Some additional notes about these constructors: + +=over 4 + +=item * + +The C methods allow you to specify a name for your method other than C by passing the C parameter: + + use Class::MakeMethods::Template::Hash ( + 'new --and_then_init' => [ + 'new' => { init_method => 'my_init' } + ], + ); + +=item * + +If you know that you're not going to have a complex class hierarchy, you can reduce resource consumption a bit by changing the above declarations from "*::Hash" to "*::Array" so your objects end up as blessed arrays rather than blessed hashes. + +=back + + +=head2 Changing Method Names + +The Template subclasses allow you to control the names assigned to +the methods you generate by selecting from several naming interfaces. + +For example, the accessors declared above use a default, Perl-ish +style interface, in which a single method can be called without an +argument to retrieve the value, or with an argument to set it. +However, you can also select a more Java-like syntax, with separate +get* and set* methods, by including the '--java' template specification: + + package MyStruct; + use Class::MakeMethods::Template::Hash ( + 'new' => 'new', + 'scalar' => '--java Foo', + ); + +(Note that the declaration of Foo could also have been written as +C<'scalar --java' =E 'Foo'> or C<'scalar' =E ['--java', +'Foo']>, or C<'scalar' =E [ 'foo' => { 'interface'=>'java' } +], all of which are interpreted identically; see the +L section on "Argument Normalization" for +details.) + +Usage of this accessor would then be as follows: + + package main; + use MyStruct; + + my $obj = MyStruct->new( setFoo => "Foozle" ); + print $obj->getFoo(); + $obj->setFoo("Bozzle"); + + +=head2 Selecting Specific Helper Methods + +You can use the ability to specify interfaces to select specific helper methods rather than getting the default collection. + +For example, let's say you wanted to use a Template::Hash:array, but you only wanted two methods to be installed in your class, a foo() accessor and a shift_foo() mutator. Any of the below combinations of syntax should do the trick: + + use Class::MakeMethods::Template::Hash + 'array' => [ + 'foo' => { interface=>{'foo'=>'get_set', 'shift_foo'=>'shift'} }, + ]; + +If you're going to have a lot of methods with the same interface, you could pre-declare a named interface once and use it repeatedly: + + BEGIN { + require Class::MakeMethods::Template::Hash; + Class::MakeMethods::Template::Hash->named_method('array')-> + {'interface'}->{'my_get_set_shift'} = + { '*'=>'get_set', 'shift_*'=>'shift' }; + } + + use Class::MakeMethods::Template::Hash + 'array --my_get_set_shift' => [ 'foo', 'bar' ]; + + +=head2 Tree Structure Example + +In this example we will create a pair of classes with references +to other objects. + +The first class is a single-value data object implemented as a +reference to a scalar. + + package MyTreeData; + use Class::MakeMethods::Template::Scalar ( + 'new' => 'new', + 'string' => 'value', + ); + +The second class defines a node in a tree, with a constructor, an +accessor for a data object from the class above, and accessors for +a list of child nodes. + + package MyTreeNode; + use Class::MakeMethods::Template::Hash ( + 'new' => 'new', + 'object -class MyTreeData' => 'data', + 'array_of_objects -class MyTreeNode' => 'children', + ); + + sub depth_first_data { + my $self = shift; + return $self->data, map { $_->depth_first_data() } $self->children; + } + +Here's a sample of how the above classes could be used in a program. + + package main; + use MyTreeData; + use MyTreeNode; + + my $node = MyTreeNode->new( + data => { value=>'data1' }, + children => [ { value=>'data3' } ] + ); + $node->push_children( MyTreeNode->new( data => { value=>'data2' } ) ); + + foreach my $data ( $node->depth_first_data ) { + print $data->value(); + } + + +=head1 SEE ALSO + +See L for general information about this distribution. + +=head2 Annotated Tutorials + +Ron Savage has posted a pair of annotated examples, linked to below. +Each demonstrates building a class with MakeMethods, and each +includes scads of comments that walk you through the logic and +demonstrate how the various methods work together. + + http://savage.net.au/Perl-tutorials.html + http://savage.net.au/Perl-tutorials/tut-33.tgz + http://savage.net.au/Perl-tutorials/tut-34.tgz + +=cut diff --git a/lib/Class/MakeMethods/Docs/ReadMe.pod b/lib/Class/MakeMethods/Docs/ReadMe.pod new file mode 100644 index 0000000..6fd7890 --- /dev/null +++ b/lib/Class/MakeMethods/Docs/ReadMe.pod @@ -0,0 +1,279 @@ +=head1 NAME + +Class::MakeMethods::Docs::ReadMe - About Class::MakeMethods + + +=head1 DESCRIPTION + +This is an updated release of Class::MakeMethods, for distribution through +CPAN. + +This distribution includes the Class::MakeMethods::Template and +Class::MakeMethods::Emulator modules which were packaged sepearately in some +previous releases. + + +=head1 MOTIVATION + +By passing arguments to "use Class::MakeMethods ..." statements, you can +select from a library of hundreds of common types of methods, which are +dynamically installed as subroutines in your module, simplifying the code +for your class. + + +=head1 DISTRIBUTION AND INSTALLATION + +=head2 Version + +This is Class::MakeMethods v1.010, intended for general use. + +This module's CPAN registration should read: + + Name DSLIP Description + -------------- ----- --------------------------------------------- + Class:: + ::MakeMethods RdpOp Generate common types of methods + +=head2 Prerequisites + +In general, this module should work with Perl 5.003 or later, +without requring any modules beyond the core Perl distribution. + +The following optional feature may not be available on some platforms: + +=over 4 + +=item * + +Class::MakeMethods::Attribute: The C<:MakeMethod> subroutine +attribute requires Perl version 5.6 and the Attribute::Handlers +module (from CPAN). + +=item * + +Class::MakeMethods::Template C<--lvalue>: The lvalue modifier +provided by the Template generator subclasses will only work on +Perl version 5.6 or later. + +=item * + +Some third-party tests used to check the compliance of Emulator modules +require Test::More and will be automatically skipped on machines which do +not have this installed. + +=back + +=head2 Installation + +You should be able to install this module using the CPAN shell interface: + + perl -MCPAN -e 'install Class::MakeMethods' + +Alternately, you may retrieve this package from CPAN or from the author's site: + +=over 2 + +=item * + +http://search.cpan.org/~evo/ + +=item * + +http://www.cpan.org/modules/by-authors/id/E/EV/EVO + +=item * + +http://www.evoscript.org/Class-MakeMethods/dist/ + +=back + +After downloading the distribution, follow the normal procedure to unpack and install it, using the commands shown below or their local equivalents on your system: + + tar xzf Class-MakeMethods-*.tar.gz + cd Class-MakeMethods-* + perl Makefile.PL + make test && sudo make install + +Thanks to the kind generosity of other members of the Perl community, +this distribution is also available repackaged in the FreeBSD +"ports" and Linux RPM formats. This may simplify installation for +some users, but be aware that these alternate distributions may +lag a few versions behind the latest release on CPAN. + +=over 2 + +=item * + +http://www.freebsd.org/cgi/ports.cgi?query=Class-MakeMethods + +=item * + +http://www.rpmfind.net/linux/rpm2html/search.php?query=perl-Class-MakeMethods + +=back + +=head2 Tested Platforms + +This release has been tested succesfully on the following platforms: + + 5.6.1 on darwin + +Earlier releases have also tested OK on the following platforms: + + IP30-R12000-irix + OpenBSD.i386-openbsd + i386-freebsd / i386-freebsd-thread-multi + i386-linux + i386-netbsd / i386-netbsd-thread-multi + i586-linux / i586-linux-thread-multi-ld + i686-linux / i686-pld-linux-thread-multi + ia64-linux + ppc-linux + sparc-linux + sparc-netbsd + sun4-solaris + +Some earlier versions failed to "make test" on MSWin32, although +a forced installation would still work; that problem should be +fixed in the most recent releases. + +You may also review the current test results from CPAN-Testers: + +=over 2 + +=item * + +http://testers.cpan.org/show/Class-MakeMethods.html + +=back + +=head1 SUPPORT + +=head2 Release Status + +This module has been used in a variety of production systems and +has been available on CPAN for over two years, with several other +distributions dependant on it, so it would be fair to say that it +is fully released. + +However, while the commonly-used portions are well tested, some of +the more obscure combinations of options are less so, and new bug +reports do trickle in occasionally. If you do encounter any problems, +please inform the author and I'll endeavor to patch them promptly. + +Additional features have been outlined for future development, but +the intent is support these by adding more options to the declaration +interface, while maintaining backward compatibility. + +See L for other outstanding issues +and development plans. + +=head2 Support + +If you have questions or feedback about this module, please feel +free to contact the author at the below address. Although there is +no formal support program, I do attempt to answer email promptly. + +I would be particularly interested in any suggestions towards +improving the documentation and correcting any Perl-version or platform +dependencies, as well as general feedback and suggested additions. + +Bug reports that contain a failing test case are greatly appreciated, +and suggested patches will be promptly considered for inclusion in +future releases. + +To report bugs via the CPAN web tracking system, go to +C or send mail +to C, replacing C<#> with C<@>. + +=head2 Community + +If you've found this module useful or have feedback about your +experience with it, consider sharing your opinion with other Perl +users by posting your comment to CPAN's ratings system: + +=over 2 + +=item * + +http://cpanratings.perl.org/rate/?distribution=Class-MakeMethods + +=back + +For more general discussion, you may wish to post a message on PerlMonks or the comp.lang.perl.misc newsgroup: + +=over 2 + +=item * + +http://www.perlmonks.org/index.pl?node=Seekers%20of%20Perl%20Wisdom + +=item * + +http://groups.google.com/groups?group=comp.lang.perl.misc + +=back + + +=head1 CREDITS AND COPYRIGHT + +=head2 Author + +Developed by Matthew Simon Cavalletto at Evolution Softworks. +More free Perl software is available at C. + +You may contact the author directly at C or C. + +=head2 Feedback and Suggestions + +Thanks to the following people for bug reports, suggestions, and other feedback: + + Martyn J. Pearce + Scott R. Godin + Ron Savage + Jay Lawrence + Adam Spiers + Malcolm Cook + Terrence Brannon + Jared Rhine + Peter Chen + Mike Castle + +=head2 Source Material + +This package was inspired by the ground-breaking original closure-generating method maker module: + + Class::MethodMaker, by Peter Seibel. + +Additional inspiration, cool tricks, and blocks of useful code for +this module were extracted from the following CPAN modules: + + Class::Accessor, by Michael G Schwern + Class::Contract, by Damian Conway + Class::SelfMethods, by Toby Everett + +=head2 Copyright + +Copyright 2002, 2003 Matthew Simon Cavalletto. + +Portions copyright 1998, 1999, 2000, 2001 Evolution Online Systems, Inc. + +Based on Class::MethodMaker, originally developed by Peter Seibel. Portions Copyright 1996 Organic Online. Portions Copyright 2000 Martyn J. Pearce. + +Class::MakeMethods::Emulator::accessors is based on accessors. Portions by Steve Purkis. + +Class::MakeMethods::Emulator::AccessorFast is based on Class::Accessor::Fast. Portions Copyright 2000 Michael G Schwern. + +Class::MakeMethods::Emulator::Inheritable is based on Class::Data::Inheritable. Portions Copyright 2000 Damian Conway and Michael G Schwern. + +Class::MakeMethods::Emulator::mcoder is based on mcoder. Portions Copyright 2003 by Salvador Fandiño. + +Class::MakeMethods::Emulator::Singleton is based on Class::Singleton, by Andy Wardley. Portions Copyright 1998 Canon Research Centre Europe Ltd. + +Class::MakeMethods::Utility::Ref is based on Ref.pm. Portions Copyright 1994 David Muir Sharnoff. + +=head2 License + +You may use, modify, and distribute this software under the same terms as Perl. + +=cut diff --git a/lib/Class/MakeMethods/Docs/RelatedModules.pod b/lib/Class/MakeMethods/Docs/RelatedModules.pod new file mode 100644 index 0000000..93ef930 --- /dev/null +++ b/lib/Class/MakeMethods/Docs/RelatedModules.pod @@ -0,0 +1,962 @@ +=head1 NAME + +Class::MakeMethods::Docs::RelatedModules - Survey of Class Builders + + +=head1 SYNOPSIS + + http://search.cpan.org/search?mode=module&query=Class + + +=head1 DESCRIPTION + +There are a variety of modules on CPAN dedicated to the purpose of +generating common constructor and accessor methods. Below, I survey +several of these, summarizing some basic features and technical +approaches, and comparing them to Class::MakeMethods and other +modules. + + +=head2 Caution + +B Please consult the +documentation from a current version of each module for more specific +details. Corrections and clarifications would by welcomed by the author at the email address below. + + +=head2 Points of Comparison + +In general, I compared the following characteristics: + +=over 4 + +=item Distribution + +Is it included with Perl, or on CPAN? Is it being actively maintained? + +=item Usage + +How do you go about declaring your class's methods? + +=item Mechanism + +How are they generated and delivered? + +=item Instance type + +Are the objects of your class blessed hashes, or something else? + +=item Core Methods + +Does the module provide a constructor and basic accessors? Are there specialized methods for hash-ref, array-ref, and object-ref accessors? + +=item Extensible + +Can you subclass the package to create new types of methods, or is there some other way to extend it? + +=item Other Methods + +Other types of methods provided. + +=item Emulator + +Does Class::MakeMethods provide a drop-in replacement for this module? + +=item Comments + +Other characteristics or features of note. + +=back + + +=head1 RELATED MODULES + +=head2 accessors + +=over 4 + +=item Distribution + +CPAN. Uploaded Sep 2003. + +=item Comments + +I have not yet reviewed this module in detail. + +=item Example + + package MyObject; + use accessors qw( foo bar baz ); + +=back + +=head2 Attribute::Property + +=over 4 + +=item Distribution + +CPAN. + +=item Comments + +I have not yet reviewed this module in detail. + +=back + + +=head2 Class::Accessor + +=over 4 + +=item Distribution + +CPAN. Last update 4/01. + +=item Usage + +Inherit and call function with declaration arguments + +=item Mechanism + +Generates and installs closures + +=item Instance Type + +Hash. + +=item Subclasses Cleanly + +Cleanly. + +=item Standard Methods + +Scalar accessors. + +=item Extensible + +Yes. + +=item Comments + +Accessor methods call overwritable Cget(I)> and +Cset(I, I)> methods. + +Also includes Class::Accessor::Fast, which creates direct hash keys accessors without calling get and set methods. + +=item Emulator + +Yes, but only for the Fast variation; see Class::MakeMethods::Emulator::AccessorFast. + +=item Example + + package MyObject; + @ISA = qw(Class::Accessor); + MyObject->mk_accessors(qw( simple ordered mapping obj_ref )); + +=back + + +=head2 Class::Class + +=over 4 + +=item Distribution + +CPAN. Last update 1/00. + +=item Usage + +Inherit and fill %MEMBERS hash; methods created when first object is created + +=item Mechanism + +Generates and installs closures + +=item Instance Type + +Hash. + +=item Subclasses Cleanly + +Yes. + +=item Standard Methods + +Constructor and various accessors. + +=item Extensible + +No. + +=item Example + +Usage is similar to Class::Struct: + + package MyObject; + use Class::Class; + @ISA = qw(Class::Class); + %MEMBERS = ( + simple => '$', + ordered => '@', + mapping => '%', + obj_ref => 'FooObject' + ); + +=item Other Method Types + +Provides a polymorph() method that is similar to Class::Method's "ClassName:class_name -require". + +=back + + +=head2 Class::Constructor + +=over 4 + +=item Distribution + +CPAN. Last update 11/01. + +=item Usage + +Inherit and call function with declaration arguments + +=item Mechanism + +Generates and installs closures + +=item Instance Type + +Hash. + +=item Subclasses Cleanly + +Cleanly. + +=item Standard Methods + +Hash constructor, with bells. + +=item Extensible + +No. + +=item Emulator + +No, but possible. + +=item Example + + package MyObject; + @ISA = qw(Class::Constructor); + MyObject->mk_constructor( Name => 'new' ); + +=back + + +=head2 Class::Classgen + +=over 4 + +=item Distribution + +CPAN. Last update 12/00. + +=item Usage + +Pre-processor run against declaration files. + +=item Mechanism + +Assembles and saves code file + +=item Instance Type + +Hash. + +=item Subclasses Cleanly + +Yes. (I think.) + +=item Standard Methods + +Constructor and various accessors. + +=item Extensible + +No. (I think.) + +=item Example + + header: + package MyObject; + variables: + $simple + @ordered + %mapping + $obj_ref + +=back + + +=head2 Class::Contract + +=over 4 + +=item Distribution + +CPAN. Last update 5/01. + +=item Usage + +Call function with declaration arguments + +=item Mechanism + +Generates and installs closures + +=item Instance Type + +Scalar reference with external data storage. + +=item Subclasses Cleanly + +Yes. + +=item Standard Methods + +Constructor and various accessors. + +=item Extensible + +Yes. (I think.) + +=item Comments + +Supports pre- and post-conditions, class invariants, and other +software engineering goodies. + +=item Example + + package MyObject; + use Class::Contract; + contract { + ctor 'new'; + attr 'simple' => SCALAR; + attr 'ordered' => ARRAY; + attr 'mapping' => HASH; + attr 'obj_ref' => 'FooObject'; + } + +=back + + +=head2 Class::Data::Inheritable + +=over 4 + +=item Distribution + +CPAN. Last update 4/00. + +=item Usage + +Inherit and call function with declaration arguments + +=item Mechanism + +Generates and installs closures + +=item Instance Type + +Class data, with inheritance. + +=item Subclasses Cleanly + +Yes, specifically. + +=item Standard Methods + +Scalar accessors. + +=item Extensible + +No. + +=item Example + +Usage is similar to Class::Accessor: + + package MyObject; + @ISA = qw(Class::Data::Inheritable); + MyObject->mk_classdata(qw( simple ordered mapping obj_ref )); + +=item Emulator + +Yes, Class::MakeMethods::Emulator::Inheritable, passes original test suite. + +=back + + +=head2 Class::Delegate + +=over 4 + +=item Distribution + +CPAN. Uploaded 12/0. + +=item Comments + +I have not yet reviewed this module in detail. + +=back + +=head2 Class::Delegation + +=over 4 + +=item Distribution + +CPAN. Uploaded 12/01. + +=item Comments + +I have not yet reviewed this module in detail. + +=back + +=head2 Class::Generate + +=over 4 + +=item Distribution + +CPAN. Last update 11/00. + +=item Usage + +Call function with declaration arguments + +=item Mechanism + +Assembles and evals code string, or saves code file. + +=item Instance Type + +Hash. + +=item Subclasses Cleanly + +Yes. + +=item Standard Methods + +Constructor and accessors (scalar, array, hash, object, object array, etc). + +=item Extensible + +Unknown. + +=item Comments + +Handles private/protected limitations, pre and post conditions, +assertions, and more. + +=item Example + +Usage is similar to Class::Struct: + + package MyObject; + use Class::Generate; + class MyObject => [ + simple => '$', + ordered => '@', + mapping => '%', + obj_ref => 'FooObject' + ]; + +=back + +=head2 Class::Hook + +=item Distribution + +CPAN. Uploaded 12/01. + +=item Comments + +I have not yet reviewed this module in detail. + + +=head2 Class::Holon + +=over 4 + +=item Distribution + +CPAN. Experimental/Alpha release 07/2001. + +=item Instance Type + +Hash, array, or flyweight-index. + +=item Subclasses Cleanly + +No. (I think.) + +=item Standard Methods + +Constructor and scalar accessors; flywieght objects also get scalar mutator methods. + +=item Extensible + +No. (I think.) + +=item Comments + +I'm not sure I understand the intent of this module; perhaps future versions will make this clearer.... + +=back + + +=head2 Class::MethodMaker + +=over 4 + +=item Distribution + +CPAN. Last update 1/01. + +=item Usage + +Import, or call function, with declaration arguments + +=item Mechanism + +Generates and installs closures + +=item Instance Type + +Hash, Static. + +=item Subclasses Cleanly + +Yes. + +=item Standard Methods + +Constructor and various accessors. + +=item Extensible + +Yes. + +=item Example + +Usage is similar to Class::MakeMethods: + + package MyObject; + use Class::MethodMaker ( + new => 'new', + get_set => 'simple', + list => 'ordered', + hash => 'mapping', + object => [ 'FooObject' => 'obj_ref' ], + ); + +=item Emulator + +Yes, Class::MakeMethods::Emulator::MethodMaker, passes original test suite. + +=back + + +=head2 Class::MakeMethods + +=over 4 + +=item Distribution + +CPAN. + +=item Usage + +Import, or call function, with declaration arguments; or if desired, make methods on-demand with Autoload, or declare subroutines with a special Attribute. + +=item Mechanism + +Generates and installs closures + +=item Instance Type + +Hash, Array, Scalar, Static, Class data, others. + +=item Subclasses Cleanly + +Yes. + +=item Standard Methods + +Constructor and various accessors. + +=item Extensible + +Yes. + +=item Example + +Usage is similar to Class::MethodMaker: + + package MyObject; + use Class::MakeMethods::Hash ( + new => 'new', + scalar => 'simple', + array => 'ordered', + hash => 'mapping', + object => [ 'obj_ref', { class=>'FooObject' } ], + ); + +=back + + +=head2 Class::Maker + +=over 4 + +=item Distribution + +CPAN. Last update 7/02. + +=item Usage + +Call function with declaration arguments. + +=item Mechanism + +Generates and installs closures (I think). + +=item Instance Type + +Hash (I think). + +=item Subclasses Cleanly + +Unknown. + +=item Standard Methods + +Constructor and various scalar and reference accessors. + +=item Extensible + +Unknown. + +=item Comments + +I haven't yet reviewed this module closely. + +=back + + +=head2 Class::SelfMethods + +=over 4 + +=item Distribution + +CPAN. Last update 2/00. + +=item Usage + +Inherit; methods created via AUTOLOAD + +=item Mechanism + +Generates and installs closures (I think) + +=item Instance Type + +Hash. + +=item Subclasses Cleanly + +Yes. + +=item Standard Methods + +Constructor and scalar/code accessors (see Comments). + +=item Extensible + +No. + +=item Comments + +Individual objects may be assigned a subroutine that will be called as a method on subsequent accesses. If an instance does not have a value for a given accessor, looks for a method defined with a leading underscore. + +=back + + +=head2 Class::Struct + +=over 4 + +=item Distribution + +Included in the standard Perl distribution. Replaces Class::Template. + +=item Usage + +Call function with declaration arguments + +=item Mechanism + +Assembles and evals code string + +=item Instance Type + +Hash or Array + +=item Subclasses Cleanly + +No. + +=item Standard Methods + +Constructor and various accessors. + +=item Extensible + +No. + + package MyObject; + use Class::Struct; + struct( + simple => '$', + ordered => '@', + mapping => '%', + obj_ref => 'FooObject' + ); + +=item Emulator + +Yes, Class::MakeMethods::Emulator::Struct. + +=back + + +=head2 Class::StructTemplate + +=over 4 + +=item Distribution + +CPAN. Last update 12/00. + +No documentation available. + +=item Usage + +Unknown. + +=item Mechanism + +Unknown. + +=back + + +=head2 Class::Template + +=over 4 + +=item Distribution + +CPAN. Out of date. + +=item Usage + +Call function with declaration arguments (I think) + +=item Mechanism + +Assembles and evals code string (I think) + +=item Instance Type + +Hash. + +=item Subclasses Cleanly + +Yes. (I think.) + +=item Standard Methods + +Constructor and various accessors. + +=item Extensible + +No. (I think.) + +=item Example + +Usage is similar to Class::Struct: + + package MyObject; + use Class::Template; + members MyObject { + simple => '$', + ordered => '@', + mapping => '%', + obj_ref => 'FooObject' + }; + +=back + + +=head2 Class::Virtual + +Generates methods that fail with a message indicating that they were not implemented by the subclass. (Cf. 'Template::Universal:croak -abstract'.) + +Also provides a list of abstract methods that have not been implemented by a subclass. + +=over 4 + +=item Distribution + +CPAN. Last update 3/01. + +=item Extensible + +Unknown. + +=item Mechanism + +Uses Class::Data::Inheritable and installs additional closures. + +=back + + +=head2 CodeGen::PerlBean + +=over 4 + +=item Distribution + +CPAN. + +=item Usage + +Call function with declaration arguments. + +=item Mechanism + +Generates and writes source code to a file. + +=item Instance Type + +Hash (I think). + +=item Subclasses Cleanly + +Unknown. + +=item Standard Methods + +Constructor and various scalar and reference accessors. + +=item Extensible + +Unknown. + +=item Comments + +I haven't yet reviewed this module closely. + +=back + + +=head2 HTML::Mason::MethodMaker + +=over 4 + +=item Distribution + +CPAN. + +=item Usage + +Package import with declaration arguments + +=item Mechanism + +Generates and installs closures + +=item Instance Type + +Hash. + +=item Standard Methods + +Scalar accessors. + +=item Extensible + +No. + +=item Example + + use HTML::Mason::MethodMaker ( + read_write => [ qw( simple ordered mapping obj_ref ) ] + ); + +=back + + +=head1 TO DO + +The following modules are relevant but have not yet been cataloged above. + +=head2 Attribute::Property + +=head2 Class::Accessor::Chained + +=head2 Class::Accessor::Lvalue + +=head2 Class::Accessor::Ref + +=head2 Class::AutoClass + +=head2 Class::Builder + +=head2 Class::Member + +=head2 Class::Trigger + + +=head1 SEE ALSO + +See L for general information about this distribution. + + +=head1 CREDITS AND COPYRIGHT + +=head2 Developed By + + M. Simon Cavalletto, simonm@cavalletto.org + Evolution Softworks, www.evoscript.org + +=head2 Copyright + +Copyright 2002 Matthew Simon Cavalletto. + +Portions copyright 2000, 2001 Evolution Online Systems, Inc. + +=head2 License + +You may use, modify, and distribute this document under the same terms as Perl. + +=cut diff --git a/lib/Class/MakeMethods/Docs/ToDo.pod b/lib/Class/MakeMethods/Docs/ToDo.pod new file mode 100644 index 0000000..312bdc0 --- /dev/null +++ b/lib/Class/MakeMethods/Docs/ToDo.pod @@ -0,0 +1,296 @@ +=head1 NAME + +Class::MakeMethods::Docs::ToDo - Ideas, problems, and suggestions + + +=head1 SYNOPSIS + +There are lots of things that could be done to improve this module. + + +=head1 DISTRIBUTION ISSUES + +Issues about the distribution and supporting files, rather than the code: + +=head2 Documentation + +=over 4 + +=item * + +Make sure that the documentation is broken up into appropriately-sized +chunks, and that people will know which section to look at. + +=item * + +As user questions arrive, add the answers as documentation points or examples. + +=item * + +Assemble annotated examples and tutorials, and either link to or distribute them. + +=item * + +Finish overhauling Template documentation. + +=item * + +Include Global and InsideOut uses in the EXAMPLES section + +=item * + +Template Internals: Finish documenting disk-based meta-method code-caching. + +=back + +=head2 Tests + +=over 4 + +=item * + +Use Devel::Coverage to measure test coverage, and fill in missing +cases. + +=item * + +Finish tests for Standard and Composite modules. + +=back + + +=head1 GENERAL ISSUES + +=over 4 + +=item * + +It does not appear to be possible to assign subroutine names to +closures within Perl. As a result, debugging output from Carp and +similar sources will show all generated methods as "ANON()" rather +than "YourClass::methodname()". + +UPDATE: There now seem to be fixes for this which should be integrated: See the new Sub::Name module and http://perlmonks.org/index.pl?node_id=304883 + +=item * + +For scalar methods (and others) it would be nice to have a simple +bounds-checking interface to approve or reject (with an exception) +new values that were passed in. + +As pointed out by Terrence Brannon, the right interface to +adopt is probably that of Attribute::Types: + + use Class::MakeMethods::Standard::Hash ( + 'scalar' => [ 'count' => { TYPE => 'INTEGER' } ], + 'scalar' => [ 'name' => { TYPE => qr/^[A-Z]\w*$/ } ], + 'scalar' => [ 'account' => { TYPE => &checksum_account_number } ] + ); + +=item * + +Improve use of _diagnostic hooks for debugging. Add various "(Q)" +debug diagnostics. + +=item * + +Finish building Inheritable array and object accessors. + +=item * + +Finish building Composite::* packages. + +=item * + +Resolve DESTROY-time issues with Standard::Inheritable, Composite::Inheritable, and Template::InsideOut. + +=item * + +Add slice and splice functionality to Standard::*:hash and Composite::*:hash. + +=back + + +=head1 TEMPLATE CLASSES + +=head2 Template::Generic + +=over 4 + +=item * + +Allow untyped object accesors if C attribute is not set. +(Suggested in Jan-01 NY Perl Seminar discussion.) + +=item * + +Standardize naming templates for array, hash, other method types. + +Deprecate verb_x methods? Or at last make them consistently available both ways. + +Make list methods consistent with hash_of_lists methods, in action, and +in name (x_verb). Also for others (e.g., set_ clear_ boolean) + +=item * + +Should default object template provide auto-create behavior on ->get()? + +=item * + +Generalize the "Generic:scalar -init_and_get" interface to support +memoizing values for other accessor types. + +=item * + +Consider adding hash each and array iterator methods, using a closure +to provide iteration. + +=item * + +Add support for tied arrays & scalars, a la tiedhash + +=item * + +Add string_multiple_index. + +=item * + +Extend index methods to support weak indexes with WeakRef. Perhaps +just have it accept a hash ref to use as the index, and then allow +people to pass in tied hashes? + +=item * + +Maybe make private or protected method croak if they were called by a +method_init method which was called by an outside package. + +Not entirely clear what the right semantics or security precautions are here... + +=back + + +=head2 Template::Generic Subclasses + +=over 4 + +=item * + +Finish building code_or_scalar meta-method. + +=item * + +Finish building Class::MakeMethods::ClassInherit subclass. + +Need to work out how to capture changes for non-scalar values. For +example, if a subclass inherits an array accessor and then pops +it, is there some way to provide them with copy-on-write? + +=item * + +Add enumerated string/number type. + +Provide helper methods with map of associated values (ex $o->port += 80 ... $o->port_readable eq 'HTTP' ). Cf. code for earlier +unpublished 'lookup' method type. + +=item * + +For StructBuiltin: + +Add -fatal flag to die if core func returns false / undef +Add call method to recall method with alternative arguments. +Add -nocall flag to not call core func on new. + +=item * + +Replace ClassName:static_hash_classname with Class:indexed_string. + +=back + + +=head2 Template Internals + +=over 4 + +=item * + +Figure out which modules, if any, should actually be using AutoLoader. +Probably just Template::Generic? + +=item * + +Give users a way to do meta-method code-caching in Perl library +hierarchy, rather than in /tmp/auto or other user-specified +directory.. + +Provide mechanism for pre-generating these at install time. + +Perhaps load these via do, rather than open/read/eval? + +Perhaps pre-generate expanded libs with all of the -imports resolved? + +=item * + +Support generating code files and loading them as needed. + +This would be similar to Class::Classgen, except that we'd do the +generation at run-time the first time it was required, rather than +in a separate pass. + +For example, given the following declaration: + + package Foo::Bar; + Class::MakeMethods::Template::Hash->import(-codecache=>'auto', scalar=>'foo'); + +We should be able to write out the following file: + + cat 'auto/Foo/Bar/methods-line-2.pl' + # NOTE: Generated for Foo::Bar by the Class::MakeMethods module. + # Changes made here will be lost when Foo::Bar is modified. + package Foo::Bar; + sub foo { + my $self = shift; + if ( scalar @_ ) { + $self->{'foo'} = shift(); + } + $self->{'foo'} + } + +Then on subsequent uses, we can just re-load the generated code: + + require "auto/Foo/Bar/methods-line-2.pl"; + +To do this, we need to: + +=over 4 + +=item * + +Provide an option to select this if desired; maybe ... +import('-cache' => 'auto/', ...)? + +=item * + +Figure out which directory we can/should write into. + +=item * + +Re-evaluate the textual code templates, without generating the +closures. Substitute in any _STATIC_ATTR_ values. Make other _ATTR_ +values point to some public lookup table or package scalar. + +=item * + +Notice if the source file (or Class::MakeMethods modules) has +been updated more recently than the generated file. + +=back + +=back + + +=head1 SEE ALSO + +See L for general information about this distribution. + +=cut diff --git a/lib/Class/MakeMethods/Emulator.pm b/lib/Class/MakeMethods/Emulator.pm new file mode 100644 index 0000000..96786da --- /dev/null +++ b/lib/Class/MakeMethods/Emulator.pm @@ -0,0 +1,165 @@ +package Class::MakeMethods::Emulator; + +$VERSION = 1.009; + +######################################################################## +### IMPORT BEHAVIOR: import(), _handle_namespace() +######################################################################## + +@EXPORT_OK = qw( namespace_capture namespace_release ); +sub import { + + if ( scalar @_ == 2 and $_[1] eq '-isasubclass' ) { + splice @_, 1, 1; + my $target_class = ( caller )[0]; + no strict; + push @{"$target_class\::ISA"}, $_[0]; + } + + if ( $_[0] eq __PACKAGE__ ) { + require Exporter and goto &Exporter::import # lazy Exporter + } +} + +sub _handle_namespace { + my $class = shift; + my $emulation_target = shift; + my $firstarg = shift or return; + my $take = shift || '-take_namespace'; + my $release = shift || '-release_namespace'; + + if ( $firstarg eq $take) { + Class::MakeMethods::Emulator::namespace_capture($class, $emulation_target); + return 1; + } elsif ( $firstarg eq $release) { + Class::MakeMethods::Emulator::namespace_release($class, $emulation_target); + return 1; + } +} + +######################################################################## +### NAMESPACE MUNGING: _namespace_capture(), _namespace_release() +######################################################################## + +sub namespace_capture { + my $source_package = shift; + my $target_package = shift; + + # warn "Mapping $source_package over $target_package \n"; + + my $source_file = "$source_package.pm"; + $source_file =~ s{::}{/}g; + + my $target_file = "$target_package.pm"; + $target_file =~ s{::}{/}g; + + my $temp_package = $source_package . '::Target::' . $target_package; + my $temp_file = "$temp_package.pm"; + $temp_file =~ s{::}{/}g; + + no strict; + unless ( ${$temp_package . "::TargetCaptured"} ++ ) { + *{$temp_package . "::"} = *{$target_package . "::"}; + $::INC{$temp_file} = $::INC{$target_file}; + } + *{$target_package . "::"} = *{$source_package . "::"}; + $::INC{$target_file} = $::INC{$source_file} +} + +sub namespace_release { + my $source_package = shift; + my $target_package = shift; + + my $target_file = "$target_package.pm"; + $target_file =~ s{::}{/}g; + + my $temp_package = $source_package . '::Target::' . $target_package; + my $temp_file = "$temp_package.pm"; + $temp_file =~ s{::}{/}g; + + no strict; + unless ( ${"${temp_package}::TargetCaptured"} ) { + Carp::croak("Can't _namespace_release: -take_namespace not called yet."); + } + *{$target_package . "::"} = *{$temp_package. "::"}; + $::INC{$target_file} = $::INC{$temp_file}; +} + +######################################################################## + +1; + +__END__ + + +=head1 NAME + +Class::MakeMethods::Emulator - Demonstrate class-generator equivalency + + +=head1 SYNOPSIS + + # Equivalent to use Class::Singleton; + use Class::MakeMethods::Emulator::Singleton; + + # Equivalent to use Class::Struct; + use Class::MakeMethods::Emulator::Struct; + struct ( ... ); + + # Equivalent to use Class::MethodMaker( ... ); + use Class::MakeMethods::Emulator::MethodMaker( ... ); + + # Equivalent to use base 'Class::Inheritable'; + use base 'Class::MakeMethods::Emulator::Inheritable'; + MyClass->mk_classdata( ... ); + + # Equivalent to use base 'Class::AccessorFast'; + use base 'Class::MakeMethods::Emulator::AccessorFast'; + MyClass->mk_accessors(qw(this that whatever)); + + # Equivalent to use accessors( ... ); + use Class::MakeMethods::Emulator::accessors( ... ); + + # Equivalent to use mcoder( ... ); + use Class::MakeMethods::Emulator::mcoder( ... ); + + +=head1 DESCRIPTION + +In several cases, Class::MakeMethods provides functionality closely +equivalent to that of an existing module, and it is simple to map +the existing module's interface to that of Class::MakeMethods. + +Class::MakeMethods::Emulator provides emulators for Class::MethodMaker, +Class::Accessor::Fast, Class::Data::Inheritable, Class::Singleton, +Class::Struct, accessors, and mcoder, each of which passes the +original module's test suite, usually requiring only the addition +of a a single line to each test, activating the emulation module. + +Beyond demonstrating compatibility, these emulators also generally +indicate the changes needed to switch to direct use of Class::MakeMethods +functionality, illustrate commonalities between the various modules, +and serve as a source for new ideas that can be integrated into +Class::MakeMethods. + + +=head1 SEE ALSO + +See L for general information about this distribution. + +See L, and L from CPAN. + +See L, and L from CPAN. + +See L, and L from CPAN. + +See L, and L from CPAN. + +See L, and L from CPAN. + +See L, and L from CPAN. + +See L, and L from CPAN. + +=cut + diff --git a/lib/Class/MakeMethods/Emulator/AccessorFast.pm b/lib/Class/MakeMethods/Emulator/AccessorFast.pm new file mode 100644 index 0000000..0f47e04 --- /dev/null +++ b/lib/Class/MakeMethods/Emulator/AccessorFast.pm @@ -0,0 +1,102 @@ +package Class::MakeMethods::Emulator::AccessorFast; + +use strict; +use Class::MakeMethods::Composite::Hash; +use Class::MakeMethods::Emulator '-isasubclass'; + +sub _emulator_target { 'Class::Accessor::Fast' } + +sub import { + my $class = shift; + $class->_handle_namespace( $class->_emulator_target, $_[0] ) and shift; +} + +######################################################################## + +sub mk_accessors { + Class::MakeMethods::Composite::Hash->make( + -TargetClass => (shift), + 'new' => { name => 'new', modifier => 'with_values' }, + 'scalar' => [ map { + $_, + "_${_}_accessor", { 'hash_key' => $_ } + } @_ ], + ); +} + +sub mk_ro_accessors { + Class::MakeMethods::Composite::Hash->make( + -TargetClass => (shift), + 'new' => { name => 'new', modifier => 'with_values' }, + 'scalar' => [ map { + $_, { permit => 'ro' }, + "_${_}_accessor", { 'hash_key' => $_, permit => 'ro' } + } @_ ], + ); +} + +sub mk_wo_accessors { + Class::MakeMethods::Composite::Hash->make( + -TargetClass => (shift), + 'new' => { name => 'new', modifier => 'with_values' }, + 'scalar' => [ map { + $_, { permit => 'wo' }, + "_${_}_accessor", { 'hash_key' => $_, permit => 'wo' } + } @_ ], + ); +} + +######################################################################## + +1; + +__END__ + + +=head1 NAME + +Class::MakeMethods::Emulator::AccessorFast - Emulate Class::Accessor::Fast + + +=head1 SYNOPSIS + + package Foo; + + use base qw(Class::MakeMethods::Emulator::AccessorFast); + Foo->mk_accessors(qw(this that whatever)); + + # Meanwhile, in a nearby piece of code! + # Emulator::AccessorFast provides new(). + my $foo = Foo->new; + + my $whatever = $foo->whatever; # gets $foo->{whatever} + $foo->this('likmi'); # sets $foo->{this} = 'likmi' + + +=head1 DESCRIPTION + +This module emulates the functionality of Class::Accessor::Fast, using Class::MakeMethods to generate similar methods. + +You may use it directly, as shown in the SYNOPSIS above, + +Furthermore, you may call C to alias the Class::Accessor::Fast namespace +to this package, and subsequent calls to the original package will +be transparently handled by this emulator. To remove the emulation +aliasing, call C. + +B This affects B subsequent uses of Class::Accessor::Fast +in your program, including those in other modules, and might cause +unexpected effects. + + +=head1 SEE ALSO + +See L for general information about this distribution. + +See L for more about this family of subclasses. + +See L for documentation of the original module. + +=cut diff --git a/lib/Class/MakeMethods/Emulator/Inheritable.pm b/lib/Class/MakeMethods/Emulator/Inheritable.pm new file mode 100644 index 0000000..90b0a91 --- /dev/null +++ b/lib/Class/MakeMethods/Emulator/Inheritable.pm @@ -0,0 +1,162 @@ +package Class::MakeMethods::Emulator::Inheritable; + +use strict; + +use Class::MakeMethods::Template::ClassInherit; +use Class::MakeMethods::Emulator qw( namespace_capture namespace_release ); + +my $emulation_target = 'Class::Data::Inheritable'; + +sub import { + my $mm_class = shift; + if ( scalar @_ and $_[0] =~ /^-take_namespace/ and shift) { + namespace_capture(__PACKAGE__, $emulation_target); + } elsif ( scalar @_ and $_[0] =~ /^-release_namespace/ and shift) { + namespace_release(__PACKAGE__, $emulation_target); + } + # The fallback should really be to NEXT::import. + $mm_class->SUPER::import( @_ ); +} + +######################################################################## + +sub mk_classdata { + my $declaredclass = shift; + my $attribute = shift; + Class::MakeMethods::Template::ClassInherit->make( + -TargetClass => $declaredclass, + 'scalar' => [ -interface => { '*'=>'get_set', '_*_accessor'=>'get_set' }, + $attribute ], + ); + if ( scalar @_ ) { + $declaredclass->$attribute( @_ ); + } +} + +######################################################################## + +1; + +__END__ + +=head1 NAME + +Class::MakeMethods::Emulator::Inheritable - Emulate Class::Inheritable + + +=head1 SYNOPSIS + + package Stuff; + use base qw(Class::MakeMethods::Emulator::Inheritable); + + # Set up DataFile as inheritable class data. + Stuff->mk_classdata('DataFile'); + + # Declare the location of the data file for this class. + Stuff->DataFile('/etc/stuff/data'); + + +=head1 DESCRIPTION + +This module is an adaptor that provides emulatation of Class::Data::Inheritable by invoking similiar functionality provided by Class::MakeMethods::ClassInherit. + +The public interface provided by Class::MakeMethods::Emulator::Inheritable is identical to that of Class::Data::Inheritable. + +Class::Data::Inheritable is for creating accessor/mutators to class +data. That is, if you want to store something about your class as a +whole (instead of about a single object). This data is then inherited +by your subclasses and can be overriden. + +=head1 USAGE + +As specified by L, clients should inherit from this module and then invoke the mk_classdata() method for each class method desired: + + Class->mk_classdata($data_accessor_name); + +This is a class method used to declare new class data accessors. A +new accessor will be created in the Class using the name from +$data_accessor_name. + + Class->mk_classdata($data_accessor_name, $initial_value); + +You may also pass a second argument to initialize the value. + +To facilitate overriding, mk_classdata creates an alias to the +accessor, _field_accessor(). So Suitcase() would have an alias +_Suitcase_accessor() that does the exact same thing as Suitcase(). +This is useful if you want to alter the behavior of a single accessor +yet still get the benefits of inheritable class data. For example. + + sub Suitcase { + my($self) = shift; + warn "Fashion tragedy" if @_ and $_[0] eq 'Plaid'; + + $self->_Suitcase_accessor(@_); + } + + +=head1 COMPATIBILITY + +Note that the internal implementation of Class::MakeMethods::ClassInherit does not match that of Class::Data::Inheritable. In particular, Class::Data::Inheritable installs new methods in subclasses when they first initialize their value, while + +=head1 EXAMPLE + +The example provided by L is equally applicable to this emulator. + + package Pere::Ubu; + use base qw(Class::MakeMethods::Emulator::Inheritable); + Pere::Ubu->mk_classdata('Suitcase'); + +will generate the method Suitcase() in the class Pere::Ubu. + +This new method can be used to get and set a piece of class data. + + Pere::Ubu->Suitcase('Red'); + $suitcase = Pere::Ubu->Suitcase; + +The interesting part happens when a class inherits from Pere::Ubu: + + package Raygun; + use base qw(Pere::Ubu); + + # Raygun's suitcase is Red. + $suitcase = Raygun->Suitcase; + +Raygun inherits its Suitcase class data from Pere::Ubu. + +Inheritance of class data works analgous to method inheritance. As +long as Raygun does not "override" its inherited class data (by using +Suitcase() to set a new value) it will continue to use whatever is set +in Pere::Ubu and inherit further changes: + + # Both Raygun's and Pere::Ubu's suitcases are now Blue + Pere::Ubu->Suitcase('Blue'); + +However, should Raygun decide to set its own Suitcase() it has now +"overridden" Pere::Ubu and is on its own, just like if it had +overriden a method: + + # Raygun has an orange suitcase, Pere::Ubu's is still Blue. + Raygun->Suitcase('Orange'); + +Now that Raygun has overridden Pere::Ubu futher changes by Pere::Ubu +no longer effect Raygun. + + # Raygun still has an orange suitcase, but Pere::Ubu is using Samsonite. + Pere::Ubu->Suitcase('Samsonite'); + + +=head1 SEE ALSO + +See L for general information about this distribution. + +See L for more about this family of subclasses. + +See L for documentation of the original module. + +See L for a discussion of class data in Perl. + +See L and L for inheritable data methods. + +=cut + diff --git a/lib/Class/MakeMethods/Emulator/MethodMaker.pm b/lib/Class/MakeMethods/Emulator/MethodMaker.pm new file mode 100644 index 0000000..4956ba3 --- /dev/null +++ b/lib/Class/MakeMethods/Emulator/MethodMaker.pm @@ -0,0 +1,676 @@ +package Class::MakeMethods::Emulator::MethodMaker; + +use Class::MakeMethods '-isasubclass'; +require Class::MakeMethods::Emulator; + +$VERSION = 1.03; + +use strict; + +=head1 NAME + +Class::MakeMethods::Emulator::MethodMaker - Emulate Class::MethodMaker + + +=head1 SYNOPSIS + + package MyObject; + use Class::MakeMethods::Emulator::MethodMaker( + new_with_init => 'new', + get_set => [ qw / foo bar baz / ]; + ); + + ... OR ... + + package MyObject; + use Class::MakeMethods::Emulator::MethodMaker '-take_namespace'; + use Class::MethodMaker ( + new_with_init => 'new', + get_set => [ qw / foo bar baz / ]; + ); + + +=head1 DESCRIPTION + +This module provides emulation of Class::MethodMaker, using the Class::MakeMethods framework. + +Although originally based on Class::MethodMaker, the calling convention +for Class::MakeMethods differs in a variety of ways; most notably, the names +given to various types of methods have been changed, and the format for +specifying method attributes has been standardized. This package uses +the aliasing capability provided by Class::MakeMethods, defining methods +that modify the declaration arguments as necessary and pass them off to +various subclasses of Class::MakeMethods. + + +=head1 COMPATIBILITY + +Full compatibility is maintained with version 1.03; some of the +changes in versions 1.04 through 1.10 are not yet included. + +The test suite from Class::MethodMaker version 1.10 is included +with this package, in the t/emulator_class_methodmaker/ directory. +The unsupported tests have names ending in ".todo". + +The tests are unchanged from those in the Class::MethodMaker +distribution, except for the substitution of +C in the place of +C. + +In cases where earlier distributions of Class::MethodMaker contained +a different version of a test, it is also included. (Note that +version 0.92's get_concat returned '' for empty values, but in +version 0.96 this was changed to undef; this emulator follows the +later behavior. To avoid "use of undefined value" warnings from +the 0.92 version of get_concat.t, that test has been modified by +appending a new flag after the name, C<'get_concat --noundef'>, +which restores the earlier behavior.) + + +=head1 USAGE + +There are several ways to call this emulation module: + +=over 4 + +=item * + +Direct Access + +Replace occurances in your code of C with C. + +=item * + +Install Emulation + +If you C, the Class::MethodMaker namespace will be aliased to this package, and calls to the original package will be transparently handled by this emulator. + +To remove the emulation aliasing, call C. + +B This affects B subsequent uses of Class::MethodMaker in your program, including those in other modules, and might cause unexpected effects. + +=item * + +The -sugar Option + +Passing '-sugar' as the first argument in a use or import call will cause the 'methods' package to be declared as an alias to this one. + +This allows you to write declarations in the following manner. + + use Class::MakeMethods::Emulator::MethodMaker '-sugar'; + + make methods + get_set => [ qw / foo bar baz / ], + list => [ qw / a b c / ]; + +B This feature is deprecated in Class::MethodMaker version 0.96 and later. + +=back + +=cut + +my $emulation_target = 'Class::MethodMaker'; + +sub import { + my $mm_class = shift; + + if ( scalar @_ and $_[0] =~ /^-take_namespace/ and shift ) { + Class::MakeMethods::Emulator::namespace_capture(__PACKAGE__, $emulation_target); + } elsif ( scalar @_ and $_[0] =~ /^-release_namespace/ and shift ) { + Class::MakeMethods::Emulator::namespace_release(__PACKAGE__, $emulation_target); + } + + if ( scalar @_ and $_[0] eq '-sugar' and shift ) { + Class::MakeMethods::Emulator::namespace_capture(__PACKAGE__, "methods"); + } + + $mm_class->make( @_ ) if ( scalar @_ ); +} + + +=head1 METHOD CATALOG + +B The documentation below is derived from version 1.02 of +Class::MethodMaker. Class::MakeMethods::Emulator::MethodMaker +provides support for all of the features and examples shown below, +with no changes required. + + +=head1 CONSTRUCTOR METHODS + +=head2 new + +Equivalent to Class::MakeMethods 'Template::Hash:new --with_values'. + +=cut + +sub new { return 'Template::Hash:new --with_values' } + + +=head2 new_with_init + +Equivalent to Class::MakeMethods 'Template::Hash:new --with_init'. + +=cut + +sub new_with_init { return 'Template::Hash:new --with_init' } + + +=head2 new_hash_init + +Equivalent to Class::MakeMethods 'Template::Hash:new --instance_with_methods'. + +=cut + +sub new_hash_init { return 'Template::Hash:new --instance_with_methods' } + + +=head2 new_with_args + +Equivalent to Class::MakeMethods 'Template::Hash:new --with_values'. + +=cut + +sub new_with_args { return 'Template::Hash:new --with_values' } + + +=head2 copy + +Equivalent to Class::MakeMethods 'Template::Hash:new --copy_with_values'. + +=cut + +sub copy { return 'Template::Hash:new --copy_with_values' } + + +=head1 SCALAR ACCESSORS + +=head2 get_set + +Basically equivalent to Class::MakeMethods 'Template::Hash:scalar', except that various arguments are intercepted and converted into the parallel Class::MakeMethods::Template interface declarations. + +=cut + +my $scalar_interface = { '*'=>'get_set', 'clear_*'=>'clear' }; + +sub get_set { + shift and return [ + ( ( $_[0] and $_[0] eq '-static' and shift ) ? 'Template::Static:scalar' + : 'Template::Hash:scalar' ), + '-interface' => $scalar_interface, + map { + ( ref($_) eq 'ARRAY' ) + ? ( '-interface'=>{ + ( $_->[0] ? ( $_->[0] => 'get_set' ) : () ), + ( $_->[1] ? ( $_->[1] => 'clear' ) : () ), + ( $_->[2] ? ( $_->[2] => 'get' ) : () ), + ( $_->[3] ? ( $_->[3] => 'set_return' ) : () ), + } ) + : ($_ eq '-compatibility') + ? ( '-interface', $scalar_interface ) + : ($_ eq '-noclear') + ? ( '-interface', 'default' ) + : ( /^-/ ? "-$_" : $_ ) + } @_ + ] +} + + +=head2 get_concat + +Equivalent to Class::MakeMethods 'Template::Hash:string' with a special interface declaration that provides the get_concat and clear behaviors. + +=cut + +my $get_concat_interface = { + '*'=>'get_concat', 'clear_*'=>'clear', + '-params'=>{ 'join' => '', 'return_value_undefined' => undef() } +}; + +my $old_get_concat_interface = { + '*'=>'get_concat', 'clear_*'=>'clear', + '-params'=>{ 'join' => '', 'return_value_undefined' => '' } +}; + +sub get_concat { + shift and return [ 'Template::Hash:string', '-interface', + ( $_[0] eq '--noundef' ? ( shift and $old_get_concat_interface ) + : $get_concat_interface ), @_ ] +} + +=head2 counter + +Equivalent to Class::MakeMethods 'Template::Hash:number --counter'. + +=cut + +sub counter { return 'Template::Hash:number --counter' } + + +=head1 OBJECT ACCESSORS + +Basically equivalent to Class::MakeMethods 'Template::Hash:object' with an declaration that provides the "delete_x" interface. Due to a difference in expected argument syntax, the incoming arguments are revised before being delegated to Template::Hash:object. + +=cut + +my $object_interface = { '*'=>'get_set_init', 'delete_*'=>'clear' }; + +sub object { + shift and return [ + 'Template::Hash:object', + '-interface' => $object_interface, + _object_args(@_) + ] +} + +sub _object_args { + my @meta_methods; + ! (@_ % 2) or Carp::croak("Odd number of arguments for object declaration"); + while ( scalar @_ ) { + my ($class, $list) = (shift(), shift()); + push @meta_methods, map { + (! ref $_) ? { name=> $_, class=>$class } + : { name=> $_->{'slot'}, class=>$class, + delegate=>( $_->{'forward'} || $_->{'comp_mthds'} ) } + } ( ( ref($list) eq 'ARRAY' ) ? @$list : ($list) ); + } + return @meta_methods; +} + + +=head2 object_list + +Basically equivalent to Class::MakeMethods 'Template::Hash:object_list' with an declaration that provides the relevant helper methods. Due to a difference in expected argument syntax, the incoming arguments are revised before being delegated to Template::Hash:object_list. + +=cut + +my $array_interface = { + '*'=>'get_push', + '*_set'=>'set_items', 'set_*'=>'set_items', + map( ('*_'.$_ => $_, $_.'_*' => $_ ), + qw( pop push unshift shift splice clear count ref index )), +}; + +sub object_list { + shift and return [ + 'Template::Hash:array_of_objects', + '-interface' => $array_interface, + _object_args(@_) + ]; +} + +=head2 forward + +Basically equivalent to Class::MakeMethods 'Template::Universal:forward_methods'. Due to a difference in expected argument syntax, the incoming arguments are revised before being delegated to Template::Universal:forward_methods. + + forward => [ comp => 'method1', comp2 => 'method2' ] + +Define pass-through methods for certain fields. The above defines that +method C will be handled by component C, whilst method +C will be handled by component C. + +=cut + +sub forward { + my $class = shift; + my @results; + while ( scalar @_ ) { + my ($comp, $method) = ( shift, shift ); + push @results, { name=> $method, target=> $comp }; + } + [ 'forward_methods', @results ] +} + + + +=head1 REFERENCE ACCESSORS + +=head2 list + +Equivalent to Class::MakeMethods 'Template::Hash:array' with a custom method naming interface. + +=cut + +sub list { + shift and return [ 'Template::Hash:array', '-interface' => $array_interface, @_ ]; +} + + +=head2 hash + +Equivalent to Class::MakeMethods 'Template::Hash:hash' with a custom method naming interface. + +=cut + +my $hash_interface = { + '*'=>'get_push', + '*s'=>'get_push', + 'add_*'=>'get_set_items', + 'add_*s'=>'get_set_items', + 'clear_*'=>'delete', + 'clear_*s'=>'delete', + map {'*_'.$_ => $_} qw(push set keys values exists delete tally clear), +}; + +sub hash { + shift and return [ 'Template::Hash:hash', '-interface' => $hash_interface, @_ ]; +} + + +=head2 tie_hash + +Equivalent to Class::MakeMethods 'Template::Hash:tiedhash' with a custom method naming interface. + +=cut + +sub tie_hash { + shift and return [ 'Template::Hash:tiedhash', '-interface' => $hash_interface, @_ ]; +} + +=head2 hash_of_lists + +Equivalent to Class::MakeMethods 'Template::Hash:hash_of_arrays', or if the -static flag is present, to 'Template::Static:hash_of_arrays'. + +=cut + +sub hash_of_lists { + shift and return ( $_[0] and $_[0] eq '-static' and shift ) + ? [ 'Template::Static:hash_of_arrays', @_ ] + : [ 'Template::Hash:hash_of_arrays', @_ ] +} + + +=head1 STATIC ACCESSORS + +=head2 static_get_set + +Equivalent to Class::MakeMethods 'Template::Static:scalar' with a custom method naming interface. + +=cut + +sub static_get_set { + shift and return [ 'Template::Static:scalar', '-interface', $scalar_interface, @_ ] +} + +=head2 static_list + +Equivalent to Class::MakeMethods 'Template::Static:array' with a custom method naming interface. + +=cut + +sub static_list { + shift and return [ 'Template::Static:array', '-interface' => $array_interface, @_ ]; +} + +=head2 static_hash + +Equivalent to Class::MakeMethods 'Template::Static:hash' with a custom method naming interface. + +=cut + +sub static_hash { + shift and return [ 'Template::Static:hash', '-interface' => $hash_interface, @_ ]; +} + + +=head1 GROUPED ACCESSORS + +=head2 boolean + +Equivalent to Class::MakeMethods 'Template::Static:bits' with a custom method naming interface. + +=cut + +my $bits_interface = { + '*'=>'get_set', 'set_*'=>'set_true', 'clear_*'=>'set_false', + 'bit_fields'=>'bit_names', 'bits'=>'bit_string', 'bit_dump'=>'bit_hash' +}; + +sub boolean { + shift and return [ 'Template::Hash:bits', '-interface' => $bits_interface, @_ ]; +} + + +=head2 grouped_fields + +Creates get/set methods like get_set but also defines a method which +returns a list of the slots in the group. + + use Class::MakeMethods::Emulator::MethodMaker + grouped_fields => [ + some_group => [ qw / field1 field2 field3 / ], + ]; + +Its argument list is parsed as a hash of group-name => field-list +pairs. Get-set methods are defined for all the fields and a method with +the name of the group is defined which returns the list of fields in the +group. + +=cut + +sub grouped_fields { + my ($class, %args) = @_; + my @methods; + foreach (keys %args) { + my @slots = @{ $args{$_} }; + push @methods, + $_, sub { @slots }, + $class->make( 'get_set', \@slots ); + } + return @methods; +} + +=head2 struct + +Equivalent to Class::MakeMethods 'Template::Hash::struct'. + +B This feature is included but not documented in Class::MethodMaker version 1. + + +=cut + +sub struct { return 'Template::Hash:struct' } + + +=head1 INDEXED ACCESSORS + +=head2 listed_attrib + +Equivalent to Class::MakeMethods 'Template::Flyweight:boolean_index' with a custom method naming interface. + +=cut + +sub listed_attrib { + shift and return [ 'Template::Flyweight:boolean_index', '-interface' => { + '*'=>'get_set', 'set_*'=>'set_true', 'clear_*'=>'set_false', + '*_objects'=>'find_true', }, @_ ] +} + + +=head2 key_attrib + +Equivalent to Class::MakeMethods 'Template::Hash:string_index'. + +=cut + +sub key_attrib { return 'Template::Hash:string_index' } + +=head2 key_with_create + +Equivalent to Class::MakeMethods 'Template::Hash:string_index --find_or_new'. + +=cut + +sub key_with_create { return 'Template::Hash:string_index --find_or_new'} + + +=head1 CODE ACCESSORS + +=head2 code + +Equivalent to Class::MakeMethods 'Template::Hash:code'. + +=cut + +sub code { return 'Template::Hash:code' } + + +=head2 method + +Equivalent to Class::MakeMethods 'Template::Hash:code --method'. + +=cut + +sub method { return 'Template::Hash:code --method' } + + +=head2 abstract + +Equivalent to Class::MakeMethods 'Template::Universal:croak --abstract'. + +=cut + +sub abstract { return 'Template::Universal:croak --abstract' } + + +=head1 ARRAY CONSTRUCTOR AND ACCESSORS + +=head2 builtin_class (EXPERIMENTAL) + +Equivalent to Class::MakeMethods 'Template::StructBuiltin:builtin_isa' with a modified argument order. + +=cut + +sub builtin_class { + shift and return [ 'Template::StructBuiltin:builtin_isa', + '-new_function'=>(shift), @{(shift)} ] +} + +=head1 CONVERSION + +If you wish to convert your code from use of the Class::MethodMaker emulator to direct use of Class::MakeMethods, you will need to adjust the arguments specified in your C or C calls. + +Often this is simply a matter of replacing the names of aliased method-types listed below with the new equivalents. + +For example, suppose that you code contained the following declaration: + + use Class::MethodMaker ( + counter => [ 'foo' ] + ); + +Consulting the listings below you can find that C is an alias for C and you could thus revise your declaration to read: + + use Class::MakeMethods ( + 'Hash:number --counter' => [ 'foo' ] + ); + +However, note that those methods marked "(with custom interface)" below have a different default naming convention for helper methods in Class::MakeMethods, and you will need to either supply a similar interface or alter your module's calling interface. + +Also note that the C, C, and C method types, marked "(with modified arguments)" below, require their arguments to be specified differently. + +See L for more information about the default interfaces of these method types. + + +=head2 Hash methods + +The following equivalencies are declared for old meta-method names that are now handled by the Hash implementation: + + new 'Template::Hash:new --with_values' + new_with_init 'Template::Hash:new --with_init' + new_hash_init 'Template::Hash:new --instance_with_methods' + copy 'Template::Hash:copy' + get_set 'Template::Hash:scalar' (with custom interfaces) + counter 'Template::Hash:number --counter' + get_concat 'Template::Hash:string --get_concat' (with custom interface) + boolean 'Template::Hash:bits' (with custom interface) + list 'Template::Hash:array' (with custom interface) + struct 'Template::Hash:struct' + hash 'Template::Hash:hash' (with custom interface) + tie_hash 'Template::Hash:tiedhash' (with custom interface) + hash_of_lists 'Template::Hash:hash_of_arrays' + code 'Template::Hash:code' + method 'Template::Hash:code --method' + object 'Template::Hash:object' (with custom interface and modified arguments) + object_list 'Template::Hash:array_of_objects' (with custom interface and modified arguments) + key_attrib 'Template::Hash:string_index' + key_with_create 'Template::Hash:string_index --find_or_new' + +=head2 Static methods + +The following equivalencies are declared for old meta-method names +that are now handled by the Static implementation: + + static_get_set 'Template::Static:scalar' (with custom interface) + static_hash 'Template::Static:hash' (with custom interface) + +=head2 Flyweight method + +The following equivalency is declared for the one old meta-method name +that us now handled by the Flyweight implementation: + + listed_attrib 'Template::Flyweight:boolean_index' + +=head2 Struct methods + +The following equivalencies are declared for old meta-method names +that are now handled by the Struct implementation: + + builtin_class 'Template::Struct:builtin_isa' + +=head2 Universal methods + +The following equivalencies are declared for old meta-method names +that are now handled by the Universal implementation: + + abstract 'Template::Universal:croak --abstract' + forward 'Template::Universal:forward_methods' (with modified arguments) + + +=head1 EXTENDING + +In order to enable third-party subclasses of MethodMaker to run under this emulator, several aliases or stub replacements are provided for internal Class::MethodMaker methods which have been eliminated or renamed. + +=over 4 + +=item * + +install_methods - now simply return the desired methods + +=item * + +find_target_class - now passed in as the target_class attribute + +=item * + +ima_method_maker - no longer supported; use target_class instead + +=back + +=cut + +sub find_target_class { (shift)->_context('TargetClass') } +sub get_target_class { (shift)->_context('TargetClass') } +sub install_methods { (shift)->_install_methods(@_) } +sub ima_method_maker { 1 } + + +=head1 BUGS + +This module aims to provide a 100% compatible drop-in replacement for Class::MethodMaker; if you detect a difference when using this emulation, please inform the author. + + +=head1 SEE ALSO + +See L for general information about this distribution. + +See L for more about this family of subclasses. + +See L for more information about the original module. + +A good introduction to Class::MethodMaker is provided by pages 222-234 of I, by Damian Conway (Manning, 1999). + + http://www.browsebooks.com/Conway/ + +=cut + +1; diff --git a/lib/Class/MakeMethods/Emulator/Singleton.pm b/lib/Class/MakeMethods/Emulator/Singleton.pm new file mode 100644 index 0000000..c47ad9e --- /dev/null +++ b/lib/Class/MakeMethods/Emulator/Singleton.pm @@ -0,0 +1,85 @@ +package Class::MakeMethods::Emulator::Singleton; + +use strict; +require Class::MakeMethods::Emulator; + +my $emulation_target = 'Class::Singleton'; + +sub import { + my $mm_class = shift; + if ( scalar @_ and $_[0] =~ /^-take_namespace/ and shift) { + Class::MakeMethods::Emulator::namespace_capture(__PACKAGE__, $emulation_target); + } elsif ( scalar @_ and $_[0] =~ /^-release_namespace/ and shift) { + Class::MakeMethods::Emulator::namespace_release(__PACKAGE__, $emulation_target); + } + # The fallback should really be to NEXT::import. + $mm_class->SUPER::import( @_ ); +} + +######################################################################## + +use Class::MakeMethods ( + 'Template::Hash:new --with_values' => '_new_instance', + 'Template::ClassVar:instance --get_init' => [ 'instance', + {new_method=>'_new_instance', variable=>'_instance'} ] +); + +######################################################################## + +1; + +__END__ + +=head1 NAME + +Class::MakeMethods::Emulator::Singleton - Emulate Class::Singleton + + +=head1 SYNOPSIS + + use Class::MakeMethods::Emulator::Singleton; + + # returns a new instance + my $one = Class::MakeMethods::Emulator::Singleton->instance(); + + # returns same instance + my $two = Class::MakeMethods::Emulator::Singleton->instance(); + + +=head1 COMPATIBILITY + +This module emulates the functionality of Class::Singleton, using Class::MakeMethods to generate similar methods. + +You may use it directly, as shown in the SYNOPSIS above, + +Furthermore, you may call C to alias the Class::Singleton namespace to this package, and subsequent calls to the original package will be transparently handled by this emulator. To remove the emulation aliasing, call C. + +B This affects B subsequent uses of Class::Singleton in your program, including those in other modules, and might cause unexpected effects. + + +=head1 DESCRIPTION + +A Singleton describes an object class that can have only one instance +in any system. An example of a Singleton might be a print spooler +or system registry. This module implements a Singleton class from +which other classes can be derived. By itself, the Class::Singleton +module does very little other than manage the instantiation of a +single object. In deriving a class from Class::Singleton, your +module will inherit the Singleton instantiation method and can +implement whatever specific functionality is required. + + +=head1 SEE ALSO + +See L for general information about this distribution. + +See L for more about this family of subclasses. + +See L for documentation of the original module. + +For a description and discussion of the Singleton class, see +"Design Patterns", Gamma et al, Addison-Wesley, 1995, ISBN 0-201-63361-2. + +See L and L for documentation of the created methods. + +=cut diff --git a/lib/Class/MakeMethods/Emulator/Struct.pm b/lib/Class/MakeMethods/Emulator/Struct.pm new file mode 100644 index 0000000..4dad355 --- /dev/null +++ b/lib/Class/MakeMethods/Emulator/Struct.pm @@ -0,0 +1,154 @@ +package Class::MakeMethods::Emulator::Struct; + +use strict; + +use Class::MakeMethods; + +use vars qw(@ISA @EXPORT); +require Exporter; +push @ISA, qw(Exporter); +@EXPORT = qw(struct); + +sub import { + my $self = shift; + + if ( @_ == 0 ) { + $self->export_to_level( 1, $self, @EXPORT ); + } elsif ( @_ == 1 ) { + $self->export_to_level( 1, $self, @_ ); + } else { + &struct; + } +} + +######################################################################## + +my %type_map = ( + '$' => 'scalar', + '@' => 'array', + '%' => 'hash', + '_' => 'object', +); + +sub struct { + my ($class, @decls); + my $base_type = ref $_[1] ; + if ( $base_type eq 'HASH' ) { + $base_type = 'Standard::Hash'; + $class = shift; + @decls = %{shift()}; + _usage_error() if @_; + } + elsif ( $base_type eq 'ARRAY' ) { + $base_type = 'Standard::Array'; + $class = shift; + @decls = @{shift()}; + _usage_error() if @_; + } + else { + $base_type = 'Standard::Array'; + $class = (caller())[0]; + @decls = @_; + } + _usage_error() if @decls % 2 == 1; + + my @rewrite; + while ( scalar @decls ) { + my ($name, $type) = splice(@decls, 0, 2); + push @rewrite, $type_map{$type} + ? ( $type_map{$type} => { 'name'=>$name, auto_init=>1 } ) + : ( $type_map{'_'} => { 'name'=>$name, 'class'=>$type, auto_init=>1 } ); + } + Class::MakeMethods->make( + -TargetClass => $class, + -MakerClass => $base_type, + "new" => 'new', + @rewrite + ); +} + +sub _usage_error { + require Carp; + Carp::confess "struct usage error"; +} + +######################################################################## + +1; + +__END__ + +=head1 NAME + +Class::MakeMethods::Emulator::Struct - Emulate Class::Struct + + +=head1 SYNOPSIS + + use Class::MakeMethods::Emulator::Struct; + + struct ( + simple => '$', + ordered => '@', + mapping => '%', + obj_ref => 'FooObject' + ); + + +=head1 DESCRIPTION + +This module emulates the functionality of Class::Struct by munging the provided field-declaration arguments to match those expected by Class::MakeMethods. + +It supports the same four types of accessors, the choice of array-based or hash-based objects, and the choice of installing methods in the current package or a specified target. + + +=head1 EXAMPLE + +The below three declarations create equivalent methods for a simple hash-based class with a constructor and four accessors. + + use Class::Struct; + struct ( + simple => '$', + ordered => '@', + mapping => '%', + obj_ref => 'FooObject' + ); + + use Class::MakeMethods::Emulator::Struct; + struct ( + simple => '$', + ordered => '@', + mapping => '%', + obj_ref => 'FooObject' + ); + + use Class::MakeMethods ( + -MakerClass => 'Standard::Array', + 'new' => 'new', + 'scalar' => 'simple', + 'array -auto_init 1' => 'ordered', + 'hash -auto_init 1' => 'mapping', + 'object -auto_init 1' => '-class FooObject obj_ref' + ); + +=head1 COMPATIBILITY + +This module aims to offer a "95% compatible" drop-in replacement for the core Class::Struct module for purposes of comparison and code migration. + +The C test for the core Class::Struct module is included with this package. The test is unchanged except for the a direct substitution of this emulator's name in the place of the core module. + +However, there are numerous internal differences between the methods generated by the original Class::Struct and this emulator, and some existing code may not work correctly without modification. + + +=head1 SEE ALSO + +See L for general information about this distribution. + +See L for more about this family of subclasses. + +See L for documentation of the original module. + +See L and L for documentation of the created methods. + +=cut + diff --git a/lib/Class/MakeMethods/Emulator/accessors.pm b/lib/Class/MakeMethods/Emulator/accessors.pm new file mode 100644 index 0000000..69c3bb8 --- /dev/null +++ b/lib/Class/MakeMethods/Emulator/accessors.pm @@ -0,0 +1,122 @@ +package Class::MakeMethods::Emulator::accessors; + +$VERSION = '0.02'; + +use Class::MakeMethods::Emulator '-isasubclass'; +use Class::MakeMethods::Template::Hash '-isasubclass'; + +sub _emulator_target { 'accessors' } +sub _accessor_type { 'scalar --get_set_chain' } + +sub import { + my $class = shift; + + $class->_handle_namespace( $class->_emulator_target, $_[0] ) and shift; + + foreach ( @_ ) { + die "invalid accessor - $_" unless ( /\A[a-z]\w+\z/i and + $_ ne 'DESTROY' and $_ ne 'AUTOLOAD' ) + } + + $class->make($class->_accessor_type => [@_]); +} + +######################################################################## + +package Class::MakeMethods::Emulator::accessors::chained; +@ISA = 'Class::MakeMethods::Emulator::accessors'; +$INC{'Class/MakeMethods/Emulator/accessors/chained.pm'} = + $INC{'Class/MakeMethods/Emulator/accessors.pm'}; + +sub _emulator_target { 'accessors::chained' } +sub _accessor_type { 'scalar --get_set_chain' } + +######################################################################## + +package Class::MakeMethods::Emulator::accessors::classic; +@ISA = 'Class::MakeMethods::Emulator::accessors'; +$INC{'Class/MakeMethods/Emulator/accessors/classic.pm'} = + $INC{'Class/MakeMethods/Emulator/accessors.pm'}; + +sub _emulator_target { 'accessors::classic' } +sub _accessor_type { 'scalar' } + +######################################################################## + +1; + +__END__ + + +=head1 NAME + +Class::MakeMethods::Emulator::accessors - Emulate the accessors module + + +=head1 SYNOPSIS + + package Foo; + use Class::MakeMethods::Emulator::accessors qw( foo bar baz ); + + my $obj = bless {}, 'Foo'; + + # generates chaining accessors: + $obj->foo( 'hello ' ) + ->bar( 'world' ) + ->baz( "!\n" ); + + print $obj->foo, $obj->bar, $obj->baz; + +This module also defines subpackages for the classic and chaining subclasses: + + package Bar; + use Class::MakeMethods::Emulator::accessors; + use Class::MakeMethods::Emulator::accessors::classic qw( foo bar baz ); + + my $obj = bless {}, 'Bar'; + + # always return the current value, even on set: + $obj->foo( 'hello ' ) if $obj->bar( 'world' ); + + print $obj->foo, $obj->bar, $obj->baz( "!\n" ); + + +=head1 DESCRIPTION + +This module emulates the functionality of the accessors module, using +Class::MakeMethods to generate similar methods. + +In particular, the following lines are equivalent: + + use accessors 'foo'; + use Class::MakeMethods::Template::Hash 'scalar --get_set_chain' => 'foo'; + + use accessors::chained 'foo'; + use Class::MakeMethods::Template::Hash 'scalar --get_set_chain' => 'foo'; + + use accessors::classic 'foo'; + use Class::MakeMethods::Template::Hash 'scalar' => 'foo'; + +You may use this module directly, as shown in the SYNOPSIS above, + +Furthermore, you may call C to alias the accessors namespace to this package, +and subsequent calls to the original package will be transparently +handled by this emulator. To remove the emulation aliasing, call +C. +The same mechanism is also available for the classic and chained subclasses. + +B This affects B subsequent uses of the accessors module in +your program, including those in other modules, and might cause +unexpected effects. + + +=head1 SEE ALSO + +See L for general information about this distribution. + +See L for more about this family of subclasses. + +See L for documentation of the original module. + +=cut diff --git a/lib/Class/MakeMethods/Emulator/mcoder.pm b/lib/Class/MakeMethods/Emulator/mcoder.pm new file mode 100644 index 0000000..84ef034 --- /dev/null +++ b/lib/Class/MakeMethods/Emulator/mcoder.pm @@ -0,0 +1,116 @@ +package Class::MakeMethods::Emulator::mcoder; + +$VERSION = '0.05'; + +use Class::MakeMethods::Emulator '-isasubclass'; +use Class::MakeMethods::Template '-isasubclass'; + +######################################################################## + +sub import { + my $class = shift; + ( my $target = $class ) =~ s/^Class::MakeMethods::Emulator:://; + $class->_handle_namespace( $target, $_[0] ) and shift; + $class->make( @_ ) if ( scalar @_ ); +} + + +sub new { 'Template::Hash::new --with_values' } +sub proxy { 'Template::Universal:forward_methods -target' } +sub generic { { '-import' => { 'Template::Hash:scalar' => '*' } } } +sub get { { interface => { default => { '*' =>'get' } } } } +sub set { { interface => { default => { 'set_*' =>'set' } } } } +sub undef { { interface => { default => { 'undef_*' =>'clear' } } } } +sub delete { { interface => { default => { 'delete_*'=>'hash_delete' } } } } +sub bool_set { { interface => { default => { 'set_*' =>'set_value' } }, + '-import' => { 'Template::Hash:boolean' => '*' } } } +sub bool_unset { { interface => { default => { 'unset_*' =>'clear' } } } } +sub calculated { { interface => { default => { '*' =>'get_init' } }, + params => { init_method=>'_calculate_*' } } } + +######################################################################## + +foreach my $type ( qw( new get set proxy calculated ) ) { + $INC{"Class/MakeMethods/Emulator/mcoder/$type.pm"} = + $INC{"mcoder/$type.pm"} = __FILE__; + *{__PACKAGE__ . "::${type}::import"} = sub { + (shift) and (__PACKAGE__)->make( $type => [ @_ ] ) + }; +} + +######################################################################## + +1; + +__END__ + +package Class::MakeMethods::Emulator::mcoder::get; +@ISA = 'Class::MakeMethods::Emulator::mcoder'; +$INC{"Class/MakeMethods/Emulator/mcoder/get.pm"} = __FILE__; +sub import { goto &Class::MakeMethods::Emulator::mcoder::sub_import } + +package Class::MakeMethods::Emulator::mcoder::set; +@ISA = 'Class::MakeMethods::Emulator::mcoder'; +$INC{"Class/MakeMethods/Emulator/mcoder/set.pm"} = __FILE__; +sub import { goto &Class::MakeMethods::Emulator::mcoder::sub_import } + +package Class::MakeMethods::Emulator::mcoder::proxy; +@ISA = 'Class::MakeMethods::Emulator::mcoder'; +$INC{"Class/MakeMethods/Emulator/mcoder/proxy.pm"} = __FILE__; +sub import { goto &Class::MakeMethods::Emulator::mcoder::sub_import } + + +1; + +__END__ + +=head1 NAME + +Class::MakeMethods::Emulator::mcoder - Emulate the mcoder module + + +=head1 SYNOPSIS + + package MyClass; + + use Class::MakeMethods::Emulator::mcoder + [qw(get set)] => [qw(color sound height)], + proxy => [qw(runner run walk stop)], + calculated => weight; + + sub _calculate_weight { shift->ask_weight } + + +=head1 DESCRIPTION + +This module emulates the functionality of the mcoder module, using +Class::MakeMethods to generate similar methods. + +For example, the following lines are equivalent: + + use mcoder 'get' => 'foo'; + use mcoder::get 'foo'; + use Class::MakeMethods::Template::Hash 'scalar --get' => 'foo'; + +You may use this module directly, as shown in the SYNOPSIS above, +or you may call C to alias the mcoder namespace to this package, +and subsequent calls to the original package will be transparently +handled by this emulator. To remove the emulation aliasing, call +C. +The same mechanism is also available for the "sugar" subclasses. + +B This affects B subsequent uses of the mcoder module in +your program, including those in other modules, and might cause +unexpected effects. + + +=head1 SEE ALSO + +See L for general information about this distribution. + +See L for more about this family of subclasses. + +See L< mcoder> for documentation of the original module. + +=cut diff --git a/lib/Class/MakeMethods/Evaled.pm b/lib/Class/MakeMethods/Evaled.pm new file mode 100644 index 0000000..233c9c6 --- /dev/null +++ b/lib/Class/MakeMethods/Evaled.pm @@ -0,0 +1,97 @@ +=head1 NAME + +Class::MakeMethods::Evaled - Make methods with simple string evals + + +=head1 SYNOPSIS + + package MyObject; + use Class::MakeMethods::Evaled::Hash ( + new => 'new', + scalar => [ 'foo', 'bar' ], + array => 'my_list', + hash => 'my_index', + ); + + +=head1 DESCRIPTION + +This document describes the various subclasses of Class::MakeMethods +included under the Evaled::* namespace, and the method types each +one provides. + +The Evaled subclasses generate methods using a simple string templating mechanism and basic string evals. + + +=head2 Calling Conventions + +When you C this package, the method names you provide +as arguments cause subroutines to be generated and installed in +your module. + +See L for more information. + +=head2 Declaration Syntax + +To declare methods, pass in pairs of a method-type name followed +by one or more method names. + +Valid method-type names for this package are listed in L<"METHOD +GENERATOR TYPES">. + +See L and L for more information. + +=cut + +package Class::MakeMethods::Evaled; + +$VERSION = 1.000; +use strict; +use Carp; + +use Class::MakeMethods::Standard '-isasubclass'; +use Class::MakeMethods::Utility::TextBuilder 'text_builder'; + +######################################################################## + +=head2 About Evaled Methods + + +=cut + +sub evaled_methods { + my $class = shift; + my $template = shift; + my $package = $Class::MakeMethods::CONTEXT{TargetClass}; + my @declarations = $class->_get_declarations( @_ ); + my @code_chunks; + foreach my $method ( @declarations ) { + my $code = $template; + $code =~ s/__(\w+?)__/$method->{lc $1}/eg; + + # my $code = text_builder( $template, { + # '__NAME__' => $method->{name}, + # '__METHOD__{}' => $method, + # '__CONTEXT__{}' => $Class::MakeMethods::CONTEXT, + # } ); + + push @code_chunks, $code; + } + my $code = join( "\n", "package $package;", @code_chunks, "1;" ); + eval $code; + $@ and Class::MakeMethods::_diagnostic('inst_eval_syntax', 'from eval', $@, $code); + return; +} + +######################################################################## + +=head1 SEE ALSO + +See L for general information about this distribution. + +For distribution, installation, support, copyright and license +information, see L. + +=cut + +1; diff --git a/lib/Class/MakeMethods/Evaled/Hash.pm b/lib/Class/MakeMethods/Evaled/Hash.pm new file mode 100644 index 0000000..e306c76 --- /dev/null +++ b/lib/Class/MakeMethods/Evaled/Hash.pm @@ -0,0 +1,349 @@ +=head1 NAME + +Class::MakeMethods::Evaled::Hash - Typical hash methods + + +=head1 SYNOPSIS + + package MyObject; + use Class::MakeMethods::Evaled::Hash ( + new => 'new', + scalar => [ 'foo', 'bar' ], + array => 'my_list', + hash => 'my_index', + ); + ... + + # Constructor + my $obj = MyObject->new( foo => 'Foozle' ); + + # Scalar Accessor + print $obj->foo(); + + $obj->bar('Barbados'); + print $obj->bar(); + + # Array accessor + $obj->my_list(0 => 'Foozle', 1 => 'Bang!'); + print $obj->my_list(1); + + # Hash accessor + $obj->my_index('broccoli' => 'Blah!', 'foo' => 'Fiddle'); + print $obj->my_index('foo'); + + +=head1 DESCRIPTION + +The Evaled::Hash subclass of MakeMethods provides a simple constructor and accessors for blessed-hash object instances. + +=head2 Calling Conventions + +When you C this package, the method names you provide +as arguments cause subroutines to be generated and installed in +your module. + +See L for a summary, or L for full details. + +=head2 Declaration Syntax + +To declare methods, pass in pairs of a method-type name followed +by one or more method names. Valid method-type names for this +package are listed in L<"METHOD GENERATOR TYPES">. + +See L for more +syntax information. + + +=cut + +package Class::MakeMethods::Evaled::Hash; + +$VERSION = 1.000; +use strict; +use Class::MakeMethods::Evaled '-isasubclass'; + +######################################################################## + +=head1 METHOD GENERATOR TYPES + +=head2 new - Constructor + +For each method name passed, returns a subroutine with the following characteristics: + +=over 4 + +=item * + +If called as a class method, makes a new hash and blesses it into that class. + +=item * + +If called on a hash-based instance, makes a copy of it and blesses the copy into the same class as the original instance. + +=item * + +If passed a list of key-value pairs, appends them to the new hash. These arguments override any copied values, and later arguments with the same name will override earlier ones. + +=item * + +Returns the new instance. + +=back + +Sample declaration and usage: + + package MyObject; + use Class::MakeMethods::Evaled::Hash ( + new => 'new', + ); + ... + + # Bare constructor + my $empty = MyObject->new(); + + # Constructor with initial values + my $obj = MyObject->new( foo => 'Foozle', bar => 'Barbados' ); + + # Copy with overriding value + my $copy = $obj->new( bar => 'Bob' ); + +=cut + +sub new { + (shift)->evaled_methods( q{ + sub __NAME__ { + my $callee = shift; + if ( ref $callee ) { + bless { %$callee, @_ }, ref $callee; + } else { + bless { @_ }, $callee; + } + } + }, @_ ) +} + +######################################################################## + +=head2 scalar - Instance Accessor + +For each method name passed, uses a closure to generate a subroutine with the following characteristics: + +=over 4 + +=item * + +Must be called on a hash-based instance. + +=item * + +Uses the method name as a hash key to access the related value for each instance. + +=item * + +If called without any arguments returns the current value. + +=item * + +If called with an argument, stores that as the value, and returns it, + +=back + +Sample declaration and usage: + + package MyObject; + use Class::MakeMethods::Evaled::Hash ( + scalar => 'foo', + ); + ... + + # Store value + $obj->foo('Foozle'); + + # Retrieve value + print $obj->foo; + +=cut + +sub scalar { + (shift)->evaled_methods( q{ + sub __NAME__ { + my $self = shift; + if ( scalar @_ ) { + $self->{'__NAME__'} = shift; + } else { + $self->{'__NAME__'}; + } + } + }, @_ ) +} + +######################################################################## + +=head2 array - Instance Ref Accessor + +For each method name passed, uses a closure to generate a subroutine with the following characteristics: + +=over 4 + +=item * + +Must be called on a hash-based instance. + +=item * + +Uses the method name as a hash key to access the related value for each instance. + +=item * + +The value for each instance will be a reference to an array (or undef). + +=item * + +If called without any arguments, returns the current array-ref value (or undef). + +=item * + +If called with one argument, uses that argument as an index to retrieve from the referenced array, and returns that value (or undef). + +=item * + +If called with a list of index-value pairs, stores the value at the given index in the referenced array. If the instance's value was previously undefined, a new array is autovivified. The current value in each position will be overwritten, and later arguments with the same index will override earlier ones. Returns the current array-ref value. + +=back + +Sample declaration and usage: + + package MyObject; + use Class::MakeMethods::Evaled::Hash ( + array => 'bar', + ); + ... + + # Set values by position + $obj->bar(0 => 'Foozle', 1 => 'Bang!'); + + # Positions may be overwritten, and in any order + $obj->bar(2 => 'And Mash', 1 => 'Blah!'); + + # Retrieve value by position + print $obj->bar(1); + + # Direct access to referenced array + print scalar @{ $obj->bar() }; + + # Reset the array contents to empty + @{ $obj->bar() } = (); + +=cut + +sub array { + (shift)->evaled_methods( q{ + sub __NAME__ { + my $self = shift; + if ( scalar(@_) == 0 ) { + return $self->{'__NAME__'}; + } elsif ( scalar(@_) == 1 ) { + $self->{'__NAME__'}->[ shift() ]; + } elsif ( scalar(@_) % 2 ) { + Carp::croak "Odd number of items in assigment to __NAME__"; + } else { + while ( scalar(@_) ) { + my $key = shift(); + $self->{'__NAME__'}->[ $key ] = shift(); + } + return $self->{'__NAME__'}; + } + } + }, @_ ) +} + +######################################################################## + +=head2 hash - Instance Ref Accessor + +For each method name passed, uses a closure to generate a subroutine with the following characteristics: + +=over 4 + +=item * + +Must be called on a hash-based instance. + +=item * + +Uses the method name as a hash key to access the related value for each instance. + +=item * + +The value for each instance will be a reference to a hash (or undef). + +=item * + +If called without any arguments, returns the current hash-ref value (or undef). + +=item * + +If called with one argument, uses that argument as an index to retrieve from the referenced hash, and returns that value (or undef). + +=item * + +If called with a list of key-value pairs, stores the value under the given key in the referenced hash. If the instance's value was previously undefined, a new hash is autovivified. The current value under each key will be overwritten, and later arguments with the same key will override earlier ones. Returns the current hash-ref value. + +=back + +Sample declaration and usage: + + package MyObject; + use Class::MakeMethods::Evaled::Hash ( + hash => 'baz', + ); + ... + + # Set values by key + $obj->baz('foo' => 'Foozle', 'bar' => 'Bang!'); + + # Values may be overwritten, and in any order + $obj->baz('broccoli' => 'Blah!', 'foo' => 'Fiddle'); + + # Retrieve value by key + print $obj->baz('foo'); + + # Direct access to referenced hash + print keys %{ $obj->baz() }; + + # Reset the hash contents to empty + @{ $obj->baz() } = (); + +=cut + +sub hash { + (shift)->evaled_methods( q{ + sub __NAME__ { + my $self = shift; + if ( scalar(@_) == 0 ) { + return $self->{'__NAME__'}; + } elsif ( scalar(@_) == 1 ) { + $self->{'__NAME__'}->{ shift() }; + } elsif ( scalar(@_) % 2 ) { + Carp::croak "Odd number of items in assigment to '__NAME__'"; + } else { + while ( scalar(@_) ) { + $self->{'__NAME__'}->{ shift() } = shift(); + } + return $self->{'__NAME__'}; + } + } + }, @_ ) +} + +######################################################################## + +=head1 SEE ALSO + +See L for general information about this distribution. + +See L for more about this family of subclasses. + +=cut + +1; diff --git a/lib/Class/MakeMethods/Standard.pm b/lib/Class/MakeMethods/Standard.pm new file mode 100644 index 0000000..024049e --- /dev/null +++ b/lib/Class/MakeMethods/Standard.pm @@ -0,0 +1,68 @@ +=head1 NAME + +Class::MakeMethods::Standard - Make common object accessors + + +=head1 SYNOPSIS + + package MyObject; + use Class::MakeMethods::Standard::Hash ( + new => 'new', + scalar => [ 'foo', 'bar' ], + array => 'my_list', + hash => 'my_index', + ); + + +=head1 DESCRIPTION + +This document describes the various subclasses of Class::MakeMethods +included under the Standard::* namespace, and the method types each +one provides. + +The Standard subclasses provide a parameterized set of method-generation +implementations. + +Subroutines are generated as closures bound to a hash containing +the method name and (optionally) additional parameters. + + +=head1 USAGE AND SYNTAX + +When you C a subclass of this package, the method declarations +you provide as arguments cause subroutines to be generated and +installed in your module. You can also omit the arguments to C +and instead make methods at runtime by passing the declarations to +a subsequent call to C. + +You may include any number of declarations in each call to C +or C. If methods with the same name already exist, earlier +calls to C or C win over later ones, but within each +call, later declarations superceed earlier ones. + +You can install methods in a different package by passing +C<-target_class =E I> as your first arguments to C +or C. + +See L for more details. + +=cut + +package Class::MakeMethods::Standard; + +$VERSION = 1.000; +use strict; +use Class::MakeMethods '-isasubclass'; + +######################################################################## + +=head1 SEE ALSO + +See L for general information about this distribution. + +For distribution, installation, support, copyright and license +information, see L. + +=cut + +1; diff --git a/lib/Class/MakeMethods/Standard/Array.pm b/lib/Class/MakeMethods/Standard/Array.pm new file mode 100644 index 0000000..52c1b0b --- /dev/null +++ b/lib/Class/MakeMethods/Standard/Array.pm @@ -0,0 +1,555 @@ +=head1 NAME + +Class::MakeMethods::Standard::Array - Methods for Array objects + +=head1 SYNOPSIS + + package MyObject; + use Class::MakeMethods::Standard::Array ( + new => 'new', + scalar => [ 'foo', 'bar' ], + array => 'my_list', + hash => 'my_index', + ); + ... + + my $obj = MyObject->new( foo => 'Foozle' ); + print $obj->foo(); + + $obj->bar('Barbados'); + print $obj->bar(); + + $obj->my_list(0 => 'Foozle', 1 => 'Bang!'); + print $obj->my_list(1); + + $obj->my_index('broccoli' => 'Blah!', 'foo' => 'Fiddle'); + print $obj->my_index('foo'); + +=head1 DESCRIPTION + +The Standard::Array suclass of MakeMethods provides a basic +constructor and accessors for blessed-array object instances. + +=head2 Calling Conventions + +When you C this package, the method names you provide +as arguments cause subroutines to be generated and installed in +your module. + +See L for more information. + +=head2 Declaration Syntax + +To declare methods, pass in pairs of a method-type name followed +by one or more method names. + +Valid method-type names for this package are listed in L<"METHOD +GENERATOR TYPES">. + +See L and L for more information. + +=cut + +package Class::MakeMethods::Standard::Array; + +$VERSION = 1.000; +use strict; +use Class::MakeMethods::Standard '-isasubclass'; +use Class::MakeMethods::Utility::ArraySplicer 'array_splicer'; + +######################################################################## + +=head2 Positional Accessors and %FIELDS + +Each accessor method is assigned the next available array index at +which to store its value. + +The mapping between method names and array positions is stored in +a hash named %FIELDS in the declaring package. When a package +declares its first positional accessor, its %FIELDS are initialized +by searching its inheritance tree. + +B: Subclassing packages that use positional accessors is +somewhat fragile, since you may end up with two distinct methods assigned to the same position. Specific cases to avoid are: + +=over 4 + +=item * + +If you inherit from more than one class with positional accessors, +the positions used by the two sets of methods will overlap. + +=item * + +If your superclass adds additional positional accessors after you +declare your first, they will overlap yours. + +=back + +=cut + +sub _array_index { + my $class = shift; + my $name = shift; + no strict; + local $^W = 0; + if ( ! scalar %{$class . "::FIELDS"} ) { + my @classes = @{$class . "::ISA"}; + my @fields; + while ( @classes ) { + my $superclass = shift @classes; + if ( scalar %{$superclass . "::FIELDS"} ) { + push @fields, %{$superclass . "::FIELDS"}; + } else { + unshift @classes, @{$superclass . "::ISA"} + } + } + %{$class . "::FIELDS"} = @fields + } + my $field_hash = \%{$class . "::FIELDS"}; + $field_hash->{$name} or $field_hash->{$name} = scalar keys %$field_hash +} + +######################################################################## + +=head1 METHOD GENERATOR TYPES + +=head2 new - Constructor + +For each method name passed, returns a subroutine with the following characteristics: + +=over 4 + +=item * + +Has a reference to a sample item to copy. This defaults to a reference to an empty array, but you may override this with the C<'defaults' => I> method parameter. + +=item * + +If called as a class method, makes a new array containing values from the sample item, and blesses it into that class. + +=item * + +If called on an array-based instance, makes a copy of it and blesses the copy into the same class as the original instance. + +=item * + +If passed a list of method-value pairs, calls each named method with the associated value as an argument. + +=item * + +Returns the new instance. + +=back + +Sample declaration and usage: + + package MyObject; + use Class::MakeMethods::Standard::Array ( + new => 'new', + ); + ... + + # Bare constructor + my $empty = MyObject->new(); + + # Constructor with initial sequence of method calls + my $obj = MyObject->new( foo => 'Foozle', bar => 'Barbados' ); + + # Copy with overriding sequence of method calls + my $copy = $obj->new( bar => 'Bob' ); + +=cut + +sub new { + my $class = shift; + map { + my $name = $_->{name}; + my $defaults = $_->{defaults} || []; + $name => sub { + my $callee = shift; + my $self = ref($callee) ? bless( [@$callee], ref($callee) ) + : bless( [@$defaults], $callee ); + while ( scalar @_ ) { + my $method = shift; + $self->$method( shift ); + } + return $self; + } + } $class->_get_declarations(@_) +} + +######################################################################## + +=head2 scalar - Instance Accessor + +For each method name passed, uses a closure to generate a subroutine with the following characteristics: + +=over 4 + +=item * + +Must be called on an array-based instance. + +=item * + +Determines the array position associated with the method name, and uses that as an index into each instance to access the related value. This defaults to the next available slot in %FIELDS, but you may override this with the C<'array_index' => I> method parameter, or by pre-filling the contents of %FIELDS. + +=item * + +If called without any arguments returns the current value (or undef). + +=item * + +If called with an argument, stores that as the value, and returns it, + +=back + +Sample declaration and usage: + + package MyObject; + use Class::MakeMethods::Standard::Array ( + scalar => 'foo', + ); + ... + + # Store value + $obj->foo('Foozle'); + + # Retrieve value + print $obj->foo; + +=cut + +sub scalar { + my $class = shift; + map { + my $name = $_->{name}; + my $index = $_->{array_index} || + _array_index( $class->_context('TargetClass'), $name ); + $name => sub { + my $self = shift; + if ( scalar @_ ) { + $self->[$index] = shift; + } else { + $self->[$index]; + } + } + } $class->_get_declarations(@_) +} + +######################################################################## + +=head2 array - Instance Ref Accessor + +For each method name passed, uses a closure to generate a subroutine with the following characteristics: + +=over 4 + +=item * + +Must be called on an array-based instance. + +=item * + +Determines the array position associated with the method name, and uses that as an index into each instance to access the related value. This defaults to the next available slot in %FIELDS, but you may override this with the C<'array_index' => I> method parameter, or by pre-filling the contents of %FIELDS. + +=item * + +The value for each instance will be a reference to an array (or undef). + +=item * + +If called without any arguments, returns the current array-ref value (or undef). + +=item * + +If called with a single non-ref argument, uses that argument as an index to retrieve from the referenced array, and returns that value (or undef). + +=item * + +If called with a single array ref argument, uses that list to return a slice of the referenced array. + +=item * + +If called with a list of argument pairs, each with a non-ref index and an associated value, stores the value at the given index in the referenced array. If the instance's value was previously undefined, a new array is autovivified. The current value in each position will be overwritten, and later arguments with the same index will override earlier ones. Returns the current array-ref value. + +=item * + +If called with a list of argument pairs, each with the first item being a reference to an array of up to two numbers, loops over each pair and uses those numbers to splice the value array. + +The first controlling number is the position at which the splice will begin. Zero will start before the first item in the list. Negative numbers count backwards from the end of the array. + +The second number is the number of items to be removed from the list. If it is omitted, or undefined, or zero, no items are removed. If it is a positive integer, that many items will be returned. + +If both numbers are omitted, or are both undefined, they default to containing the entire value array. + +If the second argument is undef, no values will be inserted; if it is a non-reference value, that one value will be inserted; if it is an array-ref, its values will be copied. + +The method returns the items that removed from the array, if any. + +=back + +Sample declaration and usage: + + package MyObject; + use Class::MakeMethods::Standard::Array ( + array => 'bar', + ); + ... + + # Clear and set contents of list + print $obj->bar([ 'Spume', 'Frost' ] ); + + # Set values by position + $obj->bar(0 => 'Foozle', 1 => 'Bang!'); + + # Positions may be overwritten, and in any order + $obj->bar(2 => 'And Mash', 1 => 'Blah!'); + + # Retrieve value by position + print $obj->bar(1); + + # Direct access to referenced array + print scalar @{ $obj->bar() }; + +There are also calling conventions for slice and splice operations: + + # Retrieve slice of values by position + print join(', ', $obj->bar( undef, [0, 2] ) ); + + # Insert an item at position in the array + $obj->bar([3], 'Potatoes' ); + + # Remove 1 item from position 3 in the array + $obj->bar([3, 1], undef ); + + # Set a new value at position 2, and return the old value + print $obj->bar([2, 1], 'Froth' ); + +=cut + +sub array { + my $class = shift; + map { + my $name = $_->{name}; + my $index = $_->{array_index} || + _array_index( $class->_context('TargetClass'), $name ); + my $init = $_->{auto_init}; + $name => sub { + my $self = shift; + if ( scalar(@_) == 0 ) { + if ( $init and ! defined $self->[$index] ) { + $self->[$index] = []; + } + ( ! $self->[$index] ) ? () : + ( wantarray ) ? @{ $self->[$index] } : + $self->[$index] + } elsif ( scalar(@_) == 1 and ref $_[0] eq 'ARRAY' ) { + $self->[$index] = [ @{ $_[0] } ]; + ( ! $self->[$index] ) ? () : + ( wantarray ) ? @{ $self->[$index] } : + $self->[$index] + } else { + $self->[$index] ||= []; + array_splicer( $self->[$index], @_ ); + } + } + } $class->_get_declarations(@_) +} + +######################################################################## + +=head2 hash - Instance Ref Accessor + +For each method name passed, uses a closure to generate a subroutine with the following characteristics: + +=over 4 + +=item * + +Must be called on an array-based instance. + +=item * + +Determines the array position associated with the method name, and uses that as an index into each instance to access the related value. This defaults to the next available slot in %FIELDS, but you may override this with the C<'array_index' => I> method parameter, or by pre-filling the contents of %FIELDS. + + +=item * + +The value for each instance will be a reference to a hash (or undef). + +=item * + +If called without any arguments, returns the contents of the hash in list context, or a hash reference in scalar context (or undef). + +=item * + +If called with one argument, uses that argument as an index to retrieve from the referenced hash, and returns that value (or undef). If the single argument is an array ref, then a slice of the referenced hash is returned. + +=item * + +If called with a list of key-value pairs, stores the value under the given key in the referenced hash. If the instance's value was previously undefined, a new hash is autovivified. The current value under each key will be overwritten, and later arguments with the same key will override earlier ones. Returns the contents of the hash in list context, or a hash reference in scalar context. + +=back + +Sample declaration and usage: + + package MyObject; + use Class::MakeMethods::Standard::Array ( + hash => 'baz', + ); + ... + + # Set values by key + $obj->baz('foo' => 'Foozle', 'bar' => 'Bang!'); + + # Values may be overwritten, and in any order + $obj->baz('broccoli' => 'Blah!', 'foo' => 'Fiddle'); + + # Retrieve value by key + print $obj->baz('foo'); + + # Retrive slice of values by position + print join(', ', $obj->baz( ['foo', 'bar'] ) ); + + # Direct access to referenced hash + print keys %{ $obj->baz() }; + + # Reset the hash contents to empty + @{ $obj->baz() } = (); + +=cut + +sub hash { + my $class = shift; + map { + my $name = $_->{name}; + my $index = $_->{array_index} || + _array_index( $class->_context('TargetClass'), $name ); + my $init = $_->{auto_init}; + $name => sub { + my $self = shift; + if ( scalar(@_) == 0 ) { + if ( $init and ! defined $self->[$index] ) { + $self->[$index] = {}; + } + ( ! $self->[$index] ) ? () : + ( wantarray ) ? %{ $self->[$index] } : + $self->[$index] + } elsif ( scalar(@_) == 1 ) { + if ( ref($_[0]) eq 'HASH' ) { + my $hash = shift; + $self->[$index] = { %$hash }; + } elsif ( ref($_[0]) eq 'ARRAY' ) { + return @{$self->[$index]}{ @{$_[0]} } + } else { + return $self->[$index]->{ $_[0] } + } + } elsif ( scalar(@_) % 2 ) { + Carp::croak "Odd number of items in assigment to $name"; + } else { + while ( scalar(@_) ) { + my $key = shift(); + $self->[$index]->{ $key } = shift(); + } + ( wantarray ) ? %{ $self->[$index] } : + $self->[$index] + } + } + } $class->_get_declarations(@_) +} + +######################################################################## + +=head2 object - Instance Ref Accessor + +For each method name passed, uses a closure to generate a subroutine with the following characteristics: + +=over 4 + +=item * + +Must be called on an array-based instance. + +=item * + +Determines the array position associated with the method name, and uses that as an index into each instance to access the related value. This defaults to the next available slot in %FIELDS, but you may override this with the C<'array_index' => I> method parameter, or by pre-filling the contents of %FIELDS. + +=item * + +The value for each instance will be a reference to an object (or undef). + +=item * + +If called without any arguments returns the current value. + +=item * + +If called with an argument, stores that as the value, and returns it, + +=back + +Sample declaration and usage: + + package MyObject; + use Class::MakeMethods::Standard::Hash ( + object => 'foo', + ); + ... + + # Store value + $obj->foo( Foozle->new() ); + + # Retrieve value + print $obj->foo; + +=cut + +sub object { + my $class = shift; + map { + my $name = $_->{name}; + my $index = $_->{array_index} || + _array_index( $class->_context('TargetClass'), $name ); + my $class = $_->{class}; + my $init = $_->{auto_init}; + if ( $init and ! $class ) { + Carp::croak("Use of auto_init requires value for class parameter") + } + my $new_method = $_->{new_method} || 'new'; + $name => sub { + my $self = shift; + if ( scalar @_ ) { + my $value = shift; + if ( $class and ! UNIVERSAL::isa( $value, $class ) ) { + Carp::croak "Wrong argument type ('$value') in assigment to $name"; + } + $self->[$index] = $value; + } else { + if ( $init and ! defined $self->[$index] ) { + $self->[$index] = $class->$new_method(); + } else { + $self->[$index]; + } + } + } + } $class->_get_declarations(@_) +} + +######################################################################## + +=head1 SEE ALSO + +See L for general information about this distribution. + +See L for more about this family of subclasses. + +See L for equivalent functionality +based on blessed hashes. If your module will be extensively +subclassed, consider switching to Standard::Hash to avoid the +subclassing concerns described above. + +=cut + +1; diff --git a/lib/Class/MakeMethods/Standard/Global.pm b/lib/Class/MakeMethods/Standard/Global.pm new file mode 100644 index 0000000..9c1e48d --- /dev/null +++ b/lib/Class/MakeMethods/Standard/Global.pm @@ -0,0 +1,405 @@ +=head1 NAME + +Class::MakeMethods::Standard::Global - Global data + +=head1 SYNOPSIS + + package MyClass; + use Class::MakeMethods::Standard::Global ( + scalar => [ 'foo' ], + array => [ 'my_list' ], + hash => [ 'my_index' ], + ); + ... + + MyClass->foo( 'Foozle' ); + print MyClass->foo(); + + print MyClass->new(...)->foo(); # same value for any instance + print MySubclass->foo(); # ... and for any subclass + + MyClass->my_list(0 => 'Foozle', 1 => 'Bang!'); + print MyClass->my_list(1); + + MyClass->my_index('broccoli' => 'Blah!', 'foo' => 'Fiddle'); + print MyClass->my_index('foo'); + + +=head1 DESCRIPTION + +The Standard::Global suclass of MakeMethods provides basic accessors for shared data. + +=head2 Calling Conventions + +When you C this package, the method names you provide +as arguments cause subroutines to be generated and installed in +your module. + +See L for more information. + +=head2 Declaration Syntax + +To declare methods, pass in pairs of a method-type name followed +by one or more method names. + +Valid method-type names for this package are listed in L<"METHOD +GENERATOR TYPES">. + +See L and L for more information. + +=cut + +package Class::MakeMethods::Standard::Global; + +$VERSION = 1.000; +use strict; +use Class::MakeMethods::Standard '-isasubclass'; +use Class::MakeMethods::Utility::ArraySplicer 'array_splicer'; + +######################################################################## + +=head1 METHOD GENERATOR TYPES + +=head2 scalar - Global Accessor + +For each method name passed, uses a closure to generate a subroutine with the following characteristics: + +=over 4 + +=item * + +May be called as a class method, or on any instance or subclass, and behaves identically regardless of what it was called on. + +=item * + +If called without any arguments returns the current value. + +=item * + +If called with an argument, stores that as the value, and returns it, + +=back + +Sample declaration and usage: + + package MyClass; + use Class::MakeMethods::Standard::Global ( + scalar => 'foo', + ); + ... + + # Store value + MyClass->foo('Foozle'); + + # Retrieve value + print MyClass->foo; + +=cut + +sub scalar { + map { + my $name = $_->{name}; + my $data; + $name => sub { + my $self = shift; + if ( scalar(@_) == 0 ) { + $data; + } else { + $data = shift; + } + } + } (shift)->_get_declarations(@_) +} + +######################################################################## + +=head2 array - Global Ref Accessor + +For each method name passed, uses a closure to generate a subroutine with the following characteristics: + +=over 4 + +=item * + +May be called as a class method, or on any instance or subclass, and behaves identically regardless of what it was called on. + +=item * + +The global value will be a reference to an array (or undef). + +=item * + +If called without any arguments, returns the current array-ref value (or undef). + + +=item * + +If called with a single non-ref argument, uses that argument as an index to retrieve from the referenced array, and returns that value (or undef). + +=item * + +If called with a single array ref argument, uses that list to return a slice of the referenced array. + +=item * + +If called with a list of argument pairs, each with a non-ref index and an associated value, stores the value at the given index in the referenced array. If the global value was previously undefined, a new array is autovivified. The current value in each position will be overwritten, and later arguments with the same index will override earlier ones. Returns the current array-ref value. + +=item * + +If called with a list of argument pairs, each with the first item being a reference to an array of up to two numbers, loops over each pair and uses those numbers to splice the value array. + +The first controlling number is the position at which the splice will begin. Zero will start before the first item in the list. Negative numbers count backwards from the end of the array. + +The second number is the number of items to be removed from the list. If it is omitted, or undefined, or zero, no items are removed. If it is a positive integer, that many items will be returned. + +If both numbers are omitted, or are both undefined, they default to containing the entire value array. + +If the second argument is undef, no values will be inserted; if it is a non-reference value, that one value will be inserted; if it is an array-ref, its values will be copied. + +The method returns the items that removed from the array, if any. + +=back + +Sample declaration and usage: + + package MyClass; + use Class::MakeMethods::Standard::Global ( + array => 'bar', + ); + ... + + # Clear and set contents of list + print MyClass->bar([ 'Spume', 'Frost' ] ); + + # Set values by position + MyClass->bar(0 => 'Foozle', 1 => 'Bang!'); + + # Positions may be overwritten, and in any order + MyClass->bar(2 => 'And Mash', 1 => 'Blah!'); + + # Retrieve value by position + print MyClass->bar(1); + + # Direct access to referenced array + print scalar @{ MyClass->bar() }; + +There are also calling conventions for slice and splice operations: + + # Retrieve slice of values by position + print join(', ', MyClass->bar( undef, [0, 2] ) ); + + # Insert an item at position in the array + MyClass->bar([3], 'Potatoes' ); + + # Remove 1 item from position 3 in the array + MyClass->bar([3, 1], undef ); + + # Set a new value at position 2, and return the old value + print MyClass->bar([2, 1], 'Froth' ); + +=cut + +sub array { + map { + my $name = $_->{name}; + my $data; + my $init = $_->{auto_init}; + $name => sub { + my $self = shift; + if ( scalar(@_) == 0 ) { + if ( $init and ! defined $data ) { + $data = []; + } + ! $data ? () : wantarray ? @$data : $data; + } elsif ( scalar(@_) == 1 and ref $_[0] eq 'ARRAY' ) { + $data = [ @{ $_[0] } ]; + wantarray ? @$data : $data; + } else { + $data ||= []; + return array_splicer( $data, @_ ); + } + } + } (shift)->_get_declarations(@_) +} + +######################################################################## + +=head2 hash - Global Ref Accessor + +For each method name passed, uses a closure to generate a subroutine with the following characteristics: + +=over 4 + +=item * + +May be called as a class method, or on any instance or subclass, and behaves identically regardless of what it was called on. + +=item * + +The global value will be a reference to a hash (or undef). + +=item * + +If called without any arguments, returns the contents of the hash in list context, or a hash reference in scalar context (or undef). + +=item * + +If called with one non-ref argument, uses that argument as an index to retrieve from the referenced hash, and returns that value (or undef). + +=item * + +If called with one array-ref argument, uses the contents of that array to retrieve a slice of the referenced hash. + +=item * + +If called with one hash-ref argument, sets the contents of the referenced hash to match that provided. + +=item * + +If called with a list of key-value pairs, stores the value under the given key in the referenced hash. If the global value was previously undefined, a new hash is autovivified. The current value under each key will be overwritten, and later arguments with the same key will override earlier ones. Returns the contents of the hash in list context, or a hash reference in scalar context. + +=back + +Sample declaration and usage: + + package MyClass; + use Class::MakeMethods::Standard::Global ( + hash => 'baz', + ); + ... + + # Set values by key + MyClass->baz('foo' => 'Foozle', 'bar' => 'Bang!'); + + # Values may be overwritten, and in any order + MyClass->baz('broccoli' => 'Blah!', 'foo' => 'Fiddle'); + + # Retrieve value by key + print MyClass->baz('foo'); + + # Retrive slice of values by position + print join(', ', MyClass->baz( ['foo', 'bar'] ) ); + + # Direct access to referenced hash + print keys %{ MyClass->baz() }; + + # Reset the hash contents to empty + @{ MyClass->baz() } = (); + +=cut + +sub hash { + map { + my $name = $_->{name}; + my $data; + my $init = $_->{auto_init}; + $name => sub { + my $self = shift; + if ( scalar(@_) == 0 ) { + if ( $init and ! defined $data ) { + $data = {}; + } + ! $data ? () : wantarray ? %$data : $data + } elsif ( scalar(@_) == 1 ) { + if ( ref($_[0]) eq 'HASH' ) { + my $hash = shift; + $data = { %$hash }; + } elsif ( ref($_[0]) eq 'ARRAY' ) { + return @{$data}{ @{$_[0]} } + } else { + return $data->{ $_[0] } + } + } elsif ( scalar(@_) % 2 ) { + Carp::croak "Odd number of items in assigment to $name"; + } else { + while ( scalar(@_) ) { + my $key = shift(); + $data->{ $key } = shift(); + } + wantarray ? %$data : $data; + } + } + } (shift)->_get_declarations(@_) +} + +######################################################################## + +=head2 object - Global Ref Accessor + +For each method name passed, uses a closure to generate a subroutine with the following characteristics: + +=over 4 + +=item * + +May be called as a class method, or on any instance or subclass, and behaves identically regardless of what it was called on. + +=item * + +The global value will be a reference to an object (or undef). + +=item * + +If called without any arguments returns the current value. + +=item * + +If called with an argument, stores that as the value, and returns it, + +=back + +Sample declaration and usage: + + package MyClass; + use Class::MakeMethods::Standard::Global ( + object => 'foo', + ); + ... + + # Store value + MyClass->foo( Foozle->new() ); + + # Retrieve value + print MyClass->foo; + +=cut + +sub object { + map { + my $name = $_->{name}; + my $data; + my $class = $_->{class}; + my $init = $_->{auto_init}; + if ( $init and ! $class ) { + Carp::croak("Use of auto_init requires value for class parameter") + } + my $new_method = $_->{new_method} || 'new'; + $name => sub { + my $self = shift; + if ( scalar @_ ) { + my $value = shift; + if ( $class and ! UNIVERSAL::isa( $value, $class ) ) { + Carp::croak "Wrong argument type ('$value') in assigment to $name"; + } + $data = $value; + } else { + if ( $init and ! defined $data ) { + $data = $class->$new_method(); + } + $data; + } + } + } (shift)->_get_declarations(@_) +} + +######################################################################## + +=head1 SEE ALSO + +See L for general information about this distribution. + +See L for more about this family of subclasses. + +=cut + +1; diff --git a/lib/Class/MakeMethods/Standard/Hash.pm b/lib/Class/MakeMethods/Standard/Hash.pm new file mode 100644 index 0000000..ba4f65b --- /dev/null +++ b/lib/Class/MakeMethods/Standard/Hash.pm @@ -0,0 +1,501 @@ +=head1 NAME + +Class::MakeMethods::Standard::Hash - Standard hash methods + +=head1 SYNOPSIS + + package MyObject; + use Class::MakeMethods::Standard::Hash ( + new => 'new', + scalar => [ 'foo', 'bar' ], + array => 'my_list', + hash => 'my_index', + ); + ... + + my $obj = MyObject->new( foo => 'Foozle' ); + print $obj->foo(); + + $obj->bar('Barbados'); + print $obj->bar(); + + $obj->my_list(0 => 'Foozle', 1 => 'Bang!'); + print $obj->my_list(1); + + $obj->my_index('broccoli' => 'Blah!', 'foo' => 'Fiddle'); + print $obj->my_index('foo'); + +=head1 DESCRIPTION + +The Standard::Hash suclass of MakeMethods provides a basic constructor and accessors for blessed-hash object instances. + +=head2 Calling Conventions + +When you C this package, the method names you provide +as arguments cause subroutines to be generated and installed in +your module. + +See L for more information. + +=head2 Declaration Syntax + +To declare methods, pass in pairs of a method-type name followed +by one or more method names. + +Valid method-type names for this package are listed in L<"METHOD +GENERATOR TYPES">. + +See L and L for more information. + +=cut + +package Class::MakeMethods::Standard::Hash; + +$VERSION = 1.000; +use strict; +use Class::MakeMethods::Standard '-isasubclass'; +use Class::MakeMethods::Utility::ArraySplicer 'array_splicer'; + +######################################################################## + +=head1 METHOD GENERATOR TYPES + +=head2 new - Constructor + +For each method name passed, returns a subroutine with the following characteristics: + +=over 4 + +=item * + +Has a reference to a sample item to copy. This defaults to a reference to an empty hash, but you may override this with the C<'defaults' => I> method parameter. + +=item * + +If called as a class method, makes a new hash and blesses it into that class. + +=item * + +If called on a hash-based instance, makes a copy of it and blesses the copy into the same class as the original instance. + +=item * + +If passed a list of key-value pairs, appends them to the new hash. These arguments override any copied values, and later arguments with the same name will override earlier ones. + +=item * + +Returns the new instance. + +=back + +Sample declaration and usage: + + package MyObject; + use Class::MakeMethods::Standard::Hash ( + new => 'new', + ); + ... + + # Bare constructor + my $empty = MyObject->new(); + + # Constructor with initial values + my $obj = MyObject->new( foo => 'Foozle', bar => 'Barbados' ); + + # Copy with overriding value + my $copy = $obj->new( bar => 'Bob' ); + +=cut + +sub new { + map { + my $name = $_->{name}; + my $defaults = $_->{defaults} || {}; + $name => sub { + my $callee = shift; + my $self = ref($callee) ? bless( { %$callee }, ref $callee ) + : bless( { %$defaults }, $callee ); + while ( scalar @_ ) { + my $method = shift; + UNIVERSAL::can( $self, $method ) + or Carp::croak("Can't call method '$method' in constructor for " . ( ref($callee) || $callee )); + $self->$method( shift ); + } + return $self; + } + } (shift)->_get_declarations(@_) +} + +######################################################################## + +=head2 scalar - Instance Accessor + +For each method name passed, uses a closure to generate a subroutine with the following characteristics: + +=over 4 + +=item * + +Must be called on a hash-based instance. + +=item * + +Has a specific hash key to use to access the related value for each instance. +This defaults to the method name, but you may override this with the C<'hash_key' => I> method parameter. + +=item * + +If called without any arguments returns the current value. + +=item * + +If called with an argument, stores that as the value, and returns it, + +=back + +Sample declaration and usage: + + package MyObject; + use Class::MakeMethods::Standard::Hash ( + scalar => 'foo', + ); + ... + + # Store value + $obj->foo('Foozle'); + + # Retrieve value + print $obj->foo; + +=cut + +sub scalar { + map { + my $name = $_->{name}; + my $hash_key = $_->{hash_key} || $_->{name}; + $name => sub { + my $self = shift; + if ( scalar(@_) == 0 ) { + $self->{$hash_key}; + } else { + $self->{$hash_key} = shift; + } + } + } (shift)->_get_declarations(@_) +} + +######################################################################## + +=head2 array - Instance Ref Accessor + +For each method name passed, uses a closure to generate a subroutine with the following characteristics: + +=over 4 + +=item * + +Must be called on a hash-based instance. + +=item * + +Has a specific hash key to use to access the related value for each instance. +This defaults to the method name, but you may override this with the C<'hash_key' => I> method parameter. + +=item * + +The value for each instance will be a reference to an array (or undef). + +=item * + +If called without any arguments, returns the contents of the array in list context, or an array reference in scalar context (or undef). + +=item * + +If called with a single array ref argument, sets the contents of the array to match the contents of the provided one. + +=item * + +If called with a single numeric argument, uses that argument as an index to retrieve from the referenced array, and returns that value (or undef). + +=item * + +If called with a two arguments, the first undefined and the second an array ref argument, uses that array's contents as a list of indexes to return a slice of the referenced array. + +=item * + +If called with a list of argument pairs, each with a numeric index and an associated value, stores the value at the given index in the referenced array. If the instance's value was previously undefined, a new array is autovivified. The current value in each position will be overwritten, and later arguments with the same index will override earlier ones. Returns the current array-ref value. + +=item * + +If called with a list of argument pairs, each with the first item being a reference to an array of up to two numbers, loops over each pair and uses those numbers to splice the value array. + +The first controlling number is the position at which the splice will begin. Zero will start before the first item in the list. Negative numbers count backwards from the end of the array. + +The second number is the number of items to be removed from the list. If it is omitted, or undefined, or zero, no items are removed. If it is a positive integer, that many items will be returned. + +If both numbers are omitted, or are both undefined, they default to containing the entire value array. + +If the second argument is undef, no values will be inserted; if it is a non-reference value, that one value will be inserted; if it is an array-ref, its values will be copied. + +The method returns the items that removed from the array, if any. + +=back + +Sample declaration and usage: + + package MyObject; + use Class::MakeMethods::Standard::Hash ( + array => 'bar', + ); + ... + + # Clear and set contents of list + print $obj->bar([ 'Spume', 'Frost' ] ); + + # Set values by position + $obj->bar(0 => 'Foozle', 1 => 'Bang!'); + + # Positions may be overwritten, and in any order + $obj->bar(2 => 'And Mash', 1 => 'Blah!'); + + # Retrieve value by position + print $obj->bar(1); + + # Direct access to referenced array + print scalar @{ $obj->bar() }; + +There are also calling conventions for slice and splice operations: + + # Retrieve slice of values by position + print join(', ', $obj->bar( undef, [0, 2] ) ); + + # Insert an item at position in the array + $obj->bar([3], 'Potatoes' ); + + # Remove 1 item from position 3 in the array + $obj->bar([3, 1], undef ); + + # Set a new value at position 2, and return the old value + print $obj->bar([2, 1], 'Froth' ); + +=cut + +sub array { + map { + my $name = $_->{name}; + my $hash_key = $_->{hash_key} || $_->{name}; + my $init = $_->{auto_init}; + $name => sub { + my $self = shift; + if ( scalar(@_) == 0 ) { + if ( $init and ! defined $self->{$hash_key} ) { + $self->{$hash_key} = []; + } + ( ! $self->{$hash_key} ) ? () : + ( wantarray ) ? @{ $self->{$hash_key} } : + $self->{$hash_key} + } elsif ( scalar(@_) == 1 and ref $_[0] eq 'ARRAY' ) { + $self->{$hash_key} = [ @{ $_[0] } ]; + ( ! $self->{$hash_key} ) ? () : + ( wantarray ) ? @{ $self->{$hash_key} } : + $self->{$hash_key} + } else { + $self->{$hash_key} ||= []; + return array_splicer( $self->{$hash_key}, @_ ); + } + } + } (shift)->_get_declarations(@_) +} + +######################################################################## + +=head2 hash - Instance Ref Accessor + +For each method name passed, uses a closure to generate a subroutine with the following characteristics: + +=over 4 + +=item * + +Must be called on a hash-based instance. + +=item * + +Has a specific hash key to use to access the related value for each instance. +This defaults to the method name, but you may override this with the C<'hash_key' => I> method parameter. + +=item * + +The value for each instance will be a reference to a hash (or undef). + +=item * + +If called without any arguments, returns the contents of the hash in list context, or a hash reference in scalar context (or undef). + +=item * + +If called with one non-ref argument, uses that argument as an index to retrieve from the referenced hash, and returns that value (or undef). + +=item * + +If called with one array-ref argument, uses the contents of that array to retrieve a slice of the referenced hash. + +=item * + +If called with one hash-ref argument, sets the contents of the referenced hash to match that provided. + +=item * + +If called with a list of key-value pairs, stores the value under the given key in the referenced hash. If the instance's value was previously undefined, a new hash is autovivified. The current value under each key will be overwritten, and later arguments with the same key will override earlier ones. Returns the contents of the hash in list context, or a hash reference in scalar context. + +=back + +Sample declaration and usage: + + package MyObject; + use Class::MakeMethods::Standard::Hash ( + hash => 'baz', + ); + ... + + # Set values by key + $obj->baz('foo' => 'Foozle', 'bar' => 'Bang!'); + + # Values may be overwritten, and in any order + $obj->baz('broccoli' => 'Blah!', 'foo' => 'Fiddle'); + + # Retrieve value by key + print $obj->baz('foo'); + + # Retrive slice of values by position + print join(', ', $obj->baz( ['foo', 'bar'] ) ); + + # Direct access to referenced hash + print keys %{ $obj->baz() }; + + # Reset the hash contents to empty + %{ $obj->baz() } = (); + +=cut + +sub hash { + map { + my $name = $_->{name}; + my $hash_key = $_->{hash_key} || $_->{name}; + my $init = $_->{auto_init}; + $name => sub { + my $self = shift; + if ( scalar(@_) == 0 ) { + if ( $init and ! defined $self->{$hash_key} ) { + $self->{$hash_key} = {}; + } + ( ! $self->{$hash_key} ) ? () : + ( wantarray ) ? %{ $self->{$hash_key} } : + $self->{$hash_key} + } elsif ( scalar(@_) == 1 ) { + if ( ref($_[0]) eq 'HASH' ) { + $self->{$hash_key} = { %{$_[0]} }; + } elsif ( ref($_[0]) eq 'ARRAY' ) { + return @{$self->{$hash_key}}{ @{$_[0]} } + } else { + return $self->{$hash_key}->{ $_[0] } + } + } elsif ( scalar(@_) % 2 ) { + Carp::croak "Odd number of items in assigment to $name"; + } else { + while ( scalar(@_) ) { + my $key = shift(); + $self->{$hash_key}->{ $key } = shift(); + } + return $self->{$hash_key}; + } + } + } (shift)->_get_declarations(@_) +} + +######################################################################## + +=head2 object - Instance Ref Accessor + +For each method name passed, uses a closure to generate a subroutine with the following characteristics: + +=over 4 + +=item * + +Must be called on a hash-based instance. + +=item * + +Has a specific hash key to use to access the related value for each instance. +This defaults to the method name, but you may override this with the C<'hash_key' => I> method parameter. + +=item * + +The value for each instance will be a reference to an object (or undef). + +=item * + +If called without any arguments returns the current value. + +=item * + +If called with an argument, stores that as the value, and returns it, + +=back + +Sample declaration and usage: + + package MyObject; + use Class::MakeMethods::Standard::Hash ( + object => 'foo', + ); + ... + + # Store value + $obj->foo( Foozle->new() ); + + # Retrieve value + print $obj->foo; + +=cut + +sub object { + map { + my $name = $_->{name}; + my $hash_key = $_->{hash_key} || $_->{name}; + my $class = $_->{class}; + my $init = $_->{auto_init}; + if ( $init and ! $class ) { + Carp::croak("Use of auto_init requires value for class parameter") + } + my $new_method = $_->{new_method} || 'new'; + $name => sub { + my $self = shift; + if ( scalar @_ ) { + my $value = shift; + if ( $class and ! UNIVERSAL::isa( $value, $class ) ) { + Carp::croak "Wrong argument type ('$value') in assigment to $name"; + } + $self->{$hash_key} = $value; + } else { + if ( $init and ! defined $self->{$hash_key} ) { + $self->{$hash_key} = $class->$new_method(); + } + $self->{$hash_key}; + } + } + } (shift)->_get_declarations(@_) +} + +######################################################################## + +=head1 SEE ALSO + +See L for general information about this distribution. + +See L for more about this family of subclasses. + +=cut + +1; diff --git a/lib/Class/MakeMethods/Standard/Inheritable.pm b/lib/Class/MakeMethods/Standard/Inheritable.pm new file mode 100644 index 0000000..d1b72ac --- /dev/null +++ b/lib/Class/MakeMethods/Standard/Inheritable.pm @@ -0,0 +1,428 @@ +=head1 NAME + +Class::MakeMethods::Standard::Inheritable - Overridable data + +=head1 SYNOPSIS + + package MyClass; + + use Class::MakeMethods( 'Standard::Inheritable:scalar' => 'foo' ); + # We now have an accessor method for an "inheritable" scalar value + + MyClass->foo( 'Foozle' ); # Set a class-wide value + print MyClass->foo(); # Retrieve class-wide value + + my $obj = MyClass->new(...); + print $obj->foo(); # All instances "inherit" that value... + + $obj->foo( 'Foible' ); # until you set a value for an instance. + print $obj->foo(); # This now finds object-specific value. + ... + + package MySubClass; + @ISA = 'MyClass'; + + print MySubClass->foo(); # Intially same as superclass, + MySubClass->foo('Foobar'); # but overridable per subclass, + print $subclass_obj->foo(); # and shared by its instances + $subclass_obj->foo('Fosil');# until you override them... + ... + + # Similar behaviour for hashes and arrays is currently incomplete + package MyClass; + use Class::MakeMethods::Standard::Inheritable ( + array => 'my_list', + hash => 'my_index', + ); + + MyClass->my_list(0 => 'Foozle', 1 => 'Bang!'); + print MyClass->my_list(1); + + MyClass->my_index('broccoli' => 'Blah!', 'foo' => 'Fiddle'); + print MyClass->my_index('foo'); + + +=head1 DESCRIPTION + +The MakeMethods subclass provides accessor methods that search an inheritance tree to find a value. This allows you to set a shared or default value for a given class, optionally override it in a subclass, and then optionally override it on a per-instance basis. + +Note that all MakeMethods methods are inheritable, in the sense that they work as expected for subclasses. These methods are different in that the I accessed by each method can be inherited or overridden in each subclass or instance. See L< Class::MakeMethods::Utility::Inheritable> for more about this type of "inheritable" or overridable" data. + + +=head2 Calling Conventions + +When you C this package, the method names you provide +as arguments cause subroutines to be generated and installed in +your module. + +See L for more information. + +=head2 Declaration Syntax + +To declare methods, pass in pairs of a method-type name followed +by one or more method names. + +Valid method-type names for this package are listed in L<"METHOD +GENERATOR TYPES">. + +See L and L for more information. + +=cut + +package Class::MakeMethods::Standard::Inheritable; + +$VERSION = 1.000; +use strict; + +use Class::MakeMethods::Standard '-isasubclass'; +use Class::MakeMethods::Utility::Inheritable qw(get_vvalue set_vvalue find_vself); +use Class::MakeMethods::Utility::ArraySplicer 'array_splicer'; + +######################################################################## + +=head1 METHOD GENERATOR TYPES + +=head2 scalar - Class-specific Accessor + +For each method name passed, uses a closure to generate a subroutine with the following characteristics: + +=over 4 + +=item * + +May be called as a class or instance method, on the declaring class or any subclass. + +=item * + +If called without any arguments returns the current value for the callee. If the callee has not had a value defined for this method, searches up from instance to class, and from class to superclass, until a callee with a value is located. + +=item * + +If called with an argument, stores that as the value associated with the callee, whether instance or class, and returns it, + +=back + +Sample declaration and usage: + + package MyClass; + use Class::MakeMethods::Standard::Inheritable ( + scalar => 'foo', + ); + ... + + # Store value + MyClass->foo('Foozle'); + + # Retrieve value + print MyClass->foo; + +=cut + +sub scalar { + my $class = shift; + map { + my $method = $_; + my $name = $method->{name}; + $method->{data} ||= {}; + $name => sub { + my $self = shift; + if ( scalar(@_) == 0 ) { + get_vvalue($method->{data}, $self); + } else { + my $value = shift; + set_vvalue($method->{data}, $self, $value); + } + } + } $class->_get_declarations(@_) +} + +######################################################################## + +=head2 array - Class-specific Ref Accessor + +For each method name passed, uses a closure to generate a subroutine with the following characteristics: + +=over 4 + +=item * + +May be called as a class method, or on any instance or subclass, Must be called on a hash-based instance. + +=item * + +The class value will be a reference to an array (or undef). + +=item * + +If called without any arguments, returns the contents of the array in list context, or an array reference in scalar context (or undef). + +=item * + +If called with a single array ref argument, sets the contents of the array to match the contents of the provided one. + +=item * + +If called with a single numeric argument, uses that argument as an index to retrieve from the referenced array, and returns that value (or undef). + +=item * + +If called with a two arguments, the first undefined and the second an array ref argument, uses that array's contents as a list of indexes to return a slice of the referenced array. + +=item * + +If called with a list of argument pairs, each with a non-ref index and an associated value, stores the value at the given index in the referenced array. If the class value was previously undefined, a new array is autovivified. The current value in each position will be overwritten, and later arguments with the same index will override earlier ones. Returns the current array-ref value. + +=item * + +If called with a list of argument pairs, each with the first item being a reference to an array of up to two numbers, loops over each pair and uses those numbers to splice the value array. + +The first controlling number is the position at which the splice will begin. Zero will start before the first item in the list. Negative numbers count backwards from the end of the array. + +The second number is the number of items to be removed from the list. If it is omitted, or undefined, or zero, no items are removed. If it is a positive integer, that many items will be returned. + +If both numbers are omitted, or are both undefined, they default to containing the entire value array. + +If the second argument is undef, no values will be inserted; if it is a non-reference value, that one value will be inserted; if it is an array-ref, its values will be copied. + +The method returns the items that removed from the array, if any. + +=back + +Sample declaration and usage: + + package MyClass; + use Class::MakeMethods::Standard::Inheritable ( + array => 'bar', + ); + ... + + # Clear and set contents of list + print MyClass->bar([ 'Spume', 'Frost' ] ); + + # Set values by position + MyClass->bar(0 => 'Foozle', 1 => 'Bang!'); + + # Positions may be overwritten, and in any order + MyClass->bar(2 => 'And Mash', 1 => 'Blah!'); + + # Retrieve value by position + print MyClass->bar(1); + + # Direct access to referenced array + print scalar @{ MyClass->bar() }; + +There are also calling conventions for slice and splice operations: + + # Retrieve slice of values by position + print join(', ', MyClass->bar( undef, [0, 2] ) ); + + # Insert an item at position in the array + MyClass->bar([3], 'Potatoes' ); + + # Remove 1 item from position 3 in the array + MyClass->bar([3, 1], undef ); + + # Set a new value at position 2, and return the old value + print MyClass->bar([2, 1], 'Froth' ); + +=cut + +sub array { + my $class = shift; + map { + my $method = $_; + my $name = $method->{name}; + $name => sub { + my $self = shift; + + if ( scalar(@_) == 0 ) { + my $v_self = find_vself($method->{data}, $self); + my $value = $v_self ? $method->{data}{$v_self} : (); + if ( $method->{auto_init} and ! $value ) { + $value = $method->{data}{$self} = []; + } + ! $value ? () : wantarray ? @$value : $value; + + } elsif ( scalar(@_) == 1 and ref $_[0] eq 'ARRAY' ) { + $method->{data}{$self} = [ @{ $_[0] } ]; + wantarray ? @{ $method->{data}{$self} } : $method->{data}{$self} + + } else { + if ( ! exists $method->{data}{$self} ) { + my $v_self = find_vself($method->{data}, $self); + $method->{data}{$self} = [ $v_self ? @$v_self : () ]; + } + return array_splicer( $method->{data}{$self}, @_ ); + } + } + } $class->_get_declarations(@_) +} + +######################################################################## + +=head2 hash - Class-specific Ref Accessor + +For each method name passed, uses a closure to generate a subroutine with the following characteristics: + +=over 4 + +=item * + +May be called as a class method, or on any instance or subclass, Must be called on a hash-based instance. + +=item * + +The class value will be a reference to a hash (or undef). + +=item * + +If called without any arguments, returns the contents of the hash in list context, or a hash reference in scalar context. If the callee has not had a value defined for this method, searches up from instance to class, and from class to superclass, until a callee with a value is located. + +=item * + +If called with one non-ref argument, uses that argument as an index to retrieve from the referenced hash, and returns that value (or undef). If the callee has not had a value defined for this method, searches up from instance to class, and from class to superclass, until a callee with a value is located. + +=item * + +If called with one array-ref argument, uses the contents of that array to retrieve a slice of the referenced hash. If the callee has not had a value defined for this method, searches up from instance to class, and from class to superclass, until a callee with a value is located. + +=item * + +If called with one hash-ref argument, sets the contents of the referenced hash to match that provided. + +=item * + +If called with a list of key-value pairs, stores the value under the given key in the hash associated with the callee, whether instance or class. If the callee did not previously have a hash-ref value associated with it, searches up instance to class, and from class to superclass, until a callee with a value is located, and copies that hash before making the assignments. The current value under each key will be overwritten, and later arguments with the same key will override earlier ones. Returns the contents of the hash in list context, or a hash reference in scalar context. + +=back + +Sample declaration and usage: + + package MyClass; + use Class::MakeMethods::Standard::Inheritable ( + hash => 'baz', + ); + ... + + # Set values by key + MyClass->baz('foo' => 'Foozle', 'bar' => 'Bang!'); + + # Values may be overwritten, and in any order + MyClass->baz('broccoli' => 'Blah!', 'foo' => 'Fiddle'); + + # Retrieve value by key + print MyClass->baz('foo'); + + # Retrive slice of values by position + print join(', ', MyClass->baz( ['foo', 'bar'] ) ); + + # Direct access to referenced hash + print keys %{ MyClass->baz() }; + + # Reset the hash contents to empty + @{ MyClass->baz() } = (); + +B + +=cut + +sub hash { + my $class = shift; + map { + my $method = $_; + my $name = $method->{name}; + $name => sub { + my $self = shift; + if ( scalar(@_) == 0 ) { + my $v_self = find_vself($method->{data}, $self); + my $value = $v_self ? $method->{data}{$v_self} : (); + if ( $method->{auto_init} and ! $value ) { + $value = $method->{data}{$self} = {}; + } + ! $value ? () : wantarray ? %$value : $value; + } elsif ( scalar(@_) == 1 ) { + if ( ref($_[0]) eq 'HASH' ) { + $method->{data}{$self} = { %{$_[0]} }; + } elsif ( ref($_[0]) eq 'ARRAY' ) { + my $v_self = find_vself($method->{data}, $self); + return unless $v_self; + return @{$method->{data}{$v_self}}{ @{$_[0]} } + } else { + my $v_self = find_vself($method->{data}, $self); + return unless $v_self; + return $method->{data}{$v_self}->{ $_[0] }; + } + } elsif ( scalar(@_) % 2 ) { + Carp::croak "Odd number of items in assigment to $method->{name}"; + } else { + if ( ! exists $method->{data}{$self} ) { + my $v_self = find_vself($method->{data}, $self); + $method->{data}{$self} = { $v_self ? %$v_self : () }; + } + while ( scalar(@_) ) { + my $key = shift(); + $method->{data}{$self}->{ $key } = shift(); + } + wantarray ? %{ $method->{data}{$self} } : $method->{data}{$self}; + } + } + } $class->_get_declarations(@_) +} + +######################################################################## + +=head2 object - Class-specific Ref Accessor + +For each method name passed, uses a closure to generate a subroutine with the following characteristics: + +=over 4 + +=item * + +May be called as a class method, or on any instance or subclass, Must be called on a hash-based instance. + +=item * + +The class value will be a reference to an object (or undef). + +=item * + +If called without any arguments returns the current value for the callee. If the callee has not had a value defined for this method, searches up from instance to class, and from class to superclass, until a callee with a value is located. + +=item * + +If called with an argument, stores that as the value associated with the callee, whether instance or class, and returns it, + +=back + +Sample declaration and usage: + + package MyClass; + use Class::MakeMethods::Standard::Inheritable ( + object => 'foo', + ); + ... + + # Store value + MyClass->foo( Foozle->new() ); + + # Retrieve value + print MyClass->foo; + +B + +=cut + +sub object { } + +######################################################################## + +=head1 SEE ALSO + +See L for general information about this distribution. + +See L for more about this family of subclasses. + +=cut + +1; diff --git a/lib/Class/MakeMethods/Standard/Universal.pm b/lib/Class/MakeMethods/Standard/Universal.pm new file mode 100644 index 0000000..641b159 --- /dev/null +++ b/lib/Class/MakeMethods/Standard/Universal.pm @@ -0,0 +1,336 @@ +=head1 NAME + +Class::MakeMethods::Standard::Universal - Generic Methods + + +=head1 SYNOPSIS + + package MyObject; + use Class::MakeMethods::Standard::Universal ( + no_op => 'this', + abstract => 'that', + delegate => { name=>'play_music', target=>'instrument', method=>'play' }, + ); + + +=head1 DESCRIPTION + +The Standard::Universal suclass of MakeMethods provides a [INCOMPLETE]. + +=head2 Calling Conventions + +When you C this package, the method names you provide +as arguments cause subroutines to be generated and installed in +your module. + +See L for more information. + +=head2 Declaration Syntax + +To declare methods, pass in pairs of a method-type name followed +by one or more method names. + +Valid method-type names for this package are listed in L<"METHOD +GENERATOR TYPES">. + +See L and L for more information. + +=cut + +package Class::MakeMethods::Standard::Universal; + +$VERSION = 1.000; +use strict; +use Carp; +use Class::MakeMethods::Standard '-isasubclass'; + +######################################################################## + +=head1 METHOD GENERATOR TYPES + +=head2 no_op - Placeholder + +For each method name passed, returns a subroutine with the following characteristics: + +=over 4 + +=item * + +Does nothing. + +=back + +You might want to create and use such methods to provide hooks for +subclass activity. + +Sample declaration and usage: + + package MyObject; + use Class::MakeMethods::Standard::Universal ( + no_op => 'whatever', + ); + ... + + # Doesn't do anything + MyObject->whatever(); + +=cut + +sub no_op { + map { + my $method = $_; + $method->{name} => sub { } + } (shift)->_get_declarations(@_) +} + +######################################################################## + +=head2 abstract - Placeholder + +For each method name passed, returns a subroutine with the following characteristics: + +=over 4 + +=item * + +Fails with an error message. + +=back + +This is intended to support the use of abstract methods, that must +be overidden in a useful subclass. + +If each subclass is expected to provide an implementation of a given method, using this abstract method will replace the generic error message below with the clearer, more explicit error message that follows it: + + Can't locate object method "foo" via package "My::Subclass" + The "foo" method is abstract and can not be called on My::Subclass + +However, note that the existence of this method will be detected by UNIVERSAL::can(), so it is not suitable for use in optional interfaces, for which you may wish to be able to detect whether the method is supported or not. + +Sample declaration and usage: + + package MyObject; + use Class::MakeMethods::Standard::Universal ( + abstract => 'whatever', + ); + ... + + package MySubclass; + sub whatever { ... } + + # Failure + MyObject->whatever(); + + # Success + MySubclass->whatever(); + +=cut + +sub abstract { + map { + my $method = $_; + $method->{name} => sub { + my $self = shift; + my $class = ref($self) ? "a " . ref($self) . " object" : $self; + croak("The $method->{name} method is abstract and can not be called on $class"); + } + } (shift)->_get_declarations(@_) +} + +######################################################################## + +=head2 call_methods - Call methods by name + +For each method name passed, returns a subroutine with the following characteristics: + +=over 4 + +=item * + +Accepts a hash of key-value pairs, or a reference to hash of such pairs. For each pair, the key is interpreted as the name of a method to call, and the value is the argument to be passed to that method. + +=back + +Sample declaration and usage: + + package MyObject; + use Class::MakeMethods::Standard::Universal ( + call_methods => 'init', + ); + ... + + my $object = MyObject->new() + $object->init( foo => 'Foozle', bar => 'Barbados' ); + + # Equivalent to: + $object->foo('Foozle'); + $object->bar('Barbados'); + +=cut + +sub call_methods { + map { + my $method = $_; + $method->{name} => sub { + my $self = shift; + local @_ = %{$_[0]} if ( scalar @_ == 1 and ref($_[0]) eq 'HASH'); + while (scalar @_) { + my $key = shift; + $self->$key( shift ) + } + } + } (shift)->_get_declarations(@_) +} + + +######################################################################## + +=head2 join_methods - Concatenate results of other methods + +For each method name passed, returns a subroutine with the following characteristics: + +=over 4 + +=item * + +Has a list of other methods names as an arrayref in the 'methods' parameter. B. + +=item * + +When called, calls each of the named method on itself, in order, and returns the concatenation of their results. + +=item * + +If a 'join' parameter is provided it is included between each method result. + +=item * + +If the 'skip_blanks' parameter is omitted, or is provided with a true value, removes all undefined or empty-string values from the results. + +=back + +=cut + +sub join_methods { + map { + my $method = $_; + $method->{methods} or confess; + $method->{join} = '' if ( ! defined $method->{join} ); + $method->{skip_blanks} = '1' if ( ! defined $method->{skip_blanks} ); + $method->{name} => sub { + my $self = shift; + my $joiner = $method->{join}; + my @values = map { $self->$_() } @{ $method->{methods} }; + @values = grep { defined and length } @values if ( $method->{skip_blanks} ); + join $joiner, @values; + } + } (shift)->_get_declarations(@_) +} + +######################################################################## + +=head2 alias - Call another method + +For each method name passed, returns a subroutine with the following characteristics: + +=over 4 + +=item * + +Calls another method on the same callee. + +=back + +You might create such a method to extend or adapt your class' interface. + +Sample declaration and usage: + + package MyObject; + use Class::MakeMethods::Standard::Universal ( + alias => { name=>'click_here', target=>'complex_machinery' } + ); + sub complex_machinery { ... } + ... + + $myobj->click_here(...); # calls $myobj->complex_machinery(...) + +=cut + +sub alias { + map { + my $method = $_; + $method->{name} => sub { + my $self = shift; + + my $t_method = $method->{target} or confess("no target"); + my @t_args = $method->{target_args} ? @{$method->{target_args}} : (); + + $self->$t_method(@t_args, @_); + } + } (shift)->_get_declarations(@_) +} + +######################################################################## + +=head2 delegate - Use another object to provide method + +For each method name passed, returns a subroutine with the following characteristics: + +=over 4 + +=item * + +Calls a method on self to retrieve another object, and then calls a method on that object and returns its value. + +=back + +You might want to create and use such methods to faciliate composition of objects from smaller objects. + +Sample declaration and usage: + + package MyObject; + use Class::MakeMethods::Standard::Universal ( + 'Standard::Hash:object' => { name=>'instrument' }, + delegate => { name=>'play_music', target=>'instrument', method=>'play' } + ); + ... + + my $object = MyObject->new(); + $object->instrument( MyInstrument->new ); + $object->play_music; + +=cut + +sub delegate { + map { + my $method = $_; + $method->{method} ||= $method->{name}; + $method->{name} => sub { + my $self = shift; + + my $t_method = $method->{target} or confess("no target"); + my @t_args = $method->{target_args} ? @{$method->{target_args}} : (); + + my $m_method = $method->{method} or confess("no method"); + my @m_args = $method->{method_args} ? @{$method->{method_args}} : (); + push @m_args, $self if ( $method->{target_args_self} ); + + my $obj = $self->$t_method( @t_args ) + or croak("Can't delegate $method->{name} because $t_method is empty"); + + $obj->$m_method(@m_args, @_); + } + } (shift)->_get_declarations(@_) +} + +######################################################################## + +=head1 SEE ALSO + +See L for general information about this distribution. + +See L for more about this family of subclasses. + +=cut + +1; diff --git a/lib/Class/MakeMethods/Template.pm b/lib/Class/MakeMethods/Template.pm new file mode 100644 index 0000000..d5cd8f5 --- /dev/null +++ b/lib/Class/MakeMethods/Template.pm @@ -0,0 +1,1255 @@ +package Class::MakeMethods::Template; + +use strict; +use Carp; + +use Class::MakeMethods '-isasubclass'; + +use vars qw( $VERSION ); +$VERSION = 1.008; + +sub _diagnostic { &Class::MakeMethods::_diagnostic } + +######################################################################## +### TEMPLATE LOOKUP AND CACHING: named_method(), _definition() +######################################################################## + +use vars qw( %TemplateCache ); + +# @results = $class->named_method( $name, @arguments ); +sub named_method { + my $class = shift; + my $name = shift; + + # Support direct access to cached Template information + if (exists $TemplateCache{"$class\::$name"}) { + return $TemplateCache{"$class\::$name"}; + } + + my @results = $class->$name( @_ ); + + if ( scalar @results == 1 and ref $results[0] eq 'HASH' ) { + # If this is a hash-definition format, cache the results for speed. + my $def = $results[0]; + $TemplateCache{"$class\::$name"} = $def; + _expand_definition($class, $name, $def); + return $def; + } + + return wantarray ? @results : $results[0]; +} + +# $mm_def = _definition( $class, $target ); +sub _definition { + my ($class, $target) = @_; + + while ( ! ref $target ) { + $target =~ s/\s.*//; + + # If method name contains a colon or double colon, call the method on the + # indicated class. + my $call_class = ( ( $target =~ s/^(.*)\:{1,2}// ) + ? Class::MakeMethods::_find_subclass($class, $1) : $class ); + $target = $call_class->named_method( $target ); + } + _diagnostic('mmdef_not_interpretable', $target) + unless ( ref($target) eq 'HASH' or ref($target) eq __PACKAGE__ ); + + return $target; +} + +######################################################################## +### TEMPLATE INTERNALS: _expand_definition() +######################################################################## + +sub _expand_definition { + my ($class, $name, $mm_def) = @_; + + return $mm_def if $mm_def->{'-parsed'}; + + $mm_def->{'template_class'} = $class; + $mm_def->{'template_name'} = $name; + + # Allow definitions to import values from each other. + my $importer; + foreach $importer ( qw( interface params behavior code_expr modifier ) ) { + my $rules = $mm_def->{$importer}->{'-import'} || $mm_def->{'-import'}; + my @rules = ( ref $rules eq 'HASH' ? %$rules : ref $rules eq 'ARRAY' ? @$rules : () ); + unshift @rules, '::' . $class . ':generic' => '*' if $class->can('generic'); + while ( + my ($source, $names) = splice @rules, 0, 2 + ) { + my $mmi = _definition($class, $source); + foreach ( ( $names eq '*' ) ? keys %{ $mmi->{$importer} } + : ( ref $names ) ? @{ $names } : ( $names ) ) { + my $current = $mm_def->{$importer}{$_}; + my $import = $mmi->{$importer}{$_}; + if ( ! $current ) { + $mm_def->{$importer}{$_} = $import; + } elsif ( ref($current) eq 'ARRAY' ) { + my @imports = ref($import) ? @$import : $import; + foreach my $imp ( @imports ) { + push @$current, $imp unless ( grep { $_ eq $imp } @$current ); + } + } + } + } + delete $mm_def->{$importer}->{'-import'}; + } + delete $mm_def->{'-import'}; + + _describe_definition( $mm_def ) if $Class::MakeMethods::CONTEXT{Debug}; + + + $mm_def->{'-parsed'} = "$_[1]"; + + bless $mm_def, __PACKAGE__; +} + +sub _describe_definition { + my $mm_def = shift; + + my $def_type = "$mm_def->{template_class}:$mm_def->{template_name}"; + warn "----\nMethods info for $def_type:\n"; + if ( $mm_def->{interface} ) { + warn join '', "Templates: \n", map { + " $_: " . _describe_value($mm_def->{interface}{$_}) . "\n" + } keys %{$mm_def->{interface}}; + } + if ( $mm_def->{modifier} ) { + warn join '', "Modifiers: \n", map { + " $_: " . _describe_value($mm_def->{modifier}{$_}) . "\n" + } keys %{$mm_def->{modifier}}; + } +} + +sub _describe_value { + my $value = $_[0]; + ref($value) eq 'ARRAY' ? join(', ', @$value) : + ref($value) eq 'HASH' ? join(', ', %$value) : + "$value"; +} + +######################################################################## +### METHOD GENERATION: make_methods() +######################################################################## + +sub make_methods { + my $mm_def = shift; + + return unless ( scalar @_ ); + + # Select default interface and initial method parameters + my $defaults = { %{ ( $mm_def->{'params'} ||= {} ) } }; + $defaults->{'interface'} ||= $mm_def->{'interface'}{'-default'} || 'default'; + $defaults->{'target_class'} = $mm_def->_context('TargetClass'); + $defaults->{'template_class'} = $mm_def->{'template_class'}; + $defaults->{'template_name'} = $mm_def->{'template_name'}; + + my %interface_cache; + + # Our return value is the accumulated list of method-name => method-sub pairs + my @methods; + + while (scalar @_) { + + ### PARSING ### Requires: $mm_def, $defaults, @_ + + my $m_name = shift @_; + _diagnostic('make_empty') unless ( defined $m_name and length $m_name ); + + # Normalize: If we've got an array of names, replace it with those names + if ( ref $m_name eq 'ARRAY' ) { + my @items = @{ $m_name }; + # If array is followed by a params hash, each one gets the same params + if ( scalar @_ and ref $_[0] eq 'HASH' and ! exists $_[0]->{'name'} ) { + my $params = shift; + @items = map { $_, $params } @items + } + unshift @_, @items; + next; + } + + # Parse interfaces, modifiers and parameters + if ( $m_name =~ s/^-// ) { + if ( $m_name !~ s/^-// ) { + # -param => value + $defaults->{$m_name} = shift @_; + } else { + if ( $m_name eq '' ) { + # '--' => { param => value ... } + %$defaults = ( %$defaults, %{ shift @_ } ); + + } elsif ( exists $mm_def->{'interface'}{$m_name} ) { + # --interface + $defaults->{'interface'} = $m_name; + + } elsif ( exists $mm_def->{'modifier'}{$m_name} ) { + # --modifier + $defaults->{'modifier'} .= + ( $defaults->{'modifier'} ? ' ' : '' ) . "-$m_name"; + + } elsif ( exists $mm_def->{'behavior'}{$m_name} ) { + # --behavior as shortcut for single-method interface + $defaults->{'interface'} = $m_name; + + } else { + _diagnostic('make_bad_modifier', $mm_def->{'name'}, "--$m_name"); + } + } + next; + } + + # Make a new meta-method hash + my $m_info; + + # Parse string, string-then-hash, and hash-only meta-method parameters + if ( ! ref $m_name ) { + if ( scalar @_ and ref $_[0] eq 'HASH' and ! exists $_[0]->{'name'} ) { + %$m_info = ( 'name' => $m_name, %{ shift @_ } ); + } else { + $m_info = { 'name' => $m_name }; + } + + } elsif ( ref $m_name eq 'HASH' ) { + unless ( exists $m_name->{'name'} and length $m_name->{'name'} ) { + _diagnostic('make_noname'); + } + $m_info = { %$m_name }; + + } else { + _diagnostic('make_unsupported', $m_name); + } + _diagnostic('debug_declaration', join(', ', map { defined $_ ? $_ : '(undef)' } %$m_info) ); + + ### INITIALIZATION ### Requires: $mm_def, $defaults, $m_info + + my $interface = ( + $interface_cache{ $m_info->{'interface'} || $defaults->{'interface'} } + ||= _interpret_interface( $mm_def, $m_info->{'interface'} || $defaults->{'interface'} ) + ); + %$m_info = ( + %$defaults, + ( $interface->{-params} ? %{$interface->{-params}} : () ), + %$m_info + ); + + + # warn "Actual: " . Dumper( $m_info ); + + + # Expand * and *{...} strings. + foreach (grep defined $m_info->{$_}, keys %$m_info) { + $m_info->{$_} =~ s/\*(?:\{([^\}]+)?\})?/ $m_info->{ $1 || 'name' } /ge + } + if ( $m_info->{'modifier'} and $mm_def->{modifier}{-folding} ) { + $m_info->{'modifier'} = _fold_modifiers( $m_info->{'modifier'}, + $mm_def->{modifier}{-folding} ) + } + + ### METHOD GENERATION ### Requires: $mm_def, $interface, $m_info + + # If the MM def provides an initialization "-init" call, run it. + if ( local $_ = $mm_def->{'behavior'}->{'-init'} ) { + push @methods, map $_->( $m_info ), (ref($_) eq 'ARRAY') ? @$_ : $_; + } + # Build Methods + for ( grep { /^[^-]/ } keys %$interface ) { + my $function_name = $_; + $function_name =~ s/\*/$m_info->{'name'}/g; + + my $behavior = $interface->{$_}; + + # Fold in additional modifiers + if ( $m_info->{'modifier'} ) { + if ( $behavior =~ /^\-/ and $mm_def->{modifier}{-folding} ) { + $behavior = $m_info->{'modifier'} = + _fold_modifiers( "$m_info->{'modifier'} $behavior", + $mm_def->{modifier}{-folding} ) + } else { + $behavior = "$m_info->{'modifier'} $behavior"; + } + } + + my $builder = + ( $mm_def->{'-behavior_cache'}{$behavior} ) ? + $mm_def->{'-behavior_cache'}{$behavior} : + ( ref($mm_def->{'behavior'}{$behavior}) eq 'CODE' ) ? + $mm_def->{'behavior'}{$behavior} : +_behavior_builder( $mm_def, $behavior, $m_info ); + + my $method = &$builder( $m_info ); + + _diagnostic('debug_make_behave', $behavior, $function_name, $method); + push @methods, ($function_name => $method) if ($method); + } + + # If the MM def provides a "-subs" call, for forwarding and other + # miscelaneous "subsidiary" or "contained" methods, run it. + if ( my $subs = $mm_def->{'behavior'}->{'-subs'} ) { + my @subs = (ref($subs) eq 'ARRAY') ? @$subs : $subs; + foreach my $sub ( @subs ) { + my @results = $sub->($m_info); + if ( scalar @results == 1 and ref($results[0]) eq 'HASH' ) { + # If it returns a hash of helper method types, check the method info + # for any matching names and call the corresponding method generator. + my $types = shift @results; + foreach my $type ( keys %$types ) { + my $names = $m_info->{$type} or next; + my @names = ref($names) eq 'ARRAY' ? @$names : split(' ', $names); + my $generator = $types->{$type}; + push @results, map { $_ => &$generator($m_info, $_) } @names; + } + } + push @methods, @results; + } + } + + # If the MM def provides a "-register" call, for registering meta-method + # information for run-time access, run it. + if ( local $_ = $mm_def->{'behavior'}->{'-register'} ) { + push @methods, map $_->( $m_info ), (ref($_) eq 'ARRAY') ? @$_ : $_; + } + } + + return @methods; +} + +# I'd like for the make_methods() sub to be simpler, and to take advantage +# of the standard _get_declarations parsing provided by the superclass. +# Sadly the below doesn't work, due to a few order-of-operations peculiarities +# of parsing interfaces and modifiers, and their associated default paramters. +# Perhaps it might work if the processing of --options could be overridden with +# a callback sub, so that interfaces and their params can be parsed in order. +sub _x_get_declarations { + my $mm_def = shift; + + my @declarations = $mm_def::SUPER->_get_declarations( @_ ); + + # use Data::Dumper; + # warn "In: " . Dumper( \@_ ); + # warn "Auto: " . Dumper( \@declarations ); + + my %interface_cache; + + while (scalar @declarations) { + + my $m_info = shift @declarations; + + # Parse interfaces and modifiers + my @specials = grep $_, split '--', ( delete $m_info->{'--'} || '' ); + foreach my $special ( @specials ) { + if ( exists $mm_def->{'interface'}{$special} ) { + # --interface + $m_info->{'interface'} = $special; + + } elsif ( exists $mm_def->{'modifier'}{$special} ) { + # --modifier + $m_info->{'modifier'} .= + ( $m_info->{'modifier'} ? ' ' : '' ) . "-$special"; + + } elsif ( exists $mm_def->{'behavior'}{$special} ) { + # --behavior as shortcut for single-method interface + $m_info->{'interface'} = $special; + + } else { + _diagnostic('make_bad_modifier', $mm_def->{'name'}, "--$special"); + } + } + + my $interface = ( + $interface_cache{ $m_info->{'interface'} } + ||= _interpret_interface( $mm_def, $m_info->{'interface'} ) + ); + $m_info = { %$m_info, %{$interface->{-params}} } if $interface->{-params}; + + _diagnostic('debug_declaration', join(', ', map { defined $_ ? $_ : '(undef)' } %$m_info) ); + + # warn "Updated: " . Dumper( $m_info ); + } +} + +######################################################################## +### TEMPLATES: _interpret_interface() +######################################################################## + +sub _interpret_interface { + my ($mm_def, $interface) = @_; + + if ( ref $interface eq 'HASH' ) { + return $interface if exists $interface->{'-parsed'}; + } + elsif ( ! defined $interface or ! length $interface ) { + _diagnostic('tmpl_empty'); + + } + elsif ( ! ref $interface ) { + if ( exists $mm_def->{'interface'}{ $interface } ) { + if ( ! ref $mm_def->{'interface'}{ $interface } ) { + $mm_def->{'interface'}{ $interface } = + { '*' => $mm_def->{'interface'}{ $interface } }; + } + } elsif ( exists $mm_def->{'behavior'}{ $interface } ) { + $mm_def->{'interface'}{ $interface } = { '*' => $interface }; + } else { + _diagnostic('tmpl_unkown', $interface); + } + $interface = $mm_def->{'interface'}{ $interface }; + + return $interface if exists $interface->{'-parsed'}; + } + elsif ( ref $interface ne 'HASH' ) { + _diagnostic('tmpl_unsupported', $interface); + } + + $interface->{'-parsed'} = "$_[1]"; + + # Allow interface inheritance via -base specification + if ( $interface->{'-base'} ) { + for ( split ' ', $interface->{'-base'} ) { + my $base = _interpret_interface( $mm_def, $_ ); + %$interface = ( %$base, %$interface ); + } + delete $interface->{'-base'}; + } + + for (keys %$interface) { + # Remove empty/undefined items. + unless ( defined $interface->{$_} and length $interface->{$_} ) { + delete $interface->{$_}; + next; + } + } + # _diagnostic('debug_interface', $_[1], join(', ', %$interface )); + + return $interface; +} + +######################################################################## +### BEHAVIORS AND MODIFIERS: _fold_modifiers(), _behavior_builder() +######################################################################## + +sub _fold_modifiers { + my $spec = shift; + my $rules = shift; + my %rules = @$rules; + + # Longest first, to prevent over-eager matching. + my $rule = join '|', map "\Q$_\E", + sort { length($b) <=> length($a) } keys %rules; + # Match repeatedly from the front. + 1 while ( $spec =~ s/($rule)/$rules{$1}/ ); + $spec =~ s/(^|\s)\s/$1/g; + return $spec; +} + +sub _behavior_builder { + my ( $mm_def, $behavior, $m_info ) = @_; + + # We're going to have to do some extra work here, so we'll cache the result + my $builder; + + # Separate the modifiers + my $core_behavior = $behavior; + my @modifiers; + while ( $core_behavior =~ s/\-(\w+)\s// ) { push @modifiers, $1 } + + # Find either the built-in or universal behavior template + if ( $mm_def->{'behavior'}{$core_behavior} ) { + $builder = $mm_def->{'behavior'}{$core_behavior}; + } else { + my $universal = _definition('Class::MakeMethods::Template::Universal','generic'); + $builder = $universal->{'behavior'}{$core_behavior} + } + + # Otherwise we're hosed. + $builder or _diagnostic('make_bad_behavior', $m_info->{'name'}, $behavior); + + if ( ! ref $builder ) { + # If we've got a text template, pass it off for interpretation. + my $code = ( ! $Class::MakeMethods::Utility::DiskCache::DiskCacheDir ) ? + _interpret_text_builder($mm_def, $core_behavior, $builder, @modifiers) + : _disk_cache_builder($mm_def, $core_behavior, $builder, @modifiers); + + # _diagnostic('debug_eval_builder', $name, $code); + local $^W unless $Class::MakeMethods::CONTEXT{Debug}; + $builder = eval $code; + if ( $@ ) { _diagnostic('behavior_eval', $@, $code) } + unless (ref $builder eq 'CODE') { _diagnostic('behavior_eval', $@, $code) } + + } elsif ( scalar @modifiers ) { + # Can't modify code subs + _diagnostic('make_behavior_mod', join(', ', @modifiers), $core_behavior); + } + + $mm_def->{'-behavior_cache'}{$behavior} = $builder; + + return $builder; +} + +######################################################################## +### CODE EXPRESSIONS: _interpret_text_builder(), _disk_cache_builder() +######################################################################## + +sub _interpret_text_builder { + require Class::MakeMethods::Utility::TextBuilder; + + my ( $mm_def, $name, $code, @modifiers ) = @_; + + foreach ( @modifiers ) { + exists $mm_def->{'modifier'}{$_} + or _diagnostic('behavior_mod_unknown', $name, $_); + } + + my @exprs = grep { $_ } map { + $mm_def->{'modifier'}{ $_ }, + $mm_def->{'modifier'}{ "$_ $name" } || $mm_def->{'modifier'}{ "$_ *" } + } ( '-all', ( scalar(@modifiers) ? @modifiers : '-default' ) ); + + # Generic method template + push @exprs, "return sub _SUB_ATTRIBS_ { \n my \$self = shift;\n * }"; + + # Closure-generator + push @exprs, "sub { my \$m_info = \$_[0]; * }"; + + my $exprs = $mm_def->{code_expr}; + unshift @exprs, { + ( map { $_=>$exprs->{$_} } grep /^[^-]/, keys %$exprs ), + '_BEHAVIOR_{}' => $mm_def->{'behavior'}, + '_SUB_ATTRIBS_' => '', + }; + + my $result = Class::MakeMethods::Utility::TextBuilder::text_builder($code, + @exprs); + + my $modifier_string = join(' ', map "-$_", @modifiers); + my $full_name = "$name ($mm_def->{template_class} $mm_def->{template_name}" . + ( $modifier_string ? " $modifier_string" : '' ) . ")"; + + _diagnostic('debug_template_builder', $full_name, $code, $result); + + return $result; +} + +sub _disk_cache_builder { + require Class::MakeMethods::Utility::DiskCache; + my ( $mm_def, $core_behavior, $builder, @modifiers ) = @_; + + Class::MakeMethods::Utility::DiskCache::disk_cache( + "$mm_def->{template_class}::$mm_def->{template_name}", + join('.', $core_behavior, @modifiers), + \&_interpret_text_builder, ($mm_def, $core_behavior, $builder, @modifiers) + ); +} + +1; + +__END__ + + +=head1 NAME + +Class::MakeMethods::Template - Extensible code templates + + +=head1 SYNOPSIS + + package MyObject; + use Class::MakeMethods::Template::Hash ( + 'new' => 'new', + 'string' => 'foo', + 'number' => 'bar', + ); + + my $obj = MyObject->new( foo => "Foozle", bar => 23 ); + print $obj->foo(); + $obj->bar(42); + + +=head1 MOTIVATION + +If you compare the source code of some of the closure-generating +methods provided by other subclasses of Class::MakeMethods, +such as the C accessors provided by the various Standard::* +subclasses, you will notice a fair amount of duplication. This +module provides a way of assembling common pieces of code to +facilitate support the maintenance of much larger libraries of +generated methods. + + +=head1 DESCRIPTION + +This module extends the Class::MakeMethods framework by providing +an abstract superclass for extensible code-templating method +generators. + +Common types of methods are generalized into B