libclass-makemethods-perl-1.01.orig/0000755000175000017500000000000010117425542016054 5ustar ericericlibclass-makemethods-perl-1.01.orig/benchmark.pl0000644000175000017500000000407210116225136020342 0ustar ericeric#!perl use strict; use ExtUtils::testlib; use Benchmark qw( cmpthese ); ######################################################################## my @generators = qw( Basic Standard Composite Evaled Template ); my @methods = map "method_$_", 1 .. 30; my %pckgno; my $cputime = shift(@ARGV) || 0; ######################################################################## print "\nComparing module load time (usage overhead)...\n"; my %inc = %INC; sub test_load { local %INC = %inc; my $f = "$_[0].pm"; $f =~ s{::}{/}g; require $f; } cmpthese( $cputime, { 'Superclass' => sub { test_load( "Class::MakeMethods" ) }, map { my $gen = $_; $gen => sub { test_load( "Class::MakeMethods::${gen}::Hash" ) } } @generators } ); ######################################################################## print "\nComparing method generation (startup duration)...\n"; cmpthese( $cputime, { 'inline' => sub { eval( join "\n", "package package_inline_" . ++ $pckgno{inline} . ";", 'sub new { my $class = shift; bless { @_ }, $class }', map "sub $_ { my \$self = shift; \@_ ? \$self->{$_} = shift : \$self->{$_} }", @methods ); }, map { my $gen = $_; $gen => sub { Class::MakeMethods->make( -MakerClass => $gen . "::Hash", -TargetClass => ( "package_${gen}_" . ++ $pckgno{$gen} ), 'new' => 'new', 'scalar' => \@methods ); } } @generators } ); ######################################################################## print "\nComparing method calling (runtime duration)...\n"; cmpthese( $cputime, { map { my $gen = $_; $gen => sub { my $instance = "package_${gen}_1"->new(); foreach ( 1 .. 5 ) { foreach my $method ( @methods ) { my $value = $instance->$method(); $instance->$method( $value ); $instance->$method(); } } } } keys %pckgno } ); ######################################################################## __END__ ######################################################################## date; perl -v | grep This; perl benchmark.pl | grep -v wallclock libclass-makemethods-perl-1.01.orig/CHANGES0000644000175000017500000005122410117155146017053 0ustar ericericNAME Class::MakeMethods::Docs::Changes - History of Class::MakeMethods SYNOPSIS Revision history for Class::MakeMethods. CHANGES Version 1.010 2004/09/01 Moved Template::TextBuilder and Template::DiskCache into Utility:: namespace. Added support for defaults with -- and -param to Standard get_declarations(). 2004/08/12 Began integrating tied-value patches from Dominique Dumont. 2004/04/27 Added method types to Evaled::Hash. 2004/04/23 Added skeleton of a new Evaled::Hash class. Version 1.009 2003/09/25 Added Emulator::mcoder and compatibility tests. Released to CPAN as Class-MakeMethods-1.009.tar.gz. 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. Version 1.008 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. 2003/09/02 Adjusted DESTROY behavior of Template::InsideOut and cleaned up documentation. Version 1.007 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. 2003/08/27 Added section to Class::MakeMethods/EXTENDING documentation based on question from Terrence Brannon. 2003/02/07 Fixed missing declaration of Template::Hash:instance, reported via RT. Version 1.006 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. 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. Version 1.005 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.) Version 1.004 2002/03/23 Released to CPAN as Class-MakeMethods-1.004.tar.gz. 2002/03/16 Allow double-colons between package name and method generator name. 2002/02/19 Fixed related use of undef in Standard::*:hash methods. 2002/02/14 Adjusted Standard::*:hash methods to avoid assuming that the hashref already exists. 2002/02/07 Added missing *_reset => clear to Template number --counter interface. 2002/02/02 Adjusted error message in Utility::ArraySplicer 2002/01/26 Applied small documentation corrections suggested by Adam Spiers. Added Standard::Universal:alias. Version 1.003 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. Please Note: 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... 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. Version 1.000.* v1.000.16 - 2002/01/21 Released to CPAN as v1.000.016. 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. 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. v1.000.15 - 2002/01/14 Released to CPAN as v1.000.015. 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 L<...>" 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). 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. v1.000.15 - 2001/07/04, 2001/07/19 Minor additions to documentation of various method types. v1.000.14 - 2001/07/01 Released as v1.000.014. 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. 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 the perlmodlib manpage 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. Early 1.000 versions 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.) 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. 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. 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. 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. 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. 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. 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". 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. 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. 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. 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. 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. 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 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. v0.9.3 - 2000/09/30 Refactored subclass_name and class_registry. Folded in some misc improvements from Class::MethodMaker 1.0. 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: 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. libclass-makemethods-perl-1.01.orig/Makefile.PL0000644000175000017500000000170710116213447020031 0ustar ericericuse ExtUtils::MakeMaker; ######################################################################## WriteMakefile( 'NAME' => 'Class::MakeMethods', 'VERSION_FROM' => 'MakeMethods.pm', 'PREREQ_PM' => {}, ($] >= 5.005 ? ( ABSTRACT_FROM => 'MakeMethods.pm', AUTHOR => 'Matthew Simon Cavalletto ', ) : ()), ); ######################################################################## sub MY::postamble { q{ again: realclean FORCE perl Makefile.PL; make pm_to_blib cleanmanifest: realclean FORCE rm MANIFEST ; perl Makefile.PL; touch MANIFEST; make manifest %.t: pm_to_blib FORCE make; perl -Iblib/lib $@ cover: FORCE cover -delete; HARNESS_PERL_SWITCHES=-MDevel::Cover make test; cover docs : README CHANGES README: MakeMethods/Docs/ReadMe.pod pod2text MakeMethods/Docs/ReadMe.pod > README CHANGES: MakeMethods/Docs/Changes.pod pod2text MakeMethods/Docs/Changes.pod > CHANGES }; } libclass-makemethods-perl-1.01.orig/MakeMethods/0000755000175000017500000000000010117425542020255 5ustar ericericlibclass-makemethods-perl-1.01.orig/MakeMethods/Attribute.pm0000644000175000017500000000762110117122502022552 0ustar ericericpackage 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 libclass-makemethods-perl-1.01.orig/MakeMethods/Autoload.pm0000644000175000017500000001323310117122502022353 0ustar ericericpackage 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 libclass-makemethods-perl-1.01.orig/MakeMethods/Basic/0000755000175000017500000000000010117425541021275 5ustar ericericlibclass-makemethods-perl-1.01.orig/MakeMethods/Basic/Array.pm0000644000175000017500000002404410117122502022704 0ustar ericeric=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; libclass-makemethods-perl-1.01.orig/MakeMethods/Basic/Global.pm0000644000175000017500000001561710117122502023034 0ustar ericeric=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; libclass-makemethods-perl-1.01.orig/MakeMethods/Basic/Hash.pm0000644000175000017500000002004610117122502022507 0ustar ericeric=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; libclass-makemethods-perl-1.01.orig/MakeMethods/Basic.pm0000644000175000017500000000466610117122502021636 0ustar ericericpackage 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 libclass-makemethods-perl-1.01.orig/MakeMethods/Composite/0000755000175000017500000000000010117425541022216 5ustar ericericlibclass-makemethods-perl-1.01.orig/MakeMethods/Composite/Array.pm0000644000175000017500000005154710117122502023635 0ustar ericeric=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; libclass-makemethods-perl-1.01.orig/MakeMethods/Composite/Global.pm0000644000175000017500000003574610117122502023762 0ustar ericeric=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; libclass-makemethods-perl-1.01.orig/MakeMethods/Composite/Hash.pm0000644000175000017500000004426310117122502023437 0ustar ericeric=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; libclass-makemethods-perl-1.01.orig/MakeMethods/Composite/Inheritable.pm0000644000175000017500000004341210117122502024775 0ustar ericeric=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; libclass-makemethods-perl-1.01.orig/MakeMethods/Composite/Universal.pm0000644000175000017500000000620510117122502024516 0ustar ericeric=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; libclass-makemethods-perl-1.01.orig/MakeMethods/Composite.pm0000644000175000017500000001266410117151061022556 0ustar ericeric=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; libclass-makemethods-perl-1.01.orig/MakeMethods/Docs/0000755000175000017500000000000010117425542021145 5ustar ericericlibclass-makemethods-perl-1.01.orig/MakeMethods/Docs/Catalog.pod0000644000175000017500000004321210117127026023221 0ustar ericeric=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 libclass-makemethods-perl-1.01.orig/MakeMethods/Docs/Changes.pod0000644000175000017500000004542610117425347023237 0ustar ericeric=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 libclass-makemethods-perl-1.01.orig/MakeMethods/Docs/Examples.pod0000644000175000017500000003752210117125140023427 0ustar ericeric=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 libclass-makemethods-perl-1.01.orig/MakeMethods/Docs/ReadMe.pod0000644000175000017500000001720010117125121022774 0ustar ericeric=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 libclass-makemethods-perl-1.01.orig/MakeMethods/Docs/RelatedModules.pod0000644000175000017500000003211110117122502024546 0ustar ericeric=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 libclass-makemethods-perl-1.01.orig/MakeMethods/Docs/ToDo.pod0000644000175000017500000001450110117122502022505 0ustar ericeric=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 libclass-makemethods-perl-1.01.orig/MakeMethods/Emulator/0000755000175000017500000000000010117425541022044 5ustar ericericlibclass-makemethods-perl-1.01.orig/MakeMethods/Emulator/AccessorFast.pm0000644000175000017500000000530410117122502024753 0ustar ericericpackage 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 libclass-makemethods-perl-1.01.orig/MakeMethods/Emulator/accessors.pm0000644000175000017500000000674410117122502024371 0ustar ericericpackage 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 libclass-makemethods-perl-1.01.orig/MakeMethods/Emulator/Inheritable.pm0000644000175000017500000001204210117122502024616 0ustar ericericpackage 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 libclass-makemethods-perl-1.01.orig/MakeMethods/Emulator/mcoder.pm0000644000175000017500000000751210117122502023647 0ustar ericericpackage 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 libclass-makemethods-perl-1.01.orig/MakeMethods/Emulator/MethodMaker.pm0000644000175000017500000004446410117122502024605 0ustar ericericpackage 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; libclass-makemethods-perl-1.01.orig/MakeMethods/Emulator/Singleton.pm0000644000175000017500000000573210117122502024342 0ustar ericericpackage 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 libclass-makemethods-perl-1.01.orig/MakeMethods/Emulator/Struct.pm0000644000175000017500000000750610117122502023665 0ustar ericericpackage 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 libclass-makemethods-perl-1.01.orig/MakeMethods/Emulator.pm0000644000175000017500000001170710117122502022377 0ustar ericericpackage 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 libclass-makemethods-perl-1.01.orig/MakeMethods/Evaled/0000755000175000017500000000000010117425542021455 5ustar ericericlibclass-makemethods-perl-1.01.orig/MakeMethods/Evaled/Hash.pm0000644000175000017500000001742310117122502022673 0ustar ericeric=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; libclass-makemethods-perl-1.01.orig/MakeMethods/Evaled.pm0000644000175000017500000000462410117137137022021 0ustar ericeric=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; libclass-makemethods-perl-1.01.orig/MakeMethods/Standard/0000755000175000017500000000000010117425541022014 5ustar ericericlibclass-makemethods-perl-1.01.orig/MakeMethods/Standard/Array.pm0000644000175000017500000003626610117136760023447 0ustar ericeric=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; libclass-makemethods-perl-1.01.orig/MakeMethods/Standard/Global.pm0000644000175000017500000002451410117136762023564 0ustar ericeric=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; libclass-makemethods-perl-1.01.orig/MakeMethods/Standard/Hash.pm0000644000175000017500000003237010117136762023246 0ustar ericeric=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; libclass-makemethods-perl-1.01.orig/MakeMethods/Standard/Inheritable.pm0000644000175000017500000003266110117136763024615 0ustar ericeric=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; libclass-makemethods-perl-1.01.orig/MakeMethods/Standard/Universal.pm0000644000175000017500000002010410117136745024324 0ustar ericeric=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; libclass-makemethods-perl-1.01.orig/MakeMethods/Standard.pm0000644000175000017500000000343710117147067022365 0ustar ericeric=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; libclass-makemethods-perl-1.01.orig/MakeMethods/Template/0000755000175000017500000000000010117425541022027 5ustar ericericlibclass-makemethods-perl-1.01.orig/MakeMethods/Template/Array.pm0000644000175000017500000000456310117147706023457 0ustar ericericpackage Class::MakeMethods::Template::Array; use Class::MakeMethods::Template::Generic '-isasubclass'; $VERSION = 1.008; use strict; require 5.00; use Carp; =head1 NAME Class::MakeMethods::Template::Array - Methods for manipulating positional values in arrays =head1 SYNOPSIS =head1 DESCRIPTION =cut use vars qw( %ClassInfo ); sub generic { { 'params' => { 'array_index' => undef, }, 'code_expr' => { _VALUE_ => '_SELF_->[_STATIC_ATTR_{array_index}]', '-import' => { 'Template::Generic:generic' => '*' }, _EMPTY_NEW_INSTANCE_ => 'bless [], _SELF_CLASS_', _SET_VALUES_FROM_HASH_ => 'while ( scalar @_ ) { local $_ = shift(); $self->[ _BFP_FROM_NAME_{ $_ } ] = shift() }' }, 'behavior' => { '-init' => sub { my $m_info = $_[0]; # If we're the first one, if ( ! $ClassInfo{$m_info->{target_class}} ) { # traverse inheritance hierarchy, looking for fields to inherit my @results; no strict 'refs'; my @sources = @{"$m_info->{target_class}\::ISA"}; while ( my $class = shift @sources ) { next unless exists $ClassInfo{ $class }; push @sources, @{"$class\::ISA"}; if ( scalar @results ) { Carp::croak "Too many inheritances of fields"; } push @results, @{$ClassInfo{$class}}; } $ClassInfo{$m_info->{target_class}} = \@sources; } my $class_info = $ClassInfo{$m_info->{target_class}}; if ( ! defined $m_info->{array_index} ) { foreach ( 0..$#$class_info ) { if ( $class_info->[$_] eq $m_info->{'name'} ) { $m_info->{array_index} = $_; last } } if ( ! defined $m_info->{array_index} ) { push @ $class_info, $m_info->{'name'}; $m_info->{array_index} = $#$class_info; } } return; }, }, } } ######################################################################## =head2 Standard Methods The following methods from Generic should be supported: scalar string number boolean bits (?) array hash tiedhash (?) hash_of_arrays (?) object instance array_of_objects (?) code code_or_scalar (?) See L for the interfaces and behaviors of these method types. The items marked with a ? above have not been tested sufficiently; please inform the author if they do not function as you would expect. =cut ######################################################################## 1; libclass-makemethods-perl-1.01.orig/MakeMethods/Template/Class.pm0000644000175000017500000000404610117147674023446 0ustar ericericpackage Class::MakeMethods::Template::Class; use Class::MakeMethods::Template::Generic '-isasubclass'; $VERSION = 1.008; use strict; require 5.0; use Carp; =head1 NAME Class::MakeMethods::Template::Class - Associate information with a package =head1 SYNOPSIS package MyObject; use Class::MakeMethods::Template::Class ( scalar => [ 'foo' ] ); package main; MyObject->foo('bar') print MyObject->foo(); =head1 DESCRIPTION These meta-methods provide access to class-specific values. They are similar to Static, except that each subclass has separate values. =cut sub generic { { '-import' => { 'Template::Generic:generic' => '*' }, 'modifier' => { }, 'code_expr' => { '_VALUE_' => '_ATTR_{data}->{_SELF_CLASS_}', }, } } ######################################################################## =head2 Class:scalar Creates methods to handle a scalar variable in the declaring package. See the documentation on C for interfaces and behaviors. =cut ######################################################################## =head2 Class:array Creates methods to handle a array variable in the declaring package. See the documentation on C for interfaces and behaviors. =cut sub array { { '-import' => { 'Template::Generic:array' => '*', }, 'modifier' => { '-all' => q{ _REF_VALUE_ or @{_ATTR_{data}->{_SELF_CLASS_}} = (); * }, }, 'code_expr' => { '_VALUE_' => '\@{_ATTR_{data}->{_SELF_CLASS_}}', }, } } ######################################################################## =head2 Class:hash Creates methods to handle a hash variable in the declaring package. See the documentation on C for interfaces and behaviors. =cut sub hash { { '-import' => { 'Template::Generic:hash' => '*', }, 'modifier' => { '-all' => q{ _REF_VALUE_ or %{_ATTR_{data}->{_SELF_CLASS_}} = (); * }, }, 'code_expr' => { '_VALUE_' => '\%{_ATTR_{data}->{_SELF_CLASS_}}', }, } } 1; libclass-makemethods-perl-1.01.orig/MakeMethods/Template/ClassInherit.pm0000644000175000017500000000717210117147672024772 0ustar ericeric=head1 NAME Class::MakeMethods::Template::ClassInherit - Overridable class data =head1 SYNOPSIS package MyClass; use Class::MakeMethods( 'Template::ClassInherit:scalar' => 'foo' ); # We now have an accessor method for an "inheritable" scalar value package main; MyClass->foo( 'Foozle' ); # Set a class-wide value print MyClass->foo(); # Retrieve class-wide value ... package MySubClass; @ISA = 'MyClass'; print MySubClass->foo(); # Intially same as superclass, MySubClass->foo('Foobar'); # but overridable per subclass/ =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, and optionally override it in a subclass. =cut ######################################################################## package Class::MakeMethods::Template::ClassInherit; use Class::MakeMethods::Template::Generic '-isasubclass'; $VERSION = 1.008; use strict; require 5.0; use Carp; sub generic { { '-import' => { 'Template::Generic:generic' => '*' }, 'modifier' => { '-all' => [ q{ _INIT_VALUE_CLASS_ * } ], }, 'code_expr' => { '_VALUE_CLASS_' => '$_value_class', '_INIT_VALUE_CLASS_' => q{ my _VALUE_CLASS_; for ( my @_INC_search = _SELF_CLASS_; scalar @_INC_search; ) { _VALUE_CLASS_ = shift @_INC_search; last if ( exists _ATTR_{data}->{_VALUE_CLASS_} ); no strict 'refs'; unshift @_INC_search, @{"_VALUE_CLASS_\::ISA"}; } }, '_VALUE_' => '_ATTR_{data}->{_VALUE_CLASS_}', '_GET_VALUE_' => q{ _ATTR_{data}->{_VALUE_CLASS_} }, '_SET_VALUE_{}' => q{ ( _VALUE_CLASS_ = _SELF_CLASS_ and _ATTR_{data}->{_VALUE_CLASS_} = * ) }, }, } } ######################################################################## =head2 Standard Methods The following methods from Generic should all be supported: scalar string string_index (?) number boolean bits (?) array (*) hash (*) tiedhash (?) hash_of_arrays (?) object (?) instance (?) array_of_objects (?) code (?) code_or_scalar (?) See L for the interfaces and behaviors of these method types. The items marked with a * above are specifically defined in this package, whereas the others are formed automatically by the interaction of this package's generic settings with the code templates provided by the Generic superclass. The items marked with a ? above have not been tested sufficiently; please inform the author if they do not function as you would expect. =cut sub array { { '-import' => { 'Template::Generic:array' => '*', }, 'modifier' => { '-all' => [ q{ _VALUE_ ||= []; * } ], }, 'code_expr' => { '_VALUE_' => '\@{_ATTR_{data}->{_SELF_CLASS_}}', }, } } sub hash { { '-import' => { 'Template::Generic:hash' => '*', }, 'modifier' => { '-all' => [ q{ _VALUE_ ||= {}; * } ], }, 'code_expr' => { '_VALUE_' => '\%{_ATTR_{data}->{_SELF_CLASS_}}', }, } } ######################################################################## =head1 SEE ALSO See L for general information about this distribution. See L for more about this family of subclasses. See L for information about the various accessor interfaces subclassed herein. If you just need scalar accessors, see L for a very elegant and efficient implementation. =cut ######################################################################## 1; libclass-makemethods-perl-1.01.orig/MakeMethods/Template/ClassName.pm0000644000175000017500000002013110117122502024217 0ustar ericericpackage Class::MakeMethods::Template::ClassName; use Class::MakeMethods::Template '-isasubclass'; $VERSION = 1.008; sub _diagnostic { &Class::MakeMethods::_diagnostic } ######################################################################## ###### CLASS NAME MANIPULATIONS ######################################################################## =head1 NAME Class::MakeMethods::Template::ClassName - Access object's class =head1 SYNOPSIS package MyObject; use Class::MakeMethods::Template::ClassName ( subclass_name => [ 'type' ] ); ... package main; my $object = MyObject->new; $object->type('Foo') # reblesses object to MyObject::Foo subclass print $object->type(); # prints "Foo". =head1 DESCRIPTION These method types access or change information about the class an object is associated with. =head2 class_name Called without arguments, returns the class name. If called with an argument, reblesses object into that class. If the class doesn't already exist, it will be created. =head2 subclass_name Called without arguments, returns the subclass name. If called with an argument, reblesses object into that subclass. If the subclass doesn't already exist, it will be created. The subclass name is written as follows: =over 4 =item * if it's the original, defining class: empty =item * if its a a package within the namespace of the original: the distingushing name within that namespace, without leading C<::> =item * if it's a package elsewhere: the full name with leading C<::> =back =cut # $subclass = _pack_subclass( $base, $pckg ); sub _pack_subclass { my $base = shift; my $pckg = shift; ( $pckg eq $base ) ? '' : ( $pckg =~ s/^\Q$base\E\:\:// ) ? $pckg : "::$pckg"; } # $pckg = _unpack_subclass( $base, $subclass ); sub _unpack_subclass { my $base = shift; my $subclass = shift; ! $subclass ? $base : ( $subclass =~ s/^::// ) ? $subclass : "$base\::$subclass"; } # $pckg = _require_class( $package ); sub _require_class { my $package = shift; no strict 'refs'; unless ( @{$package . '::ISA'} ) { (my $file = $package . '.pm' ) =~ s|::|/|go; local $SIG{__DIE__} = sub { die @_ }; # warn "Auto-requiring package $package \n"; eval { require $file }; if ( $@ ) { _diagnostic('mm_package_fail', $package, $@) } } return $package; } # $pckg = _provide_class( $base, $package ); sub _provide_class { my $base = shift; my $package = shift; # If the subclass hasn't been created yet, do so. no strict 'refs'; unless ( scalar @{$package . '::ISA'} ) { # warn "Auto-vivifying $base subclass $package\n"; @{$package . '::ISA'} = ( $base ); } return $package; } sub class_name { { 'interface' => { default => 'autocreate', autocreate => { '*'=>'autocreate' }, require => { '*'=>'require' }, }, 'behavior' => { 'autocreate' => q{ if ( ! scalar @_ ) { _CLASS_GET_ } else { _CLASS_PROVIDE_ } }, 'require' => q{ if ( ! scalar @_ ) { _CLASS_GET_ } else { _CLASS_REQUIRE_ } }, }, 'code_expr' => { _CLASS_GET_ => q{ my $class = ref $self || $self; }, _CLASS_REQUIRE_ => q{ my $class = Class::MakeMethods::Template::ClassName::_require_class( shift() ); _BLESS_AND_RETURN_ }, _CLASS_PROVIDE_ => q{ my $class = Class::MakeMethods::Template::ClassName::_provide_class( $m_info->{'target_class'}, shift() ); _BLESS_AND_RETURN_ }, _BLESS_AND_RETURN_ => q{ bless $self, $class if ( ref $self ); return $class; }, }, } } sub subclass_name { { '-import' => { 'Template::ClassName:class_name' => '*', }, 'code_expr' => { _CLASS_GET_ => q{ my $class = ref $self || $self; Class::MakeMethods::Template::ClassName::_pack_subclass( $m_info->{'target_class'}, $class ) }, _CLASS_REQUIRE_ => q{ my $subclass = Class::MakeMethods::Template::ClassName::_unpack_subclass( $m_info->{'target_class'}, shift() ); my $class = Class::MakeMethods::Template::ClassName::_require_class($subclass); _BLESS_AND_RETURN_ }, _CLASS_PROVIDE_ => q{ my $subclass = Class::MakeMethods::Template::ClassName::_unpack_subclass( $m_info->{'target_class'}, shift() ); my $class = Class::MakeMethods::Template::ClassName::_provide_class( $m_info->{'target_class'}, $subclass ); _BLESS_AND_RETURN_ }, }, } } ######################################################################## ### CLASS_REGISTRY =head2 static_hash_classname Provides a shared hash mapping keys to class names. class_registry => [ qw/ foo / ] Takes a single string or a reference to an array of strings as its argument. For each string, creates a new anonymous hash and associated accessor methods that will map scalar values to classes in the calling package's subclass hiearchy. The accessor methods provide an interface to the hash as illustrated below. Note that several of these functions operate quite differently depending on the number of arguments passed, or the context in which they are called. =over 4 =item @indexes = $class_or_ref->x; Returns the scalar values that are indexes associated with this class, or the class of this object. =item $class = $class_or_ref->x( $index ); Returns the class name associated with the provided index value. =item @classes = $class_or_ref->x( @indexes ); Returns the associated classes for each index in order. =item @all_indexes = $class_or_ref->x_keys; Returns a list of the indexes defined for this registry. =item @all_classes = $class_or_ref->x_values; Returns a list of the classes associated with this registry. =item @all_classes = $class_or_ref->unique_x_values; Returns a list of the classes associated with this registry, with no more than one occurance of any value. =item %mapping = $class_or_ref->x_hash; Return the key-value pairs used to store this attribute =item $mapping_ref = $class_or_ref->x_hash; Returns a reference to the hash used for the mapping. =item $class_or_ref->add_x( @indexes ); Adds an entry in the hash for each of the provided indexes, mapping it to this class, or the class of this object. =item $class_or_ref->clear_x; Removes those entries from the hash whose values are this class, or the class of this object. =item $class_or_ref->clear_xs( @indexes ); Remove all entries from the hash. =back =cut sub static_hash_classname { { '-import' => { 'Template::Static:hash' => '*', }, 'params' => { 'instance' => {} }, 'interface' => { default => { '*'=>'get_classname', 'add_*'=>'add_classname', 'clear_*'=>'drop_classname', '*_keys'=>'keys', '*_hash'=>'get', '*_values'=>'values', 'clear_*s'=>'clear', 'unique_*_values'=>'unique_values', }, }, 'behavior' => { 'get_classname' => sub { my $m_info = $_[0]; sub { my $self = shift; my $class = ( ref($self) || $self ); defined $m_info->{'instance'} or $m_info->{'instance'} = {}; my $hash = $m_info->{'instance'}; if ( ! scalar @_ ) { my @keys = grep { $hash->{$_} eq $class } keys %$hash; return wantarray ? @keys : $keys[0]; } elsif (scalar @_ == 1) { return $hash->{ shift() }; } else { return @{$hash}{ @_ }; } }}, 'add_classname' => sub { my $m_info = $_[0]; sub { my $self = shift; my $class = ( ref($self) || $self ); defined $m_info->{'instance'} or $m_info->{'instance'} = {}; my $hash = $m_info->{'instance'}; foreach ( @_ ) { $hash->{$_} = $class } }}, 'drop_classname' => sub { my $m_info = $_[0]; sub { my $self = shift; my $class = ( ref($self) || $self ); defined $m_info->{'instance'} or $m_info->{'instance'} = {}; my $hash = $m_info->{'instance'}; foreach ( grep { $hash->{$_} eq $class } keys %$hash ){ delete $hash{$_} } }}, }, } } ######################################################################## =head1 SEE ALSO See L for general information about this distribution. See L for information about this family of subclasses. =cut 1; libclass-makemethods-perl-1.01.orig/MakeMethods/Template/ClassVar.pm0000644000175000017500000001026610117147657024121 0ustar ericericpackage Class::MakeMethods::Template::ClassVar; use Class::MakeMethods::Template::Generic '-isasubclass'; $VERSION = 1.008; use strict; require 5.0; use Carp; =head1 NAME Class::MakeMethods::Template::ClassVar - Static methods with subclass variation =head1 SYNOPSIS package MyObject; use Class::MakeMethods::Template::ClassVar ( scalar => [ 'foo' ] ); package main; MyObject->foo('bar') print MyObject->foo(); $MyObject::foo = 'bazillion'; print MyObject->foo(); =head1 DESCRIPTION These meta-methods provide access to package (class global) variables, with the package determined at run-time. This is basically the same as the PackageVar meta-methods, except that PackageVar methods find the named variable in the package that defines the method, while ClassVar methods use the package the object is blessed into. As a result, subclasses will each store a distinct value for a ClassVar method, but will share the same value for a PackageVar or Static method. B: The following parameters are defined for ClassVar meta-methods. =over 4 =item variable The name of the variable to store the value in. Defaults to the same name as the method. =back =cut sub generic { { '-import' => { 'Template::Generic:generic' => '*' }, 'params' => { 'variable' => '*' }, 'modifier' => { '-all' => [ q{ no strict; * } ], }, 'code_expr' => { '_VALUE_' => '${_SELF_CLASS_."::"._ATTR_{variable}}', }, } } ######################################################################## =head2 Standard Methods The following methods from Generic should all be supported: scalar string string_index (?) number boolean bits (?) array (*) hash (*) tiedhash (?) hash_of_arrays (?) object (?) instance (?) array_of_objects (?) code (?) code_or_scalar (?) See L for the interfaces and behaviors of these method types. The items marked with a * above are specifically defined in this package, whereas the others are formed automatically by the interaction of this package's generic settings with the code templates provided by the Generic superclass. The items marked with a ? above have not been tested sufficiently; please inform the author if they do not function as you would expect. =cut ######################################################################## sub array { { '-import' => { 'Template::Generic:array' => '*', }, 'modifier' => { '-all' => q{ no strict; _ENSURE_REF_VALUE_; * }, }, 'code_expr' => { '_ENSURE_REF_VALUE_' => q{ _REF_VALUE_ or @{_SELF_CLASS_."::"._ATTR_{variable}} = (); }, '_VALUE_' => '(\@{_SELF_CLASS_."::"._ATTR_{variable}})', }, } } ######################################################################## sub hash { { '-import' => { 'Template::Generic:hash' => '*', }, 'modifier' => { '-all' => q{ no strict; _ENSURE_REF_VALUE_; * }, }, 'code_expr' => { '_ENSURE_REF_VALUE_' => q{ _REF_VALUE_ or %{_SELF_CLASS_."::"._ATTR_{variable}} = (); }, '_VALUE_' => '(\%{_SELF_CLASS_."::"._ATTR_{variable}})', }, } } ######################################################################## =head2 vars This rewrite rule converts package variable names into ClassVar methods of the equivalent data type. Here's an example declaration: package MyClass; use Class::MakeMethods::Template::ClassVar ( vars => '$VERSION @ISA' ); MyClass now has methods that get and set the contents of its $MyClass::VERSION and @MyClass::ISA package variables: MyClass->VERSION( 2.4 ); MyClass->push_ISA( 'Exporter' ); Subclasses can use these methods to adjust their own variables: package MySubclass; MySubclass->MyClass::push_ISA( 'MyClass' ); MySubclass->VERSION( 1.0 ); =cut sub vars { my $mm_class = shift; my @rewrite = map [ "Template::ClassVar:$_" ], qw( scalar array hash ); my %rewrite = ( '$' => 0, '@' => 1, '%' => 2 ); while (@_) { my $name = shift; my $data = shift; $data =~ s/\A(.)//; push @{ $rewrite[ $rewrite{ $1 } ] }, { 'name'=>$name, 'variable'=>$data }; } return @rewrite; } 1; libclass-makemethods-perl-1.01.orig/MakeMethods/Template/Flyweight.pm0000644000175000017500000000224410117147760024335 0ustar ericericpackage Class::MakeMethods::Template::Flyweight; use Class::MakeMethods::Template::InsideOut '-isasubclass'; $VERSION = 1.008; sub new { { '-import' => { 'Template::Scalar:new' => '*' } } } 1; __END__ =head1 NAME Class::MakeMethods::Template::Flyweight - Deprecated name for InsideOut =head1 SYNOPSIS package MyObject; use Class::MakeMethods::Template::InsideOut ( new => [ 'new' ] scalar => [ 'foo', 'bar' ] ); package main; my $obj = MyObject->new( foo => "Foozle", bar => "Bozzle" ); print $obj->foo(); # Prints Foozle $obj->bar("Bamboozle"); # Sets $obj->{bar} =head1 DESCRIPTION Earlier versions of this package included a package named Class::MakeMethods::Template::Flyweight. However, in hindsight, this name was poorly chosen, as it suggests that the Flyweight object design pattern is being used, when the functionality is more akin to what's sometimes known as "inside-out objects." This functionality is now provided by Class::MakeMethods::Template::InsideOut, of which this is an almost-empty subclass retained to provide backwards compatibility. =head1 SEE ALSO L. =cutlibclass-makemethods-perl-1.01.orig/MakeMethods/Template/Generic.pm0000644000175000017500000016270010117122502023736 0ustar ericeric=head1 NAME Class::MakeMethods::Template::Generic - Templates for common meta-method types =head1 SYNOPSIS package MyObject; use Class::MakeMethods ( 'Template::Hash:new' => [ 'new' ], 'Template::Hash:scalar' => [ 'foo' ] 'Template::Static:scalar' => [ 'bar' ] ); package main; my $obj = MyObject->new( foo => "Foozle", bar => "Bozzle" ); print $obj->foo(); $obj->bar("Bamboozle"); =head1 DESCRIPTION This package provides a variety of abstract interfaces for constructors and accessor methods, which form a common foundation for meta-methods provided by the Hash, Scalar, Flyweight, Static, PackageVar, and ClassVar implementations. Generally speaking, the Generic meta-methods define calling interfaces and behaviors which are bound to differently scoped data by each of those subclasses. =cut ######################################################################## package Class::MakeMethods::Template::Generic; use Class::MakeMethods::Template '-isasubclass'; $VERSION = 1.008; use strict; use Carp; # use AutoLoader 'AUTOLOAD'; ######################################################################## sub generic { { 'params' => { }, 'modifier' => { '-import' => { 'Template::Universal:generic' => '*' }, }, 'code_expr' => { '-import' => { 'Template::Universal:generic' => '*' }, '_VALUE_' => undef, '_REF_VALUE_' => q{ _VALUE_ }, '_GET_VALUE_' => q{ _VALUE_ }, '_SET_VALUE_{}' => q{ ( _VALUE_ = * ) }, '_PROTECTED_SET_VALUE_{}' => q{ (_ACCESS_PROTECTED_ and _SET_VALUE_{*}) }, '_PRIVATE_SET_VALUE_{}' => q{ ( _ACCESS_PRIVATE_ and _SET_VALUE_{*} ) }, }, } } # 1; # __END__ ######################################################################## =head2 new Constructor There are several types of hash-based object constructors to choose from. Each of these methods creates and returns a reference to a new blessed instance. They differ in how their (optional) arguments are interpreted to set initial values, and in how they operate when called as class or instance methods. B: The following interfaces are supported. =over 4 =item -with_values, Provides the with_values behavior. =item -with_init Provides the with_init behavior. =item -with_methods Provides the with_methods behavior. =item -new_and_init Provides the with_init behavior for I<*>, and the general purpose method_init behavior as an init method. =item -copy_with_values Provides the copy behavior. =back B: The following types of constructor methods are available. =over 4 =item with_values Creates and blesses a new instance. If arguments are passed they are included in the instance, otherwise it will be empty. Returns the new instance. May be called as a class or instance method. =item with_methods Creates, blesses, and returns a new instance. The arguments are treated as a hash of method-name/argument-value pairs, with each such pair causing a call C<$self-Ename($value)>. =item with_init Creates and blesses a new instance, then calls a method named C, passing along any arguments that were initially given. Returns the new instance. The I() method should be defined in the class declaring these methods. May be called as a class or instance method. =item and_then_init Creates a new instance using method-name/argument-value pairs, like C, but then calls a method named C before returning the new object. The C method does not receive any arguments. The I() method should be defined in the class declaring these methods. =item instance_with_methods If called as a class method, creates, blesses, and returns a new instance. If called as an object method, operates on and returns the existing instance. Accepts name-value pair arguments, or a reference to hash of such pairs, and calls the named method for each with the supplied value as a single argument. (See the Universal method_init behavior for more discussion of this pattern.) =item copy_with values Produce a copy of an instance. Can not be called as a class method. The copy is a *shallow* copy; any references will be shared by the instance upon which the method is called and the returned newborn. If a list of key-value pairs is passed as arguments to the method, they are added to the copy, overwriting any values with the same key that may have been copied from the original. =item copy_with_methods Produce a copy of an instance. Can not be called as a class method. The copy is a *shallow* copy; any references will be shared by the instance upon which the method is called and the returned newborn. Accepts name-value pair arguments, or a reference to hash of such pairs, and calls the named method on the copy for each with the supplied value as a single argument before the copy is returned. =item copy_instance_with_values If called as a class method, creates, blesses, and returns a new instance. If called as an object method, produces and returns a copy of an instance. The copy is a *shallow* copy; any references will be shared by the instance upon which the method is called and the returned newborn. If a list of key-value pairs is passed as arguments to the method, they are added to the copy, overwriting any values with the same key that may have been copied from the original. =item copy_instance_with_methods If called as a class method, creates, blesses, and returns a new instance. If called as an object method, produces and returns a copy of an instance. The copy is a *shallow* copy; any references will be shared by the instance upon which the method is called and the returned newborn. Accepts name-value pair arguments, or a reference to hash of such pairs, and calls the named method on the copy for each with the supplied value as a single argument before the copy is returned. =back B: The following parameters are supported: =over 4 =item init_method The name of the method to call after creating a new instance. Defaults to 'init'. =back =cut sub new { { '-import' => { # 'Template::Generic:generic' => '*', }, 'interface' => { default => 'with_methods', with_values => 'with_values', with_methods => 'with_methods', with_init => 'with_init', and_then_init => 'and_then_init', new_and_init => { '*'=>'new_with_init', 'init'=>'method_init'}, instance_with_methods => 'instance_with_methods', copy => 'copy_with_values', copy_with_values => 'copy_with_values', copy_with_methods => 'copy_with_methods', copy_instance_with_values => 'copy_instance_with_values', copy_instance_with_methods => 'copy_instance_with_methods', }, 'behavior' => { 'with_methods' => q{ $self = _EMPTY_NEW_INSTANCE_; _CALL_METHODS_FROM_HASH_ return $self; }, 'with_values' => q{ $self = _EMPTY_NEW_INSTANCE_; _SET_VALUES_FROM_HASH_ return $self; }, 'with_init' => q{ $self = _EMPTY_NEW_INSTANCE_; my $init_method = $m_info->{'init_method'} || 'init'; $self->$init_method( @_ ); return $self; }, 'and_then_init' => q{ $self = _EMPTY_NEW_INSTANCE_; _CALL_METHODS_FROM_HASH_ my $init_method = $m_info->{'init_method'} || 'init'; $self->$init_method(); return $self; }, 'instance_with_methods' => q{ $self = ref ($self) ? $self : _EMPTY_NEW_INSTANCE_; _CALL_METHODS_FROM_HASH_ return $self; }, 'copy_with_values' => q{ @_ = ( %$self, @_ ); $self = _EMPTY_NEW_INSTANCE_; _SET_VALUES_FROM_HASH_ return $self; }, 'copy_with_methods' => q{ @_ = ( %$self, @_ ); $self = _EMPTY_NEW_INSTANCE_; _CALL_METHODS_FROM_HASH_ return $self; }, 'copy_instance_with_values' => q{ $self = bless { ( ref $self ? %$self : () ) }, _SELF_CLASS_; _SET_VALUES_FROM_HASH_ return $self; }, 'copy_instance_with_methods' => q{ $self = bless { ref $self ? %$self : () }, _SELF_CLASS_; _CALL_METHODS_FROM_HASH_ return $self; }, }, } } ######################################################################## =head2 scalar Accessor A generic scalar-value accessor meta-method which serves as an abstraction for basic "get_set" methods and numerous related interfaces use Class::MakeMethods -MakerClass => "...", scalar => [ 'foo', 'bar' ]; ... $self->foo( 'my new foo value' ); print $self->foo(); (Note that while you can use the scalar methods to store references to various data structures, there are other meta-methods defined below that may be more useful for managing references to arrays, hashes, and objects.) B: The following calling interfaces are available. =over 4 =item get_set (default) Provides get_set method for I<*>. Example: Create method foo, which sets the value of 'foo' for this instance if an argument is passed in, and then returns the value whether or not it's been changed: use Class::MakeMethods -MakerClass => "...", scalar => [ 'foo' ]; =item get_protected_set Provides an get_set accessor for I<*> that croaks if a new value is passed in from a package that is not a subclass of the declaring one. =item get_private_set Provides an get_set accessor for I<*> that croaks if a new value is passed in from a package other than the declaring one. =item read_only Provides an accessor for I<*> that does not modify its value. (Its initial value would have to be set by some other means.) =item eiffel Provides get behavior as I<*>, and set behavior as set_I<*>. Example: Create methods bar which returns the value of 'bar' for this instance (takes no arguments), and set_bar, which sets the value of 'bar' (no return): use Class::MakeMethods -MakerClass => "...", scalar => [ --eiffel => 'bar' ]; =item java Provides get behavior as getI<*>, and set behavior as setI<*>. Example: Create methods getBaz which returns the value of 'Baz' for this instance (takes no arguments), and setBaz, which sets the value for this instance (no return): use Class::MakeMethods -MakerClass => "...", scalar => [ --java => 'Baz' ]; =item init_and_get Creates methods which cache their results in a hash key. Provides the get_init behavior for I<*>, and an delete behavior for clear_I<*>. Specifies default value for init_method parameter of init_I<*>. =item with_clear Provides get_set behavior for I<*>, and a clear_I<*> method. =back B: The following types of accessor methods are available. =over 4 =item get_set If no argument is provided, returns the value of the current instance. The value defaults to undef. If an argument is provided, it is stored as the value of the current instance (even if the argument is undef), and that value is returned. Also available as get_protected_set and get_private_set, which are available for public read-only access, but have access control limitations. =item get Returns the value from the current instance. =item set Sets the value for the current instance. If called with no arguments, the value is set to undef. Does not return a value. =item clear Sets value to undef. =item get_set_chain Like get_set, but if called with an argument, returns the object it was called on. This allows a series of mutators to be called as follows: package MyObject; use Class::MakeMethods ( 'Template::Hash:scalar --get_set_chain' => 'foo bar baz' ); ... my $obj = MyObject->new->foo('Foozle'); $obj->bar("none")->baz("Brazil"); print $obj->foo, $obj->bar, $obj->baz; =item get_set_prev Like get_set, but if called with an argument, returns the previous value before it was changed to the new one. =item get_init If the value is currently undefined, calls the init_method. Returns the value. =back B: The following parameters are supported: =over 4 =item init_method The name of a method to be called to initialize this meta-method. Only used by the get_init behavior. =back =cut sub scalar { { '-import' => { 'Template::Generic:generic' => '*' }, 'interface' => { default => 'get_set', get_set => { '*'=>'get_set' }, noclear => { '*'=>'get_set' }, with_clear => { '*'=>'get_set', 'clear_*'=>'clear' }, read_only => { '*'=>'get' }, get_private_set => 'get_private_set', get_protected_set => 'get_protected_set', eiffel => { '*'=>'get', 'set_*'=>'set_return' }, java => { 'get*'=>'get', 'set*'=>'set_return' }, init_and_get => { '*'=>'get_init', -params=>{ init_method=>'init_*' } }, }, 'behavior' => { 'get' => q{ _GET_VALUE_ }, 'set' => q{ _SET_VALUE_{ shift() } }, 'set_return' => q{ _BEHAVIOR_{set}; return }, 'clear' => q{ _SET_VALUE_{ undef } }, 'defined' => q{ defined _VALUE_ }, 'get_set' => q { if ( scalar @_ ) { _BEHAVIOR_{set} } else { _BEHAVIOR_{get} } }, 'get_set_chain' => q { if ( scalar @_ ) { _BEHAVIOR_{set}; return _SELF_ } else { _BEHAVIOR_{get} } }, 'get_set_prev' => q { my $value = _BEHAVIOR_{get}; if ( scalar @_ ) { _BEHAVIOR_{set}; } return $value; }, 'get_private_set' => q{ if ( scalar @_ ) { _PRIVATE_SET_VALUE_{ shift() } } else { _BEHAVIOR_{get} } }, 'get_protected_set' => q{ if ( scalar @_ ) { _PROTECTED_SET_VALUE_{ shift() } } else { _BEHAVIOR_{get} } }, 'get_init' => q{ if ( ! defined _VALUE_ ) { my $init_method = _ATTR_REQUIRED_{'init_method'}; _SET_VALUE_{ _SELF_->$init_method( @_ ) }; } else { _BEHAVIOR_{get} } }, }, 'params' => { new_method => 'new' }, } } ######################################################################## =head2 string Accessor A generic scalar-value accessor meta-method which serves as an abstraction for basic "get_set" methods and numerous related interfaces use Class::MakeMethods -MakerClass => "...", string => [ 'foo', 'bar' ]; ... $self->foo( 'my new foo value' ); print $self->foo(); This meta-method extends the scalar meta-method, and supports the same interfaces and parameters. However, it generally treats values as strings, and can not be used to store references. B: In addition to those provided by C, the following calling interfaces are available. =over 4 =item -get_concat Provides the get_concat behavior for I<*>, and a clear_I<*> method. Example: use Class::MakeMethods get_concat => { name => 'words', join => ", " }; $obj->words('foo'); $obj->words('bar'); $obj->words() eq 'foo, bar'; =back B: In addition to those provided by C, the following types of accessor methods are available. =over 4 =item concat Concatenates the argument value with the existing value. =item get_concat Like get_set except sets do not clear out the original value, but instead concatenate the new value to the existing one. =back B: In addition to those provided by C, the following parameters are supported. =over 4 =item join If the join parameter is defined, each time the get_concat behavior is invoked, it will glue its argument onto any existing value with the join string as the separator. The join field is applied I values, not prior to the first or after the last. Defaults to undefined =back =cut sub string { { '-import' => { 'Template::Generic:scalar' => '*' }, 'interface' => { get_concat => { '*'=>'get_concat', 'clear_*'=>'clear', -params=>{ 'join' => '' }, }, }, 'params' => { 'return_value_undefined' => '', }, 'behavior' => { 'get' => q{ if ( defined( my $value = _GET_VALUE_) ) { _GET_VALUE_; } else { _STATIC_ATTR_{return_value_undefined}; } }, 'set' => q{ my $new_value = shift(); _SET_VALUE_{ "$new_value" }; }, 'concat' => q{ my $new_value = shift(); if ( defined( my $value = _GET_VALUE_) ) { _SET_VALUE_{join( _STATIC_ATTR_{join}, $value, $new_value)}; } else { _SET_VALUE_{ "$new_value" }; } }, 'get_concat' => q{ if ( scalar @_ ) { _BEHAVIOR_{concat} } else { _BEHAVIOR_{get} } }, }, } } ######################################################################## =head2 string_index string_index => [ qw / foo bar baz / ] Creates string accessor methods, like string above, but also maintains a static hash index in which each object is stored under the value of the field when the slot is set. This is a unique index, so only one object can have a given key. If an object has a slot set to a value which another object is already set to the object currently set to that value has that slot set to undef and the new object will be put into the hash under that value. Objects with undefined values are not stored in the index. Note that to free items from memory, you must clear these values! B: =over 4 =item * The method find_x is defined which if called with any arguments returns a list of the objects stored under those values in the hash. Called with no arguments, it returns a reference to the hash. =back B: =over 4 =item * find_or_new 'string_index -find_or_new' => [ qw / foo bar baz / ] Just like string_index except the find_x method is defined to call the new method to create an object if there is no object already stored under any of the keys you give as arguments. =back =cut sub string_index { ( { '-import' => { 'Template::Generic:generic' => '*' }, 'params' => { 'new_method' => 'new', }, 'interface' => { default => { '*'=>'get_set', 'clear_*'=>'clear', 'find_*'=>'find' }, find_or_new=>{'*'=>'get_set', 'clear_*'=>'clear', 'find_*'=>'find_or_new'} }, 'code_expr' => { _REMOVE_FROM_INDEX_ => q{ if (defined ( my $old_v = _GET_VALUE_ ) ) { delete _ATTR_{'index'}{ $old_v }; } }, _ADD_TO_INDEX_ => q{ if (defined ( my $new_value = _GET_VALUE_ ) ) { if ( my $old_item = _ATTR_{'index'}{$new_value} ) { # There's already an object stored under that value so we # need to unset it's value. # And maybe issue a warning? Or croak? my $m_name = _ATTR_{'name'}; $old_item->$m_name( undef ); } # Put ourself in the index under that value _ATTR_{'index'}{$new_value} = _SELF_; } }, _INDEX_HASH_ => '_ATTR_{index}', }, 'behavior' => { '-init' => [ sub { my $m_info = $_[0]; defined $m_info->{'index'} or $m_info->{'index'} = {}; return; } ], 'get' => q{ return _GET_VALUE_; }, 'set' => q{ my $new_value = shift; _REMOVE_FROM_INDEX_ # Set our value to new _SET_VALUE_{ $new_value }; _ADD_TO_INDEX_ }, 'get_set' => q{ if ( scalar @_ ) { _BEHAVIOR_{set} } else { _BEHAVIOR_{get} } }, 'clear' => q{ _REMOVE_FROM_INDEX_ _SET_VALUE_{ undef }; }, 'find' => q{ if ( scalar @_ ) { return @{ _ATTR_{'index'} }{ @_ }; } else { return _INDEX_HASH_ } }, 'find_or_new' => q{ if ( scalar @_ ) { my $class = _SELF_CLASS_; my $new_method = _ATTR_REQUIRED_{'new_method'}; my $m_name = _ATTR_{'name'}; foreach (@_) { next if defined _ATTR_{'index'}{$_}; # create new instance and set its value; it'll add itself to index $class->$new_method()->$m_name($_); } return @{ _ATTR_{'index'} }{ @_ }; } else { return _INDEX_HASH_ } }, }, } ) } ######################################################################## =head2 number Accessor A generic scalar-value accessor meta-method which serves as an abstraction for basic "get_set" methods and numerous related interfaces use Class::MakeMethods -MakerClass => "...", string => [ 'foo', 'bar' ]; ... $self->foo( 23 ); print $self->foo(); This meta-method extends the scalar meta-method, and supports the same interfaces and parameters. However, it generally treats values as numbers, and can not be used to store strings or references. B: In addition to those provided by C, the following calling interfaces are available. =over 4 =item -counter Provides the numeric get_set behavior for I<*>, and numeric I<*>_incr and I<*>_reset methods. =back B: In addition to those provided by C, the following types of accessor methods are available. =over 4 =item get_set The get_set behavior is similar to the default scalar behavior except that empty values are treated as zero. =item increment If no argument is provided, increments the I value by 1. If an argument is provided, the value is incremented by that amount. Returns the increased value. =item clear Sets the value to zero. =back =cut sub number { { '-import' => { 'Template::Generic:scalar' => '*' }, 'interface' => { counter => { '*'=>'get_set', '*_incr'=>'incr', '*_reset'=>'clear' }, }, 'params' => { 'return_value_undefined' => 0, }, 'behavior' => { 'get_set' => q{ if ( scalar @_ ) { local $_ = shift; if ( defined $_ ) { croak "Can't set _STATIC_ATTR_{name} to non-numeric value '$_'" if ( /[^\+\-\,\d\.e]/ ); s/\,//g; } _SET_VALUE_{ $_ } } defined( _GET_VALUE_ ) ? _GET_VALUE_ : _STATIC_ATTR_{return_value_undefined} }, 'incr' => q{ _VALUE_ ||= 0; _VALUE_ += ( scalar @_ ? shift : 1 ) }, 'decr' => q{ _VALUE_ ||= 0; _VALUE_ -= ( scalar @_ ? shift : 1 ) }, }, } } ######################################################################## =head2 boolean Accessor A generic scalar-value accessor meta-method which serves as an abstraction for basic "get_set" methods and numerous related interfaces use Class::MakeMethods -MakerClass => "...", string => [ 'foo', 'bar' ]; ... $self->foo( 1 ); print $self->foo(); $self->clear_foo; This meta-method extends the scalar meta-method, and supports the same interfaces and parameters. However, it generally treats values as true-or-false flags, and can not be used to store strings, numbers, or references. B: =over 4 =item flag_set_clear (default) Provides the get_set behavior for I<*>, and set_I<*> and clear_I<*> methods to set the value to true or false. =back B: In addition to those provided by C, the following types of accessor methods are available. =over 4 =item get_set The get_set behavior is similar to the get_set scalar behavior except that empty or false values are treated as zero, and true values are treated as zero. =item set_true Sets the value to one. =item set_false Sets the value to zero. =back =cut sub boolean { { '-import' => { 'Template::Generic:scalar' => '*' }, 'interface' => { default => {'*'=>'get_set', 'clear_*'=>'set_false', 'set_*'=>'set_true'}, flag_set_clear => {'*'=>'get_set', 'clear_*'=>'set_false', 'set_*'=>'set_true'}, }, 'behavior' => { 'get' => q{ _GET_VALUE_ || 0 }, 'set' => q{ if ( shift ) { _BEHAVIOR_{set_true} } else { _BEHAVIOR_{set_false} } }, 'set_true' => q{ _SET_VALUE_{ 1 } }, 'set_false' => q{ _SET_VALUE_{ 0 } }, 'set_value' => q{ _SET_VALUE_{ scalar @_ ? shift : 1 } }, }, } } ######################################################################## =head2 bits Accessor A generic accessor for bit-field values. The difference between 'Template::Generic:bits' and 'Template::Generic:boolean' is that all flags created with this meta-method are stored in a single vector for space efficiency. B: The following calling interfaces are available. =over 4 =item default Provides get_set behavior for I<*>, a set_I<*> method which sets the value to true and a clear_I<*> method which sets the value to false. Also defines methods named bits, bit_fields, and bit_dump with the behaviors below. These methods are shared across all of the boolean meta-methods defined by a single class. =item class_methods . =back B: The following types of bit-level accessor methods are available. =over 4 =item get_set Returns the value of the named flag. If called with an argument, it first sets the named flag to the truth-value of the argument. =item set_true Sets the value to true. =item set_false Sets the value to false. =back B: The following types of methods manipulate the overall vector value. =over 4 =item bits Returns the vector containing all of the bit fields (remember however that a vector containing all 0 bits is still true). =item bit_dump Returns a hash of the flag-name/flag-value pairs. =item bits_size Returns the number of bits that can fit into the current vector. =item bits_complement Returns the twos-complement of the vector. =item bit_pos_get Takes a single argument and returns the value of the bit stored in that position. =item bit_pos_set Takes two arguments and sets the bit stored in the position of the first argument to the value of the second argument. =back B: The following types of class methods are available. =over 4 =item bit_names Returns a list of all the flags by name. =back =cut sub bits { { '-import' => { # 'Template::Generic:generic' => '*', }, 'interface' => { default => { '*'=>'get_set', 'set_*'=>'set_true', 'clear_*'=>'set_false', 'bit_fields'=>'bit_names', 'bit_string'=>'bit_string', 'bit_list'=>'bit_list', 'bit_hash'=>'bit_hash', }, class_methods => { 'bit_fields'=>'bit_names', 'bit_string'=>'bit_string', 'bit_list'=>'bit_list', 'bit_hash'=>'bit_hash', }, }, 'code_expr' => { '_VEC_POS_VALUE_{}' => 'vec(_VALUE_, *, 1)', _VEC_VALUE_ => '_VEC_POS_VALUE_{ _ATTR_{bfp} }', _CLASS_INFO_ => '$Class::MakeMethods::Template::Hash::bits{_STATIC_ATTR_{target_class}}', }, 'modifier' => { '-all' => [ q{ defined _VALUE_ or _VALUE_ = ""; * } ], }, 'behavior' => { '-init' => sub { my $m_info = $_[0]; $m_info->{bfp} ||= do { my $array = ( $Class::MakeMethods::Template::Hash::bits{$m_info->{target_class}} ||= [] ); my $idx; foreach ( 0..$#$array ) { if ( $array->[$_] eq $m_info->{'name'} ) { $idx = $_; last } } unless ( $idx ) { push @$array, $m_info->{'name'}; $idx = $#$array; } $idx; }; return; }, 'bit_names' => q{ @{ _CLASS_INFO_ }; }, 'bit_string' => q{ if ( @_ ) { _SET_VALUE_{ shift @_ }; } else { _VALUE_; } }, 'bits_size' => q{ 8 * length( _VALUE_ ); }, 'bits_complement' => q{ ~ _VALUE_; }, 'bit_hash' => q{ my @bits = @{ _CLASS_INFO_ }; if ( @_ ) { my %bits = @_; _SET_VALUE_{ pack 'b*', join '', map { $_ ? 1 : 0 } @bits{ @bits } }; return @_; } else { map { $bits[$_], vec(_VALUE_, $_, 1) } 0 .. $#bits } }, 'bit_list' => q{ if ( @_ ) { _SET_VALUE_{ pack 'b*', join( '', map { $_ ? 1 : 0 } @_ ) }; return map { $_ ? 1 : 0 } @_; } else { split //, unpack "b*", _VALUE_; } }, 'bit_pos_get' => q{ vec(_VALUE_, $_[0], 1) }, 'bit_pos_set' => q{ vec(_VALUE_, $_[0], 1) = ( $_[1] ? 1 : 0 ) }, 'get_set' => q{ if ( @_ ) { _VEC_VALUE_ = ( $_[0] ? 1 : 0 ); } else { _VEC_VALUE_; } }, 'get' => q{ _VEC_VALUE_; }, 'set' => q{ _VEC_VALUE_ = ( $_[0] ? 1 : 0 ); }, 'set_true' => q{ _VEC_VALUE_ = 1; }, 'set_false' => q{ _VEC_VALUE_ = 0; }, }, } } ######################################################################## =head2 array Accessor Creates accessor methods for manipulating arrays of values. B: The following calling interfaces are available. =over 4 =item default Provides get_set behavior for I<*>, and I_I<*> methods for the non-get behaviors below. =item minimal Provides get_set behavior for I<*>, and I<*>_I methods for clear behavior. =item get_set_items Provides the get_set_items for I<*>. =item x_verb Provides get_push behavior for I<*>, and I<*>_I methods for the non-get behaviors below. =item get_set_ref Provides the get_set_ref for I<*>. =item get_set_ref_help Provides the get_set_ref for I<*>, and I_I<*> methods for the non-get behaviors below. =back B: The following types of accessor methods are available. =over 4 =item get_set_items Called with no arguments returns a reference to the array stored in the slot. Called with one simple scalar argument it treats the argument as an index and returns the value stored under that index. Called with more than one argument, treats them as a series of index/value pairs and adds them to the array. =item get_push If arguments are passed, these values are pushed on to the list; if a single array ref is passed, its values are used as the arguments. This method returns the list of values stored in the slot. In an array context it returns them as an array and in a scalar context as a reference to the array. =item get_set_ref If arguments are passed, these values are placed on the list, replacing the current contents; if a single array ref is passed, its values are used as the arguments. This method returns the list of values stored in the slot. In an array context it returns them as an array and in a scalar context as a reference to the array. =item get_set If arguments are passed, these values are placed on the list, replacing the current contents. This method returns the list of values stored in the slot. In an array context it returns them as an array and in a scalar context as a reference to the array. =item push Append items to tail. =item pop Remove an item from the tail. =item shift Remove an item from the front. =item unshift Prepend items to front. =item splice Remove or replace items. =item clear Remove all items. =item count Returns the number of item in the list. =back =cut sub array { { '-import' => { 'Template::Generic:generic' => '*' }, 'interface' => { default => { '*'=>'get_set', map( ($_.'_*' => $_ ), qw( pop push unshift shift splice clear count )), map( ('*_'.$_ => $_ ), qw( ref index ) ), }, minimal => { '*'=>'get_set', '*_clear'=>'clear' }, get_set_items => { '*'=>'get_set_items' }, x_verb => { '*'=>'get_set', map( ('*_'.$_ => $_ ), qw(pop push unshift shift splice clear count ref index )), }, get_set_ref => { '*'=>'get_set_ref' }, get_set_ref_help => { '*'=>'get_set_ref', '-base'=>'default' }, }, 'modifier' => { '-all' => [ q{ _ENSURE_REF_VALUE_; * } ], }, 'code_expr' => { '_ENSURE_REF_VALUE_' => q{ _REF_VALUE_ ||= []; }, }, 'behavior' => { 'get_set' => q{ @{_REF_VALUE_} = @_ if ( scalar @_ ); return wantarray ? @{_GET_VALUE_} : _REF_VALUE_; }, 'get_set_ref' => q{ @{_REF_VALUE_} = ( ( scalar(@_) == 1 and ref($_[0]) eq 'ARRAY' ) ? @{$_[0]} : @_ ) if ( scalar @_ ); return wantarray ? @{_GET_VALUE_} : _REF_VALUE_; }, 'get_push' => q{ push @{_REF_VALUE_}, map { ref $_ eq 'ARRAY' ? @$_ : ($_) } @_; return wantarray ? @{_GET_VALUE_} : _REF_VALUE_; }, 'ref' => q{ _REF_VALUE_ }, 'get' => q{ return wantarray ? @{_GET_VALUE_} : _REF_VALUE_ }, 'set' => q{ @{_REF_VALUE_} = @_ }, 'pop' => q{ pop @{_REF_VALUE_} }, 'push' => q{ push @{_REF_VALUE_}, @_ }, 'shift' => q{ shift @{_REF_VALUE_} }, 'unshift' => q{ unshift @{_REF_VALUE_}, @_ }, 'slice' => q{ _GET_VALUE_->[ @_ ] }, 'splice' => q{ splice @{_REF_VALUE_}, shift, shift, @_ }, 'count' => q{ scalar @{_GET_VALUE_} }, 'clear' => q{ @{ _REF_VALUE_ } = () }, 'index' => q{ my $list = _REF_VALUE_; ( scalar(@_) == 1 ) ? $list->[shift] : wantarray ? (map $list->[$_], @_) : [map $list->[$_], @_] }, 'get_set_items' => q{ if ( scalar @_ == 0 ) { return _REF_VALUE_; } elsif ( scalar @_ == 1 ) { return _GET_VALUE_->[ shift() ]; } else { _BEHAVIOR_{set_items} } }, 'set_items' => q{ ! (@_ % 2) or croak "Odd number of items in assigment to _STATIC_ATTR_{name}"; while ( scalar @_ ) { my ($index, $value) = splice @_, 0, 2; _REF_VALUE_->[ $index ] = $value; } return _REF_VALUE_; }, } } } ######################################################################## =head2 hash Accessor Creates accessor methods for manipulating hashes of key-value pairs. B: The following calling interfaces are available. =over 4 =item default Provides get_set behavior for I<*>, and I<*>_I methods for most of the other behaviors below. =item get_set_items Provides the get_set_items for I<*>. =back B: The following types of accessor methods are available. =over 4 =item get_set_items Called with no arguments returns a reference to the hash stored. Called with one simple scalar argument it treats the argument as a key and returns the value stored under that key. Called with more than one argument, treats them as a series of key/value pairs and adds them to the hash. =item get_push Called with no arguments returns the hash stored, as a hash in a list context or as a reference in a scalar context. Called with one simple scalar argument it treats the argument as a key and returns the value stored under that key. Called with one array reference argument, the array elements are considered to be be keys of the hash. x returns the list of values stored under those keys (also known as a I.) Called with one hash reference argument, the keys and values of the hash are added to the hash. Called with more than one argument, treats them as a series of key/value pairs and adds them to the hash. =item get_set Like get_push, except if called with more then one argument, empties the current hash items before adding those arguments to the hash. =item push Called with one hash reference argument, the keys and values of the hash are added to the hash. Called with more than one argument, treats them as a series of key/value pairs and adds them to the hash. =item keys Returns a list of the keys of the hash. =item values Returns a list of the values in the hash. =item tally Takes a list of arguments and for each scalar in the list increments the value stored in the hash and returns a list of the current (after the increment) values. =item exists Takes a single key, returns whether that key exists in the hash. =item delete Takes a list, deletes each key from the hash, and returns the corresponding values. =item clear Resets hash to empty. =back =cut sub hash { { '-import' => { 'Template::Generic:generic' => '*' }, 'interface' => { 'default' => { '*'=>'get_set', map {'*_'.$_ => $_} qw(push set keys values delete exists tally clear), }, get_set_items => { '*'=>'get_set_items' }, }, 'modifier' => { '-all' => [ q{ _ENSURE_REF_VALUE_; * } ], }, 'code_expr' => { '_ENSURE_REF_VALUE_' => q{ _REF_VALUE_ ||= {}; }, _HASH_GET_ => q{ ( wantarray ? %{_GET_VALUE_} : _REF_VALUE_ ) }, _HASH_GET_VALUE_ => q{ ( ref $_[0] eq 'ARRAY' ? @{ _GET_VALUE_ }{ @{ $_[0] } } : _REF_VALUE_->{ $_[0] } ) }, _HASH_SET_ => q{ ! (@_ % 2) or croak "Odd number of items in assigment to _STATIC_ATTR_{name}"; %{_REF_VALUE_} = @_ }, _HASH_PUSH_ => q{ ! (@_ % 2) or croak "Odd number of items in assigment to _STATIC_ATTR_{name}"; my $count; while ( scalar @_ ) { local $_ = shift; _REF_VALUE_->{ $_ } = shift(); ++ $count; } $count; }, }, 'behavior' => { 'get_set' => q { # If called with no arguments, return hash contents return _HASH_GET_ if (scalar @_ == 0); # If called with a hash ref, act as if contents of hash were passed # local @_ = %{ $_[0] } if ( scalar @_ == 1 and ref $_[0] eq 'HASH' ); @_ = %{ $_[0] } if ( scalar @_ == 1 and ref $_[0] eq 'HASH' ); # If called with an index, get that value, or a slice for array refs return _HASH_GET_VALUE_ if (scalar @_ == 1 ); # Push on new values and return complete set _HASH_SET_; return _HASH_GET_; }, 'get_push' => q{ # If called with no arguments, return hash contents return _HASH_GET_ if (scalar @_ == 0); # If called with a hash ref, act as if contents of hash were passed # local @_ = %{ $_[0] } if ( scalar @_ == 1 and ref $_[0] eq 'HASH' ); @_ = %{ $_[0] } if ( scalar @_ == 1 and ref $_[0] eq 'HASH' ); # If called with an index, get that value, or a slice for array refs return _HASH_GET_VALUE_ if (scalar @_ == 1 ); # Push on new values and return complete set _HASH_PUSH_; return _HASH_GET_; }, 'get_set_items' => q{ if ( scalar @_ == 0 ) { return _REF_VALUE_; } elsif ( scalar @_ == 1 ) { return _REF_VALUE_->{ shift() }; } else { while ( scalar @_ ) { my ($index, $value) = splice @_, 0, 2; _REF_VALUE_->{ $index } = $value; } return _REF_VALUE_; } }, 'get' => q{ _HASH_GET_ }, 'set' => q{ _HASH_SET_ }, 'push' => q{ # If called with a hash ref, act as if contents of hash were passed # local @_ = %{ $_[0] } if ( scalar @_ == 1 and ref $_[0] eq 'HASH' ); @_ = %{ $_[0] } if ( scalar @_ == 1 and ref $_[0] eq 'HASH' ); _HASH_PUSH_ }, 'keys' => q{ keys %{_GET_VALUE_} }, 'values' => q{ values %{_GET_VALUE_} }, 'unique_values' => q{ values %{ { map { $_=>$_ } values %{_GET_VALUE_} } } }, 'delete' => q{ scalar @_ <= 1 ? delete @{ _REF_VALUE_ }{ $_[0] } : map { delete @{ _REF_VALUE_ }{ $_ } } (@_) }, 'exists' => q{ return 0 unless defined _GET_VALUE_; foreach (@_) { return 0 unless exists ( _REF_VALUE_->{$_} ) } return 1; }, 'tally' => q{ map { ++ _REF_VALUE_->{$_} } @_ }, 'clear' => q{ %{ _REF_VALUE_ } = () }, 'ref' => q{ _REF_VALUE_ }, }, } } ######################################################################## =head2 tiedhash Accessor A variant of Generic:hash which initializes the hash by tieing it to a caller-specified package. See the documentation on C for interfaces and behaviors. B: The following parameters I be provided: =over 4 =item tie I. The name of the class to tie to. Id the required class>. =item args I. Additional arguments for the tie, as an array ref. =back Example: use Class::MakeMethods tie_hash => [ hits => { tie => q/Tie::RefHash/, args => [] } ]; use Class::MakeMethods tie_hash => [ [qw(hits errors)] => { tie => q/Tie::RefHash/, args => [] } ]; use Class::MakeMethods tie_hash => [ { name => hits, tie => q/Tie::RefHash/, args => [] } ]; =cut sub tiedhash { { '-import' => { 'Template::Generic:hash' => '*' }, 'modifier' => { '-all' => [ q{ if ( ! defined _GET_VALUE_ ) { %{ _REF_VALUE_ } = (); tie %{ _REF_VALUE_ }, _ATTR_REQUIRED_{tie}, @{ _ATTR_{args} }; } * } ], }, } } ######################################################################## =head2 hash_of_arrays Accessor Creates accessor methods for manipulating hashes of array-refs. B: The following calling interfaces are available. =over 4 =item default Provides get behavior for I<*>, and I<*>_I methods for the other behaviors below. =back B: The following types of accessor methods are available. =over 4 =item get Returns all the values for all the given keys, in order. If no keys are given, returns all the values (in an unspecified key order). The result is returned as an arrayref in scalar context. This arrayref is I part of the data structure; messing with it will not affect the contents directly (even if a single key was provided as argument.) If any argument is provided which is an arrayref, then the members of that array are used as keys. Thus, the trivial empty-key case may be utilized with an argument of []. =item keys Returns the keys of the hash. As an arrayref in scalar context. =item exists Takes a list of keys, and returns whether all of the key exists in the hash (i.e., the C of whether the individual keys exist). =item delete Takes a list, deletes each key from the hash. =item push Takes a key, and some values. Pushes the values onto the list denoted by the key. If the first argument is an arrayref, then each element of that arrayref is treated as a key and the elements pushed onto each appropriate list. =item pop Takes a list of keys, and pops each one. Returns the list of popped elements. undef is returned in the list for each key that is has an empty list. =item unshift Like push, only the from the other end of the lists. =item shift Like pop, only the from the other end of the lists. =item splice Takes a key, offset, length, and a values list. Splices the list named by the key. Anything from the offset argument (inclusive) may be omitted. See L. =item clear Takes a list of keys. Resets each named list to empty (but does not delete the keys.) =item count Takes a list of keys. Returns the sum of the number of elements for each named list. =item index Takes a key, and a list of indices. Returns a list of each item at the corresponding index in the list of the given key. Uses undef for indices beyond range. =item remove Takes a key, and a list of indices. Removes each corresponding item from the named list. The indices are effectively looked up at the point of call -- thus removing indices 3, 1 from list (a, b, c, d) will remove (d) and (b). =item sift Takes a key, and a set of named arguments, which may be a list or a hash ref. Removes list members based on a grep-like approach. =over 4 =item filter The filter function used (as a coderef). Is passed two arguments, the value compared against, and the value in the list that is potential for grepping out. If returns true, the value is removed. Default is C. =item keys The list keys to sift through (as an arrayref). Unknown keys are ignored. Default: all the known keys. =item values The values to sift out (as an arrayref). Default: C<[undef]> =back =back =cut sub hash_of_arrays { { '-import' => { 'Template::Generic:hash' => '*' }, 'interface' => { default => { '*'=>'get', map( ('*_'.$_ => $_ ), qw(keys exists delete pop push shift unshift splice clear count index remove sift last set )), }, }, 'behavior' => { 'get' => q{ my @Result; if ( ! scalar @_ ) { @Result = map @$_, values %{_VALUE_}; } elsif ( scalar @_ == 1 and ref ($_[0]) eq 'ARRAY' ) { @Result = map @$_, @{_VALUE_}{@{$_[0]}}; } else { my @keys = map { ref ($_) eq 'ARRAY' ? @$_ : $_ } grep exists _VALUE_{$_}, @_; @Result = map @$_, @{_VALUE_}{@keys}; } return wantarray ? @Result : \@Result; }, 'pop' => q{ map { pop @{_VALUE_->{$_}} } @_ }, 'last' => q{ map { _VALUE_->{$_}->[-1] } @_ }, 'push' => q{ for ( ( ref ($_[0]) eq 'ARRAY' ? @{shift()} : shift() ) ) { push @{_VALUE_->{$_}}, @_; } }, 'shift' => q{ map { shift @{_VALUE_->{$_}} } @_ }, 'unshift' => q{ for ( ( ref ($_[0]) eq 'ARRAY' ? @{shift()} : shift() ) ) { unshift @{_VALUE_->{$_}}, @_; } }, 'splice' => q{ my $key = shift; splice @{ _VALUE_->{$key} }, shift, shift, @_; }, 'clear' => q{ foreach (@_) { _VALUE_->{$_} = []; } }, 'count' => q{ my $Result = 0; foreach (@_) { # Avoid autovivifying additional entries. $Result += exists _VALUE_->{$_} ? scalar @{_VALUE_->{$_}} : 0; } return $Result; }, 'index' => q{ my $key_r = shift; my @Result; my $key; foreach $key ( ( ref ($key_r) eq 'ARRAY' ? @$key_r : $key_r ) ) { my $ary = _VALUE_->{$key}; for (@_) { push @Result, ( @{$ary} > $_ ) ? $ary->[$_] : undef; } } return wantarray ? @Result : \@Result; }, 'set' => q{ my $key_r = shift; croak "_ATTR_{name} expects a key and then index => value pairs.\n" if @_ % 2; while ( scalar @_ ) { my $pos = shift; _VALUE_->{$key_r}->[ $pos ] = shift(); } return; }, 'remove' => q{ my $key_r = shift; my $key; foreach $key ( ( ref ($key_r) eq 'ARRAY' ? @$key_r : $key_r ) ) { my $ary = _VALUE_->{$key}; foreach ( sort {$b<=>$a} grep $_ < @$ary, @_ ) { splice (@$ary, $_, 1); } } return; }, 'sift' => q{ my %args = ( scalar @_ == 1 and ref $_[0] eq 'HASH' ) ? %{$_[0]} : @_; my $hash = _VALUE_; my $filter_sr = $args{'filter'} || sub { $_[0] == $_[1] }; my $keys_ar = $args{'keys'} || [ keys %$hash ]; my $values_ar = $args{'values'} || [undef]; # This is harder than it looks; reverse means we want to grep out only # if *none* of the values matches. I guess an evaled block, or closure # or somesuch is called for. # my $reverse = $args{'reverse'} || 0; my ($key, $i, $value); KEY: foreach $key (@$keys_ar) { next KEY unless exists $hash->{$key}; INDEX: for ($i = $#{$hash->{$key}}; $i >= 0; $i--) { foreach $value (@$values_ar) { if ( $filter_sr->($value, $hash->{$key}[$i]) ) { splice @{$hash->{$key}}, $i, 1; next INDEX; } } } } return; }, }, } } ######################################################################## =head2 object Accessor Creates accessor methods for manipulating references to objects. In addition to creating a method to get and set the object reference, the meta-method can also define forwarded methods that automatically pass calls onto the object stored in that slot; see the description of the 'delegate' parameter below. B: The following calling interfaces are available. =over 4 =item default Provides get_set behavior for I<*>, clear behavior for 'delete_*', and forwarding methods for any values in the method's 'delegate' or 'soft_delegate' parameters. =item get_and_set Provides named get method, set_I and clear_I methods. =item get_init_and_set Provides named get_init method, set_I and clear_I methods. =back B: The following types of accessor methods are available. =over 4 =item get_set The get_set method, if called with a reference to an object of the given class as the first argument, stores it. If called with any other arguments, creates and stores a new object, passing the arguemnts to the new() method for the object. If called without arguments, returns the current value, which may be undefined if one has not been stored yet. =item get_set_init The get_set_init method, if called with a reference to an object of the given class as the first argument, stores it. If the slot is not filled yet it creates an object by calling the given new method of the given class. Any arguments passed to the get_set_init method are passed on to new. In all cases the object now stored is returned. =item get_init If the instance is empty, creates and stores a new one. Returns the instance. =item get Returns the current value, which may be undefined if one has not been stored yet. =item set If called with a reference to an object of the given class as the first argument, stores it. If called with any other arguments, creates and stores a new object, passing the arguments to the new() method. If called without arguments, creates and stores a new object, without any arguments to the new() method. =item clear Removes the reference value. =item I If a 'delegate' or 'soft_delegate' parameter is provided, methods with those names are created that are forwarded directly to the object in the slot, as described below. =back B: The following parameters are supported: =over 4 =item class I. The type of object that will be stored. =item new_method The name of the method to call on the above class to create a new instance. Defaults to 'new'. =item delegate The methods to forward to the object. Can contain a method name, a string of space-spearated method names, or an array of method names. This type of method will croak if it is called when the target object is not defined. =item soft_delegate The methods to forward to the object, if it is present. Can contain a method name, a string of space-spearated method names, or an array of method names. This type of method will return nothing if it is called when the target object is not defined. =back =cut sub object { { '-import' => { # 'Template::Generic:generic' => '*', }, 'interface' => { default => { '*'=>'get_set', 'clear_*'=>'clear' }, get_set_init => { '*'=>'get_set_init', 'clear_*'=>'clear' }, get_and_set => {'*'=>'get', 'set_*'=>'set', 'clear_*'=>'clear' }, get_init_and_set => { '*'=>'get_init','set_*'=>'set','clear_*'=>'clear' }, init_and_get => { '*'=>'init_and_get', -params=>{ init_method=>'init_*' } }, }, 'params' => { new_method => 'new' }, 'code_expr' => { '_CALL_NEW_AND_STORE_' => q{ my $new_method = _ATTR_REQUIRED_{new_method}; my $class = _ATTR_REQUIRED_{'class'}; _SET_VALUE_{ $class->$new_method(@_) }; }, }, 'behavior' => { '-import' => { 'Template::Generic:scalar' => [ qw( get clear ) ], }, 'get_set' => q{ if ( scalar @_ ) { if (ref $_[0] and UNIVERSAL::isa($_[0], _ATTR_REQUIRED_{'class'})) { _SET_VALUE_{ shift }; } else { _CALL_NEW_AND_STORE_ } } else { _VALUE_; } }, 'set' => q{ if ( ! defined $_[0] ) { _SET_VALUE_{ undef }; } elsif (ref $_[0] and UNIVERSAL::isa($_[0], _ATTR_REQUIRED_{'class'})) { _SET_VALUE_{ shift }; } else { _CALL_NEW_AND_STORE_ } }, 'get_init' => q{ if ( ! defined _VALUE_ ) { _CALL_NEW_AND_STORE_ } _VALUE_; }, 'init_and_get' => q{ if ( ! defined _VALUE_ ) { my $init_method = _ATTR_REQUIRED_{'init_method'}; _SET_VALUE_{ _SELF_->$init_method( @_ ) }; } else { _BEHAVIOR_{get} } }, 'get_set_init' => q{ if (ref $_[0] and UNIVERSAL::isa($_[0], _ATTR_REQUIRED_{'class'})) { _SET_VALUE_{ shift }; } elsif ( ! defined _VALUE_ ) { _CALL_NEW_AND_STORE_ } _VALUE_; }, '-subs' => sub { { 'delegate' => sub { my($m_info, $name) = @_; sub { my $m_name = $m_info->{'name'}; my $obj = (shift)->$m_name() or Carp::croak("Can't forward $name because $m_name is empty"); $obj->$name(@_) } }, 'soft_delegate' => sub { my($m_info, $name) = @_; sub { my $m_name = $m_info->{'name'}; my $obj = (shift)->$m_name() or return; $obj->$name(@_) } }, } }, }, } } ######################################################################## =head2 instance Accessor Creates methods to handle an instance of the calling class. PROFILES =over 4 =item default Provides named get method, and I_I set, new, and clear methods. =item -implicit_new Provides named get_init method, and I_I set, and clear methods. =item -x_verb Provides named get method, and I_I set, new, and clear methods. =back B: The following types of accessor methods are available. =over 4 =item get Returns the value of the instance parameter, which may be undefined if one has not been stored yet. =item get_init If the instance is empty, creates and stores a new one. Returns the instance. =item set Takes a single argument and sets the instance to that value. =item new Creates and stores a new instance. =item clear Sets the instance parameter to undef. =back B: The following parameters are supported: =over 4 =item instance Holds the instance reference. Defaults to undef =item new_method The name of the method to call when creating a new instance. Defaults to 'new'. =back =cut sub instance { { '-import' => { 'Template::Generic:object' => '*', }, 'interface' => { default => 'get_set', }, 'code_expr' => { '_CALL_NEW_AND_STORE_' => q{ my $new_method = _ATTR_REQUIRED_{new_method}; _SET_VALUE_{ (_SELF_)->$new_method(@_) }; }, }, } } ######################################################################## =head2 array_of_objects Accessor Creates accessor methods for manipulating references to arrays of object references. Operates like C, but prior to adding any item to the array, it first checks to see if it is an instance of the designated class, and if not passes it as an argument to that class's new method and stores the result instead. Forwarded methods return a list of the results returned by Cing the method over each object in the array. See the documentation on C for interfaces and behaviors. B: The following parameters are supported: =over 4 =item class I. The type of object that will be stored. =item delegate The methods to forward to the object. Can contain a method name, a string of space-spearated method names, or an array of method names. =item new_method The name of the method to call on the above class to create a new instance. Defaults to 'new'. =back =cut sub array_of_objects { { '-import' => { 'Template::Generic:array' => '*', }, 'params' => { new_method => 'new', }, 'modifier' => { '-all get_set' => q{ _BLESS_ARGS_ * }, '-all get_push' => q{ _BLESS_ARGS_ * }, '-all set' => q{ _BLESS_ARGS_ * }, '-all push' => q{ _BLESS_ARGS_ * }, '-all unshift' => q{ _BLESS_ARGS_ * }, # The below two methods are kinda broken, because the new values # don't get auto-blessed properly... '-all splice' => q{ * }, '-all set_items' => q{ * }, }, 'code_expr' => { '_BLESS_ARGS_' => q{ my $new_method = _ATTR_REQUIRED_{'new_method'}; @_ = map { (ref $_ and UNIVERSAL::isa($_, _ATTR_REQUIRED_{class})) ? $_ : _ATTR_{'class'}->$new_method($_) } @_; }, }, 'behavior' => { '-subs' => sub { { 'delegate' => sub { my($m_info, $name) = @_; sub { my $m_name = $m_info->{'name'}; map { $_->$name(@_) } (shift)->$m_name() } }, } }, }, } } ######################################################################## =head2 code Accessor Creates accessor methods for manipulating references to subroutines. B: The following calling interfaces are available. =over 4 =item default Provides the call_set functionality. =item method Provides the call_method functionality. =back B: The following types of accessor methods are available. =over 4 =item call_set If called with one argument which is a CODE reference, it installs that code in the slot. Otherwise it runs the code stored in the slot with whatever arguments (including none) were passed in. =item call_method Just like B, except the code is called like a method, with $self as its first argument. Basically, you are creating a method which can be different for each object. =back =cut sub code { { '-import' => { # 'Template::Generic:generic' => '*', }, 'interface' => { default => 'call_set', call_set => 'call_set', method => 'call_method', }, 'behavior' => { '-import' => { 'Template::Generic:scalar' => [ qw( get_set get set clear ) ], }, 'call_set' => q{ if ( scalar @_ == 1 and ref($_[0]) eq 'CODE') { _SET_VALUE_{ shift }; # Set the subroutine reference } else { &{ _VALUE_ }( @_ ); # Run the subroutine on the given arguments } }, 'call_method' => q{ if ( scalar @_ == 1 and ref($_[0]) eq 'CODE') { _SET_VALUE_{ shift }; # Set the subroutine reference } else { &{ _VALUE_ }( _SELF_, @_ ); # Run the subroutine on self and args } }, }, } } ######################################################################## =head2 code_or_scalar Accessor Creates accessor methods for manipulating either strings or references to subroutines. You can store any scalar value; code refs are executed when you retrieve the value, while other scalars are returned as-is. B: The following calling interfaces are available. =over 4 =item default Provides the call_set functionality. =item method Provides the call_method functionality. =item eiffel Provides the named get_method, and a helper set_* method. =back B: The following types of accessor methods are available. =over 4 =item get_set_call If called with an argument, either a CODE reference or some other scalar, it installs that code in the slot. Otherwise, if the current value runs the code stored in the slot with whatever arguments (including none) were passed in. =item get_set_method Just like B, except the code is called like a method, with $self as its first argument. Basically, you are creating a method which can be different for each object. =back =cut sub code_or_scalar { { '-import' => { 'Template::Generic:scalar' => '*' }, 'interface' => { default => 'get_set_call', get_set => 'get_set_call', eiffel => { '*'=>'get_method', 'set_*'=>'set' }, method => 'get_set_method', }, 'params' => { }, 'behavior' => { 'get_call' => q{ my $value = _GET_VALUE_; ( ref($value) eq 'CODE' ) ? &$value( @_ ) : $value }, 'get_method' => q{ my $value = _GET_VALUE_; ( ref($value) eq 'CODE' ) ? &$value( _SELF_, @_ ) : $value }, 'get_set_call' => q{ if ( scalar @_ == 1 ) { _BEHAVIOR_{set} } else { _BEHAVIOR_{get_call} } }, 'get_set_method' => q{ if ( scalar @_ == 1 ) { _BEHAVIOR_{set} } else { _BEHAVIOR_{get_call} } }, }, } } ######################################################################## =head1 SEE ALSO See L for general information about this distribution. See L for information about this family of subclasses. =cut 1; libclass-makemethods-perl-1.01.orig/MakeMethods/Template/Global.pm0000644000175000017500000000407510117147703023574 0ustar ericericpackage Class::MakeMethods::Template::Global; use Class::MakeMethods::Template::Generic '-isasubclass'; $VERSION = 1.008; use strict; require 5.0; =head1 NAME Class::MakeMethods::Template::Global - Method that are not instance-dependent =head1 SYNOPSIS package MyObject; use Class::MakeMethods::Template::Global ( scalar => [ 'foo' ] ); package main; MyObject->foo('bar') print MyObject->foo(); ... print $my_instance->foo(); # same thing =head1 DESCRIPTION These meta-methods access values that are shared across all instances of your object in your process. For example, a hash_scalar meta-method will be able to store a different value for each hash instance you call it on, but a static_scalar meta-method will return the same value for any instance it's called on, and setting it from any instance will change the value that all other instances see. B: The following parameters are defined for Static meta-methods. =over 4 =item data The shared value. =back =cut sub generic { { '-import' => { 'Template::Generic:generic' => '*' }, 'code_expr' => { _VALUE_ => '_ATTR_{data}', }, 'params' => { 'data' => undef, } } } ######################################################################## =head2 Standard Methods The following methods from Generic should be supported: scalar string number boolean bits (?) array hash tiedhash (?) hash_of_arrays (?) object instance array_of_objects (?) code code_or_scalar (?) See L for the interfaces and behaviors of these method types. The items marked with a ? above have not been tested sufficiently; please inform the author if they do not function as you would expect. =head1 SEE ALSO See L for general information about this distribution. See L for more about this family of subclasses. See L for information about the various accessor interfaces subclassed herein. =cut 1; libclass-makemethods-perl-1.01.orig/MakeMethods/Template/Hash.pm0000644000175000017500000001432110117122502023240 0ustar ericericpackage Class::MakeMethods::Template::Hash; use Class::MakeMethods::Template::Generic '-isasubclass'; $VERSION = 1.008; use strict; require 5.0; sub generic { { 'params' => { 'hash_key' => '*', }, 'code_expr' => { _VALUE_ => '_SELF_->{_STATIC_ATTR_{hash_key}}', '-import' => { 'Template::Generic:generic' => '*' }, _EMPTY_NEW_INSTANCE_ => 'bless {}, _SELF_CLASS_', _SET_VALUES_FROM_HASH_ => 'while ( scalar @_ ) { local $_ = shift(); $self->{ $_ } = shift() }' }, 'behavior' => { 'hash_delete' => q{ delete _VALUE_ }, 'hash_exists' => q{ exists _VALUE_ }, }, 'modifier' => { # XXX the below doesn't work because modifiers can't have params, # although interfaces can... Either add support for default params # in modifiers, or else move this to another class. # X Should there be a version which uses caller() instead of target_class? 'class_keys' => { 'hash_key' => '"*{target_class}::*{name}"' }, } } } ######################################################################## =head1 NAME Class::MakeMethods::Template::Hash - Method interfaces for hash-based objects =head1 SYNOPSIS package MyObject; use Class::MakeMethods::Template::Hash ( new => [ 'new' ], scalar => [ 'foo', 'bar' ] ); package main; my $obj = MyObject->new( foo => "Foozle", bar => "Bozzle" ); print $obj->foo(); $obj->bar("Bamboozle"); =head1 DESCRIPTION These meta-methods create and access values within blessed hash objects. B: The following parameters are defined for Hash meta-methods. =over 4 =item hash_key The hash key to use when retrieving values from each hash instance. Defaults to '*', the name of the meta-method. Changing this allows you to change an accessor method name to something other than the name of the hash key used to retrieve its value. Note that this parameter is not portable to the other implementations, such as Global or InsideOut. You can take advantage of parameter expansion to define methods whose hash key is composed of the defining package's name and the individual method name, such as C<$self-E{I-I}>: 'hash_key' => '*{target_class}-*{name}' =back B =over 4 =item Behavior: delete Deletes the named key and associated value from the current hash instance. =back =head2 Standard Methods The following methods from Generic are all supported: new scalar string string_index number boolean bits (*) array hash tiedhash hash_of_arrays object instance array_of_objects code code_or_scalar See L for the interfaces and behaviors of these method types. The items marked with a * above are specifically defined in this package, whereas the others are formed automatically by the interaction of this package's generic settings with the code templates provided by the Generic superclass. =cut # This is the only one that needs to be specifically defined. sub bits { { '-import' => { 'Template::Generic:bits' => '*' }, 'params' => { 'hash_key' => '*{target_class}__*{template_name}', }, } } ######################################################################## =head2 struct struct => [ qw / foo bar baz / ]; Creates methods for setting, checking and clearing values which are stored by position in an array. All the slots created with this meta-method are stored in a single array. The argument to struct should be a string or a reference to an array of strings. For each string meta-method x, it defines two methods: I and I. x returns the value of the x-slot. If called with an argument, it first sets the x-slot to the argument. clear_x sets the slot to undef. Additionally, struct defines three class method: I, which returns a list of all of the struct values, I, which returns a list of all the slots by name, and I, which returns a hash of the slot-name/slot-value pairs. =cut sub struct { ( { 'interface' => { default => { '*'=>'get_set', 'clear_*'=>'clear', 'struct_fields'=>'struct_fields', 'struct'=>'struct', 'struct_dump'=>'struct_dump' }, }, 'params' => { 'hash_key' => '*{target_class}__*{template_name}', }, 'behavior' => { '-init' => sub { my $m_info = $_[0]; $m_info->{class} ||= $m_info->{target_class}; my $class_info = ($Class::MakeMethods::Template::Hash::struct{$m_info->{class}} ||= []); if ( ! defined $m_info->{sfp} ) { foreach ( 0..$#$class_info ) { if ( $class_info->[$_] eq $m_info->{'name'} ) { $m_info->{sfp} = $_; last } } if ( ! defined $m_info->{sfp} ) { push @$class_info, $m_info->{'name'}; $m_info->{sfp} = $#$class_info; } } return; }, 'struct_fields' => sub { my $m_info = $_[0]; sub { my $class_info = ( $Class::MakeMethods::Template::Hash::struct{$m_info->{class}} ||= [] ); @$class_info; }}, 'struct' => sub { my $m_info = $_[0]; sub { my $self = shift; $self->{$m_info->{hash_key}} ||= []; if ( @_ ) { @{$self->{$m_info->{hash_key}}} = @_ } @{$self->{$m_info->{hash_key}}}; }}, 'struct_dump' => sub { my $m_info = $_[0]; sub { my $self = shift; my $class_info = ( $Class::MakeMethods::Template::Hash::struct{$m_info->{class}} ||= [] ); map { ($_, $self->$_()) } @$class_info; }}, 'get_set' => sub { my $m_info = $_[0]; sub { my $self = shift; $self->{$m_info->{hash_key}} ||= []; if ( @_ ) { $self->{$m_info->{hash_key}}->[ $m_info->{sfp} ] = shift; } $self->{$m_info->{hash_key}}->[ $m_info->{sfp} ]; }}, 'clear' => sub { my $m_info = $_[0]; sub { my $self = shift; $self->{$m_info->{hash_key}} ||= []; $self->{$m_info->{hash_key}}->[ $m_info->{sfp} ] = undef; }}, }, } ) } ######################################################################## =head1 SEE ALSO See L for general information about this distribution. See L for more about this family of subclasses. See L for information about the various accessor interfaces subclassed herein. =cut 1; libclass-makemethods-perl-1.01.orig/MakeMethods/Template/Inheritable.pm0000644000175000017500000001034210117147655024622 0ustar ericeric=head1 NAME Class::MakeMethods::Template::Inheritable - Overridable data =head1 SYNOPSIS package MyClass; use Class::MakeMethods( 'Template::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... ... =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, and optionally override it in a subclass. =cut ######################################################################## package Class::MakeMethods::Template::Inheritable; use Class::MakeMethods::Template::Generic '-isasubclass'; $VERSION = 1.008; use strict; require 5.0; use Carp; sub generic { { '-import' => { 'Template::Generic:generic' => '*' }, 'modifier' => { '-all' => [ q{ _INIT_VALUE_CLASS_ * } ], }, 'code_expr' => { '_VALUE_CLASS_' => '$_value_class', '_INIT_VALUE_CLASS_' => q{ my _VALUE_CLASS_; my @_INC_search = ( _SELF_, _SELF_CLASS_ ); while ( scalar @_INC_search ) { _VALUE_CLASS_ = shift @_INC_search; last if ( exists _ATTR_{data}->{_VALUE_CLASS_} ); no strict 'refs'; unshift @_INC_search, @{"_VALUE_CLASS_\::ISA"} if ! ref _VALUE_CLASS_; } }, '_VALUE_' => '_ATTR_{data}->{_VALUE_CLASS_}', '_GET_VALUE_' => q{ _ATTR_{data}->{_VALUE_CLASS_} }, '_SET_VALUE_{}' => q{ do { my $data = *; defined($data) ? ( _VALUE_CLASS_ = _SELF_ and _ATTR_{data}->{_SELF_} = $data ) : ( delete _ATTR_{data}->{_SELF_} and undef ) } }, }, } } ######################################################################## =head2 Standard Methods The following methods from Generic should be supported: scalar string string_index (?) number boolean (?) bits (?) array (?) hash (?) tiedhash (?) hash_of_arrays (?) object (?) instance (?) array_of_objects (?) code (?) code_or_scalar (?) See L for the interfaces and behaviors of these method types. The items marked with a * above are specifically defined in this package, whereas the others are formed automatically by the interaction of this package's generic settings with the code templates provided by the Generic superclass. The items marked with a ? above have not been tested sufficiently; please inform the author if they do not function as you would expect. =cut sub array { { '-import' => { 'Template::Generic:array' => '*', }, 'modifier' => { '-all' => q{ _INIT_VALUE_CLASS_; _ENSURE_REF_VALUE_; * }, }, 'code_expr' => { '_ENSURE_REF_VALUE_' => q{ _VALUE_ ||= []; }, '_REF_VALUE_' => '(\@{_ATTR_{data}->{_VALUE_CLASS_}})', }, } } sub hash { { '-import' => { 'Template::Generic:hash' => '*', }, 'modifier' => { '-all' => q{ _INIT_VALUE_CLASS_; _ENSURE_REF_VALUE_; * }, }, 'code_expr' => { '_ENSURE_REF_VALUE_' => q{ _VALUE_ ||= {}; }, '_REF_VALUE_' => '(\%{_ATTR_{data}->{_VALUE_CLASS_}})', }, } } ######################################################################## =head1 SEE ALSO See L for general information about this distribution. See L for more about this family of subclasses. See L for information about the various accessor interfaces subclassed herein. If you just need scalar accessors, see L for a very elegant and efficient implementation. =cut ######################################################################## 1; libclass-makemethods-perl-1.01.orig/MakeMethods/Template/InsideOut.pm0000644000175000017500000001221310117147651024272 0ustar ericericpackage Class::MakeMethods::Template::InsideOut; use Class::MakeMethods::Template::Generic '-isasubclass'; $VERSION = 1.008; use strict; require 5.0; my %ClassInfo; my %Data; sub generic { { '-import' => { 'Template::Generic:generic' => '*' }, 'code_expr' => { '_VALUE_' => '_ATTR_{data}->{_SELF_}', }, 'behavior' => { -register => [ sub { my $m_info = shift; my $class_info = ( $ClassInfo{$m_info->{target_class}} ||= [] ); return ( 'DESTROY' => sub { my $self = shift; foreach ( @$class_info ) { delete $self->{data}->{$self} } # $self->SUPER::DESTROY( @_ ) }, ); } ], } } } ######################################################################## =head1 NAME Class::MakeMethods::Template::InsideOut - External data =head1 SYNOPSIS package MyObject; use Class::MakeMethods::Template::InsideOut ( scalar => [ 'foo', 'bar' ] ); sub new { ... } package main; my $obj = MyObject->new( foo => "Foozle", bar => "Bozzle" ); print $obj->foo(); # Prints Foozle $obj->bar("Bamboozle"); # Sets $obj's bar value =head1 DESCRIPTION Supports the Generic object constructor and accessors meta-method types, but accepts any object as the underlying implementation type, with member data stored in external indices. Each method stores the values associated with various objects in an hash keyed by the object's stringified identity. Since that hash is accessible only from the generated closures, it is impossible for foreign code to manipulate those values except through the method interface. A DESTROY method is installed to remove data for expired objects from the various hashes. (If the DESTROY method is not called, your program will not release this data and memory will be wasted.) B: The following parameters are defined for InsideOut meta-methods. =over 4 =item data An auto-vivified reference to a hash to be used to store the values for each object. =back Note that using InsideOut meta-methods causes the installation of a DESTROY method in the calling class, which deallocates data for each instance when it is discarded. NOTE: This needs some more work to properly handle inheritance. =head2 Standard Methods The following methods from Generic are all supported: scalar string string_index * number boolean bits array hash tiedhash hash_of_arrays object instance array_of_objects code code_or_scalar See L for the interfaces and behaviors of these method types. The items marked with a * above are specifically defined in this package, whereas the others are formed automatically by the interaction of this package's generic settings with the code templates provided by the Generic superclass. =cut ######################################################################## =head2 boolean_index boolean_index => [ qw / foo bar baz / ] Like InsideOut:boolean, boolean_index creates x, set_x, and clear_x methods. However, it also defines a class method find_x which returns a list of the objects which presently have the x-flag set to true. Note that to free items from memory, you must clear these bits! =cut sub boolean_index { { '-import' => { 'Template::Generic:boolean' => '*', }, 'interface' => { default => { '*'=>'get_set', 'set_*'=>'set_true', 'clear_*'=>'set_false', 'find_*'=>'find_true', }, }, 'behavior' => { '-init' => [ sub { my $m_info = $_[0]; defined $m_info->{data} or $m_info->{data} = {}; return; } ], 'set_true' => q{ _SET_VALUE_{ _SELF_ } }, 'set_false' => q{ delete _VALUE_; 0 }, 'find_true' => q{ values %{ _ATTR_{data} }; }, }, } } sub string_index { { '-import' => { 'Template::Generic:string_index' => '*', }, 'interface' => { default => { '*'=>'get_set', 'set_*'=>'set_true', 'clear_*'=>'set_false', 'find_*'=>'find_true', }, }, 'code_expr' => { _INDEX_HASH_ => '_ATTR_{data}', _GET_FROM_INDEX_ => q{ if (defined ( my $old_v = _GET_VALUE_ ) ) { delete _ATTR_{'data'}{ $old_v }; } }, _REMOVE_FROM_INDEX_ => q{ if (defined ( my $old_v = _GET_FROM_INDEX_ ) ) { delete _ATTR_{'data'}{ $old_v }; } }, _ADD_TO_INDEX_{} => q{ if (defined ( my $new_value = _GET_VALUE_ ) ) { if ( my $old_item = _ATTR_{'data'}{$new_value} ) { # There's already an object stored under that value so we # need to unset it's value. # And maybe issue a warning? Or croak? my $m_name = _ATTR_{'name'}; $old_item->$m_name( undef ); } # Put ourself in the index under that value _ATTR_{'data'}{ * } = _SELF_; } }, }, 'behavior' => { '-init' => [ sub { my $m_info = $_[0]; defined $m_info->{data} or $m_info->{data} = {}; return; } ], 'get' => q{ return _GET_FROM_INDEX_; }, 'set' => q{ my $new_value = shift; _REMOVE_FROM_INDEX_ _ADD_TO_INDEX_{ $new_value } }, 'clear' => q{ _REMOVE_FROM_INDEX_ }, }, } } ######################################################################## 1; libclass-makemethods-perl-1.01.orig/MakeMethods/Template/PackageVar.pm0000644000175000017500000000750510117147646024407 0ustar ericericpackage Class::MakeMethods::Template::PackageVar; use Class::MakeMethods::Template::Generic '-isasubclass'; $VERSION = 1.008; use strict; require 5.0; use Carp; =head1 NAME Class::MakeMethods::Template::PackageVar - Static methods with global variables =head1 SYNOPSIS package MyObject; use Class::MakeMethods::Template::PackageVar ( scalar => [ 'foo' ] ); package main; MyObject->foo('bar') print MyObject->foo(); $MyObject::foo = 'bazillion'; print MyObject->foo(); =head1 DESCRIPTION These meta-methods provide access to package (class global) variables. These are essentially the same as the Static meta-methods, except that they use a global variable in the declaring package to store their values. B: The following parameters are defined for PackageVar meta-methods. =over 4 =item variable The name of the variable to store the value in. Defaults to the same name as the method. =back =cut sub generic { { '-import' => { 'Template::Generic:generic' => '*' }, 'params' => { 'variable' => '*' }, 'modifier' => { '-all' => [ q{ no strict; * } ], }, 'code_expr' => { '_VALUE_' => '${_ATTR_{target_class}."::"._ATTR_{variable}}', }, } } ######################################################################## =head2 Standard Methods The following methods from Generic should all be supported: scalar string string_index (?) number boolean bits (?) array (*) hash (*) tiedhash (?) hash_of_arrays (?) object (?) instance (?) array_of_objects (?) code (?) code_or_scalar (?) See L for the interfaces and behaviors of these method types. The items marked with a * above are specifically defined in this package, whereas the others are formed automatically by the interaction of this package's generic settings with the code templates provided by the Generic superclass. The items marked with a ? above have not been tested sufficiently; please inform the author if they do not function as you would expect. =cut ######################################################################## sub array { { '-import' => { 'Template::Generic:array' => '*', }, 'modifier' => { '-all' => q{ no strict; _ENSURE_REF_VALUE_; * }, }, 'code_expr' => { '_ENSURE_REF_VALUE_' => q{ _REF_VALUE_ or @{_ATTR_{target_class}."::"._ATTR_{variable}} = (); }, '_VALUE_' => '\@{_ATTR_{target_class}."::"._ATTR_{variable}}', }, } } ######################################################################## sub hash { { '-import' => { 'Template::Generic:hash' => '*', }, 'modifier' => { '-all' => q{ no strict; _ENSURE_REF_VALUE_; * }, }, 'code_expr' => { '_ENSURE_REF_VALUE_' => q{ _REF_VALUE_ or %{_ATTR_{target_class}."::"._ATTR_{variable}} = (); }, '_VALUE_' => '\%{_ATTR_{target_class}."::"._ATTR_{variable}}', }, } } ######################################################################## =head2 PackageVar:vars This rewrite rule converts package variable names into PackageVar methods of the equivalent data type. Here's an example declaration: package MyClass; use Class::MakeMethods::Template::PackageVar ( vars => '$DEBUG %Index' ); MyClass now has methods that get and set the contents of its $MyClass::DEBUG and %MyClass::Index package variables: MyClass->DEBUG( 1 ); MyClass->Index( 'foo' => 'bar' ); =cut sub vars { my $mm_class = shift; my @rewrite = map [ "Template::PackageVar:$_" ], qw( scalar array hash ); my %rewrite = ( '$' => 0, '@' => 1, '%' => 2 ); while (@_) { my $name = shift; my $data = shift; $data =~ s/\A(.)//; push @{ $rewrite[ $rewrite{ $1 } ] }, { 'name'=>$name, 'variable'=>$data }; } return @rewrite; } 1; libclass-makemethods-perl-1.01.orig/MakeMethods/Template/Ref.pm0000644000175000017500000001017310117122502023072 0ustar ericeric=head1 NAME Class::MakeMethods::Template::Ref - Universal copy and compare methods =head1 SYNOPSIS package MyObject; use Class::MakeMethods::Template::Ref ( 'Hash:new' => [ 'new' ], clone => [ 'clone' ] ); package main; my $obj = MyObject->new( foo => ["Foozle", "Bozzle"] ); my $clone = $obj->clone(); print $obj->{'foo'}[1]; =cut package Class::MakeMethods::Template::Ref; $VERSION = 1.008; use strict; require 5.00; use Carp; use Class::MakeMethods::Template '-isasubclass'; use Class::MakeMethods::Utility::Ref qw( ref_clone ref_compare ); ###################################################################### =head1 DESCRIPTION The following types of methods are provided via the Class::MakeMethods interface: =head2 clone Produce a deep copy of an instance of almost any underlying datatype. Parameters: init_method If defined, this method is called on the new object with any arguments passed in. =cut sub clone { { 'params' => { 'init_method' => '' }, 'interface' => { default => 'clone', clone => { '*'=>'clone', }, }, 'behavior' => { 'clone' => sub { my $m_info = $_[0]; sub { my $callee = shift; ref $callee or croak "Can only copy instances, not a class.\n"; my $self = ref_clone( $callee ); my $init_method = $m_info->{'init_method'}; if ( $init_method ) { $self->$init_method( @_ ); } elsif ( scalar @_ ) { croak "No init_method"; } return $self; }}, }, } } ###################################################################### =head2 prototype Create new instances by making a deep copy of a static prototypical instance. Parameters: init_method If defined, this method is called on the new object with any arguments passed in. =cut sub prototype { ( { 'interface' => { default => { '*'=>'set_or_new', }, }, 'behavior' => { 'set_or_new' => sub { my $m_info = $_[0]; sub { my $class = shift; if ( scalar @_ == 1 and UNIVERSAL::isa( $_[0], $class ) ) { # set $m_info->{'instance'} = shift } else { # get croak "Prototype is not defined" unless $m_info->{'instance'}; my $self = ref_clone($m_info->{'instance'}); my $init_method = $m_info->{'init_method'}; if ( $init_method ) { $self->$init_method( @_ ); } elsif ( scalar @_ ) { croak "No init_method"; } return $self; } }}, 'set' => sub { my $m_info = $_[0]; sub { my $class = shift; $m_info->{'instance'} = shift }}, 'new' => sub { my $m_info = $_[0]; sub { my $class = shift; croak "Prototype is not defined" unless $m_info->{'instance'}; my $self = ref_clone($m_info->{'instance'}); my $init_method = $m_info->{'init_method'}; if ( $init_method ) { $self->$init_method( @_ ); } elsif ( scalar @_ ) { croak "No init_method"; } return $self; }}, }, } ) } ###################################################################### =head2 compare Compare one object to another. B =over 4 =item * default Three-way (sorting-style) comparison. =item * equals Are these two objects equivalent? =item * identity Are these two references to the exact same object? =back =cut sub compare { { 'params' => { 'init_method' => '' }, 'interface' => { default => { '*'=>'compare', }, equals => { '*'=>'equals', }, identity => { '*'=>'identity', }, }, 'behavior' => { 'compare' => sub { my $m_info = $_[0]; sub { my $callee = shift; ref_compare( $callee, shift ); }}, 'equals' => sub { my $m_info = $_[0]; sub { my $callee = shift; ref_compare( $callee, shift ) == 0; }}, 'identity' => sub { my $m_info = $_[0]; sub { $_[0] eq $_[1] }}, }, } } ###################################################################### =head1 SEE ALSO See L for general information about this distribution. See L for more about this family of subclasses. See L for the clone and compare functions used above. =cut ###################################################################### 1; libclass-makemethods-perl-1.01.orig/MakeMethods/Template/Scalar.pm0000644000175000017500000000322610117147632023577 0ustar ericericpackage Class::MakeMethods::Template::Scalar; use Class::MakeMethods::Template::Generic '-isasubclass'; $VERSION = 1.008; use strict; require 5.00; use Carp; =head1 NAME Class::MakeMethods::Template::Scalar - Methods for blessed scalars =head1 SYNOPSIS package MyObject; use Class::MakeMethods::Template::ExternalData ( new => 'new', scalar => 'foo', ); package main; my $obj = MyObject->new( foo => "Foozle" ); print $obj->foo(); # Prints Foozle $obj->foo("Bamboozle"); # Sets $$obj print $obj->foo(); # Prints Bamboozle =head1 DESCRIPTION Supports the Generic object constructor and accessors meta-method types, but uses scalar refs as the underlying implementation type, so only one accessor method can be used effectively. =cut sub generic { { '-import' => { 'Template::Generic:generic' => '*' }, 'code_expr' => { _VALUE_ => '(${_SELF_})', _EMPTY_NEW_INSTANCE_ => 'bless \( my $scalar = undef ), _SELF_CLASS_', }, 'params' => { } } } ######################################################################## =head2 Standard Methods The following methods from Generic are all supported: new scalar string string_index number boolean bits array hash tiedhash hash_of_arrays object instance array_of_objects code code_or_scalar See L for the interfaces and behaviors of these method types. However, note that due to special nature of this package, all accessor methods reference the same scalar value, so setting a value with one method will overwrite the value retrieved by another. =cut 1; libclass-makemethods-perl-1.01.orig/MakeMethods/Template/Static.pm0000644000175000017500000000170510117147750023622 0ustar ericericpackage Class::MakeMethods::Template::Static; use Class::MakeMethods::Template::Global '-isasubclass'; $VERSION = 1.008; 1; __END__ =head1 NAME Class::MakeMethods::Template::Static - Deprecated name for Global =head1 SYNOPSIS package MyObject; use Class::MakeMethods::Template::Global ( scalar => [ 'foo' ] ); package main; MyObject->foo('bar') print MyObject->foo(); ... print $my_instance->foo(); # same thing =head1 DESCRIPTION Earlier versions of this package included a package named Class::MakeMethods::Template::Static. However, in hindsight, this name was poorly chosen, as it suggests a constant, unchanging value, whereas the actual functionality is akin to traditional "global" variables. This functionality is now provided by Class::MakeMethods::Template::Global, of which this is an empty subclass retained to provide backwards compatibility. =head1 SEE ALSO L. =cutlibclass-makemethods-perl-1.01.orig/MakeMethods/Template/Struct.pm0000644000175000017500000000211610117147743023656 0ustar ericericpackage Class::MakeMethods::Template::Struct; use Class::MakeMethods::Template::Array '-isasubclass'; $VERSION = 1.008; 1; __END__ =head1 NAME Class::MakeMethods::Template::Struct - Deprecated name for Array =head1 SYNOPSIS package MyObject; use Class::MakeMethods::Template::Array ( new => [ 'new' ] scalar => [ 'foo', 'bar' ] ); package main; my $obj = MyObject->new( foo => "Foozle", bar => "Bozzle" ); print $obj->foo(); # Prints Foozle $obj->bar("Bamboozle"); # Sets $obj->[1] =head1 DESCRIPTION Earlier versions of this package included a package named Class::MakeMethods::Template::Struct. However, in hindsight, this name was poorly chosen, as it suggests some connection to C-style structs, where the behavior implemented more simply parallels the functionality of Template::Hash and the other Generic subclasses. This functionality is now provided by Class::MakeMethods::Template::Array, of which this is an empty subclass retained to provide backwards compatibility. =head1 SEE ALSO L. =cutlibclass-makemethods-perl-1.01.orig/MakeMethods/Template/StructBuiltin.pm0000644000175000017500000000716710117147617025220 0ustar ericericpackage Class::MakeMethods::Template::StructBuiltin; use Class::MakeMethods::Template::Generic '-isasubclass'; $VERSION = 1.008; use strict; require 5.00; use Carp; =head1 NAME Class::MakeMethods::Template::StructBuiltin =head1 SYNOPSIS use Class::MakeMethods::Template::StructBuiltin ( -TargetClass => 'MyStat', builtin_isa => [ '-{new_function}'=>'stat', qw/ dev ino mode nlink / ] ); =head1 DESCRIPTION This class generates a wrapper around some builtin function, storing the results in the object and providing a by-name interface. Takes a (core) function name, and a arrayref of return position names (we will call it pos_list). Creates: =over 4 =item new Calls the core func with any given arguments, stores the result in the instance. =item x For each member of pos_list, creates a method of the same name which gets/sets the nth member of the returned list, where n is the position of x in pos_list. =item fields Returns pos_list, in the given order. =item dump Returns a list item name, item value, in order. =back Example Usage: package Stat; use Class::MakeMethods::Template::StructBuiltin builtin_isa => [ '-{new_function}'=>'stat', qw/ dev ino mode nlink / ], package main; my $file = "$ENV{HOME}/.template"; my $s = Stat->new($file); print "File $file has ", $s->nlink, " links\n"; Note that (a) the new method does not check the return value of the function called (in the above example, if $file does not exist, you will silently get an empty object), and (b) if you really want the above example, see the core File::stat module. But you get the idea, I hope. =cut sub builtin_isa { ( { 'template' => { default => { '*'=>'get_set', 'dump'=>'dump', 'fields'=>'fields', 'new'=>'new_builtin' }, }, 'behavior' => { '-init' => sub { my $m_info = $_[0]; $m_info->{class} ||= $m_info->{target_class}; my $class_info = ( $Class::MakeMethods::Struct::builtin{$m_info->{class}} ||= [] ); if ( ! defined $m_info->{array_index} ) { foreach ( 0..$#$class_info ) { if ( $class_info->[$_] eq $m_info->{'name'} ) { $m_info->{array_index} = $_; last } } if ( ! defined $m_info->{array_index} ) { push @ $class_info, $m_info->{'name'}; $m_info->{array_index} = $#$class_info; } } if (defined $m_info->{new_function} and ! ref $m_info->{new_function}) { # NOTE Below comments found in original version of MethodMaker. -Simon # Cuz neither \&{"CORE::$func"} or $CORE::{$func} work ... N.B. this # only works for core functions that take only one arg. But I can't # quite figure out how to pass in the list without it getting # evaluated in a scalar context. Hmmm. $m_info->{new_function} = eval "sub { scalar \@_ ? CORE::$m_info->{new_function}(shift) : CORE::$m_info->{new_function} }"; } return; }, 'new_builtin' => sub { my $m_info = $_[0]; sub { my $class = shift; my $function = $m_info->{new_function}; my $self = [ &$function(@_) ]; bless $self, $class; }}, 'fields' => sub { my $m_info = $_[0]; sub { my $class_info = ( $Class::MakeMethods::Struct::builtin{$m_info->{class}} ||= [] ); @$class_info; }}, 'dump' => sub { my $m_info = $_[0]; sub { my $self = shift; my $class_info = ( $Class::MakeMethods::Struct::builtin{$m_info->{class}} ||= [] ); my @keys = @$class_info; map ($keys[$_], $self->[$_]), 0 .. $#keys; }}, 'get_set' => sub { my $m_info = $_[0]; sub { my $self = shift; if ( @_ ) { $self->[ $m_info->{array_index} ] = shift; } $self->[ $m_info->{array_index} ]; }}, }, } ) } 1; libclass-makemethods-perl-1.01.orig/MakeMethods/Template/Universal.pm0000644000175000017500000002537010117122502024333 0ustar ericericpackage Class::MakeMethods::Template::Universal; use Class::MakeMethods::Template '-isasubclass'; $VERSION = 1.008; use strict; require 5.00; require Carp; =head1 NAME Class::MakeMethods::Template::Universal - Meta-methods for any type of object =head1 SYNOPSIS package MyObject; use Class::MakeMethods::Template::Universal ( 'no_op' => [ 'twiddle' ], 'croak' => [ 'fail', { croak_msg => 'Curses!' } ] ); package main; MyObject->twiddle; # Does nothing if ( $foiled ) { MyObject->fail() } # Dies with croak_msg =head1 DESCRIPTION =head1 UNIVERSAL META-METHODS The following meta-methods and behaviors are applicable across multiple types of classes and objects. =head2 Universal:generic This is not a directly-invokable method type, but instead provides code expressions for use in other method-generators. You can use any of these features in your meta-method interfaces without explicitly importing them. B =over 4 =item * --private Causes the method to croak if it is called from outside of the package which originally declared it. Note that this protection can currently be circumvented if your class provides the method_init behavior, or another subroutine that calls methods by name. =item * --protected Causes the method to croak if it is called from a package other than the declaring package and its inheritors. Note that this protection can currently be circumvented if your class provides the method_init behavior, or another subroutine that calls methods by name. =item * --public Cancels any previous -private or -protected declaration. =item * --self_closure Causes the method to return a function reference which is bound to the arguments provided when it is first called. For examples of usage, see the test scripts in t/*closure.t. =item * --lvalue Adds the ":lvalue" attribute to the subroutine declaration. For examples of usage, see the test scripts in t/*lvalue.t. =item * --warn_calls For diagnostic purposes, call warn with the object reference, method name, and arguments before executing the body of the method. =back B =over 4 =item * attributes Runtime access to method parameters. =item * no_op -- See below. =item * croak -- See below. =item * method_init -- See below. =back =cut sub generic { { 'code_expr' => { '_SELF_' => '$self', '_SELF_CLASS_' => '(ref _SELF_ || _SELF_)', '_SELF_INSTANCE_' => '(ref _SELF_ ? _SELF_ : undef)', '_CLASS_FROM_INSTANCE_' => '(ref _SELF_ || croak "Can\'t invoke _STATIC_ATTR_{name} as a class method")', '_ATTR_{}' => '$m_info->{*}', '_STATIC_ATTR_{}' => '_ATTR_{*}', '_ATTR_REQUIRED_{}' => '(_ATTR_{*} or Carp::croak("No * parameter defined for _ATTR_{name}"))', '_ATTR_DEFAULT_{}' => sub { my @a = split(' ',$_[0],2); "(_ATTR_{$a[0]} || $a[1])" }, _ACCESS_PRIVATE_ => '( ( (caller)[0] eq _ATTR_{target_class} ) or croak "Attempted access to private method _ATTR_{name}")', _ACCESS_PROTECTED_ => '( UNIVERSAL::isa((caller)[0], _ATTR_{target_class}) or croak "Attempted access to protected method _ATTR_{name}" )', '_CALL_METHODS_FROM_HASH_' => q{ # Accept key-value attr list, or reference to unblessed hash of attrs my @args = (scalar @_ == 1 and ref($_[0]) eq 'HASH') ? %{$_[0]} : @_; while ( scalar @args ) { local $_ = shift(@args); $self->$_( shift(@args) ) } }, }, 'modifier' => { 'self_closure' => q{ my @args = @_; return sub { unshift @_, @args; * } }, 'warn_calls' => q{ warn $self."->_STATIC_ATTR_{name}(".join(', ',@_).")\n"; * }, 'public' => q{ * }, 'private' => q{ _ACCESS_PRIVATE_; * }, 'protected' => q{ _ACCESS_PROTECTED_; * }, '-folding' => [ # Public is the default; all three options are mutually exclusive. '-public' => '', '-private -public' => '-public', '-protected -public' => '-public', '-private -protected' => '-protected', '-protected -private' => '-private', ], 'lvalue' => { _SUB_ATTRIBS_ => ': lvalue' }, }, 'behavior' => { -import => { 'Template::Universal:no_op' => 'no_op', 'Template::Universal:croak' => 'croak', 'Template::Universal:method_init' => 'method_init', }, attributes => sub { my $m_info = $_[0]; return sub { my $self = shift; if ( scalar @_ == 0 ) { return $m_info; } elsif ( scalar @_ == 1 ) { return $m_info->{ shift() }; } else { %$m_info = ( %$m_info, @_ ); } } }, }, } } ######################################################################## =head2 no_op For each meta-method, creates a method with an empty body. use Class::MakeMethods::Template::Universal ( 'no_op' => [ 'foo bar baz' ], ); You might want to create and use such methods to provide hooks for subclass activity. No interfaces or parameters supported. =cut sub no_op { { 'interface' => { default => 'no_op', 'no_op' => 'no_op' }, 'behavior' => { no_op => sub { my $m_info = $_[0]; sub { } }, }, } } ######################################################################## =head2 croak For each meta-method, creates a method which will croak if called. use Class::MakeMethods::Template::Universal ( 'croak' => [ 'foo bar baz' ], ); 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. The -unsupported and -prohibited interfaces provide alternate error messages, or a custom error message can be provided using the 'croak_msg' parameter. =cut sub abstract { 'croak --abstract' } sub croak { { 'interface' => { default => 'croak', 'croak' => 'croak', 'abstract' => { '*'=>'croak', -params=> { 'croak_msg' => q/Can't locate abstract method "*" declared in "*{target_class}", called from "CALLCLASS"./ } }, 'abstract_minimal' => { '*'=>'croak', -params=> { 'croak_msg' => "The * method is abstract and can not be called" } }, 'unsupported' => { '*'=>'croak', -params=> { 'croak_msg' => "The * method does not support this operation" } }, 'prohibited' => { '*'=>'croak', -params=> { 'croak_msg' => "The * method is not allowed to perform this activity" } }, }, 'behavior' => { croak => sub { my $m_info = $_[0]; sub { $m_info->{'croak_msg'} =~ s/CALLCLASS/ ref( $_[0] ) || $_[0] /ge if $m_info->{'croak_msg'}; Carp::croak( $m_info->{'croak_msg'} ); } }, }, } } ######################################################################## =head2 method_init Creates a method that 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. Sample declaration and usage: package MyObject; use Class::MakeMethods::Template::Universal ( method_init => 'init', ); ... my $object = MyObject->new() $object->init( foo => 'Foozle', bar => 'Barbados' ); # Equivalent to: $object->foo('Foozle'); $object->bar('Barbados'); You might want to create and use such methods to allow easy initialization of multiple object or class parameters in a single call. B: including methods of this type will circumvent the protection of C and C methods, because it an outside caller can cause an object to call specific methods on itself, bypassing the privacy protection. =cut sub method_init { { 'interface' => { default => 'method_init', 'method_init' => 'method_init' }, 'code_expr' => { '-import' => { 'Template::Universal:generic' => '*' }, }, 'behavior' => { method_init => q{ _CALL_METHODS_FROM_HASH_ return $self; } }, } } ######################################################################## =head2 forward_methods Creates a method which delegates to an object provided by another method. Example: use Class::MakeMethods::Template::Universal forward_methods => [ --target=> 'whistle', w, [ 'x', 'y' ], { target=> 'xylophone' }, { name=>'z', target=>'zither', target_args=>[123], method_name=>do_zed }, ]; Example: The above defines that method C will be handled by the calling C on the object returned by C, whilst methods C and C will be handled by C, and method C will be handled by calling C on the object returned by calling C. B: =over 4 =item forward (default) Calls the method on the target object. If the target object is missing, croaks at runtime with a message saying "Can't forward bar because bar is empty." =item delegate Calls the method on the target object, if present. If the target object is missing, returns nothing. =back B: The following additional parameters are supported: =over 4 =item target I. The name of the method that will provide the object that will handle the operation. =item target_args Optional ref to an array of arguments to be passed to the target method. =item method_name The name of the method to call on the handling object. Defaults to the name of the meta-method being created. =back =cut sub forward_methods { { 'interface' => { default => 'forward', 'forward' => 'forward' }, 'params' => { 'method_name' => '*' }, 'behavior' => { 'forward' => sub { my $m_info = $_[0]; sub { my $target = $m_info->{'target'}; my @args = $m_info->{'target_args'} ? @{$m_info->{'target_args'}} : (); my $obj = (shift)->$target(@args) or Carp::croak("Can't forward $m_info->{name} because $m_info->{target} is empty"); my $method = $m_info->{'method_name'}; $obj->$method(@_); }}, 'delegate' => sub { my $m_info = $_[0]; sub { my $target = $m_info->{'target'}; my @args = $m_info->{'target_args'} ? @{$m_info->{'target_args'}} : (); my $obj = (shift)->$target(@args) or return; my $method = $m_info->{'method_name'}; $obj->$method(@_); }}, }, } } ######################################################################## =head1 SEE ALSO See L for general information about this distribution. See L for information about this family of subclasses. =cut 1; libclass-makemethods-perl-1.01.orig/MakeMethods/Template.pm0000644000175000017500000011604310117146562022375 0ustar ericericpackage 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