Class-Std-Utils-v0.0.3/0000755000076600007660000000000010722071020014406 5ustar macbookmacbookClass-Std-Utils-v0.0.3/Build.PL0000644000076600007660000000076710243741007015723 0ustar macbookmacbookuse strict; use warnings; use Module::Build; my $builder = Module::Build->new( module_name => 'Class::Std::Utils', license => 'perl', dist_author => 'Damian Conway ', dist_version_from => 'lib/Class/Std/Utils.pm', requires => { 'Test::More' => 0, 'List::Util' => 0, 'Scalar::Util' => 0, 'version' => 0, }, add_to_cleanup => [ 'Class-Std-Utils-*' ], ); $builder->create_build_script(); Class-Std-Utils-v0.0.3/Changes0000644000076600007660000000032510722050452015707 0ustar macbookmacbookRevision history for Class-Std-Utils 0.0.1 Sat Apr 9 09:54:03 2005 Initial release. 0.0.2 Sun May 22 05:44:09 2005 - added dependency on version.pm 0.0.3 Sat Nov 24 10:05:42 2007 - rt 18163Class-Std-Utils-v0.0.3/lib/0000755000076600007660000000000010722071020015154 5ustar macbookmacbookClass-Std-Utils-v0.0.3/lib/Class/0000755000076600007660000000000010722071020016221 5ustar macbookmacbookClass-Std-Utils-v0.0.3/lib/Class/Std/0000755000076600007660000000000010722071020016753 5ustar macbookmacbookClass-Std-Utils-v0.0.3/lib/Class/Std/Utils.pm0000644000076600007660000001622610722046001020421 0ustar macbookmacbookpackage Class::Std::Utils; use version; $VERSION = qv('0.0.3'); use warnings; use strict; use Carp; use Scalar::Util qw( refaddr ); sub import { my $caller = caller; no strict qw( refs ); *{ $caller . '::anon_scalar' } = \&anon_scalar; *{ $caller . '::ident' } = \&refaddr; *{ $caller . '::extract_initializers_from' } = \&extract_initializers_from; } sub anon_scalar { return \my $scalar; } use List::Util qw( first ); sub extract_initializers_from { my ($arg_ref) = @_; my $class_name = caller; # Find the class-specific sub-hash (if any)... my $specific_inits_ref = first {defined $_} $arg_ref->{$class_name}, {}; croak "$class_name initializer must be a nested hash" if ref $specific_inits_ref ne 'HASH'; # Return initializers, overriding general initializers from the top level # with any second-level initializers that are specific to the class.... return ( %{$arg_ref}, %{$specific_inits_ref} ); } 1; # Magic true value required at end of module __END__ =head1 NAME Class::Std::Utils - Utility subroutines for building "inside-out" objects =head1 VERSION This document describes Class::Std::Utils version 0.0.3 =head1 SYNOPSIS use Class::Std::Utils; # Constructor for anonymous scalars... my $new_object = bless anon_scalar(), $class; # Convert an object reference into a unique ID number... my $ID_num = ident $new_object; # Extract class-specific arguments from a hash reference... my %args = extract_initializers_from($arg_ref); =head1 DESCRIPTION This module provides three utility subroutines that simplify the creation of "inside-out" classes. See Chapters 15 and 16 of "Perl Best Practices" (O'Reilly, 2005) for details. =head1 INTERFACE =over =item C This subroutine is always exported. It takes no arguments and returns a reference to an anonymous scalar, suitable for blessing as an object. =item C This subroutine is always exported. It takes one argument--a reference-- and acts exactly like the C, returning a unique integer value suitable for identifying the referent. =item C This subroutine is always exported. It takes one argument--a hash reference-- and returns a "flattened" set of key/value pairs extracted from that hash. The typical usage is: my %class_specific_args = extract_initializers_from($args_ref); The argument hash is flattened as described in Chapter 16 of "Perl Best Practices": =over I package) in the argument hash, to see if an initializer with that key has been defined. Finally, C returns the flattened set of key/value pairs for the class's initializer set, by appending the class-specific initializer subhash to the end of the original generic initializer hash. Appending the specific initializers after the generic ones means that any key in the class- specific set will override any key in the generic set, thereby ensuring that the most relevant initializers are always selected, but that generic initializers are still available where no class-specific value has been passed in.> =back In other words, given: my $arg_ref = { key_1 => 'generic value 1', key_2 => 'generic value 2', 'Base::Class' => { key_1 => 'base value 1' }, 'Der::Class' => { key_1 => 'der value 1' key_2 => 'der value 2' }, }; package Base::Class; use Class::Std::Utils; my %base_args = extract_initializers_from($arg_ref); package Der::Class; use Class::Std::Utils; my %der_args = extract_initializers_from($arg_ref); then C<%base_args> would be initialized to: ( key_1 => 'base value 1', key_2 => 'generic value 2', 'Base::Class' => { key_1 => 'base value 1', }, 'Der::Class' => { key_1 => 'der value 1', key_2 => 'der value 2', }, ) whilst C<%der_args> would be initialized to: ( key_1 => 'der value 1', key_2 => 'der value 2', 'Base::Class' => { key_1 => 'base value 1', }, 'Der::Class' => { key_1 => 'der value 1', key_2 => 'der value 2', }, ) That is, the top-level entries would be replaced by any second-level entries with the same key that appear in a top-level entry of the same name as the calling package. This means that each class can just refer to C<$args{key_1}> and C<$args{key_2}> and be confident that the resulting values will be the most specific available for that class. =back =head1 DIAGNOSTICS =over =item C<< %s initializer must be a nested hash >> Thrown by C. You specified a top-level key that has the same name of the current class, but the value of that key wasn't a hash reference. =back =head1 CONFIGURATION AND ENVIRONMENT Class::Std::Utils requires no configuration files or environment variables. =head1 DEPENDENCIES Thsi module requires both the C and C modules, which are standard in Perl 5.8 and available from the CPAN for earlier versions of Perl. =head1 INCOMPATIBILITIES None reported. =head1 SEE ALSO The C module "Perl Best Practices", O'Reilly, 2005. =head1 BUGS AND LIMITATIONS No bugs have been reported. Please report any bugs or feature requests to C, or through the web interface at L. =head1 AUTHOR Damian Conway C<< >> =head1 LICENCE AND COPYRIGHT Copyright (c) 2005, Damian Conway C<< >>. All rights reserved. This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself. =head1 DISCLAIMER OF WARRANTY BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR, OR CORRECTION. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. Class-Std-Utils-v0.0.3/Makefile.PL0000644000076600007660000000111310243741003016357 0ustar macbookmacbookuse strict; use warnings; use ExtUtils::MakeMaker; WriteMakefile( NAME => 'Class::Std::Utils', AUTHOR => 'Damian Conway ', VERSION_FROM => 'lib/Class/Std/Utils.pm', ABSTRACT_FROM => 'lib/Class/Std/Utils.pm', PL_FILES => {}, PREREQ_PM => { 'Test::More' => 0, 'List::Util' => 0, 'Scalar::Util' => 0, 'version' => 0, }, dist => { COMPRESS => 'gzip -9f', SUFFIX => 'gz', }, clean => { FILES => 'Class-Std-Utils-*' }, ); Class-Std-Utils-v0.0.3/MANIFEST0000644000076600007660000000031310243017332015540 0ustar macbookmacbookBuild.PL Changes MANIFEST META.yml # Will be created by "make dist" Makefile.PL README lib/Class/Std/Utils.pm t/00.load.t t/anon_scalar.t t/extract_initializers_from.t t/ident.t t/pod-coverage.t t/pod.t Class-Std-Utils-v0.0.3/META.yml0000644000076600007660000000072010722071020015656 0ustar macbookmacbook# http://module-build.sourceforge.net/META-spec.html #XXXXXXX This is a prototype!!! It will change in the future!!! XXXXX# name: Class-Std-Utils version: v0.0.3 version_from: lib/Class/Std/Utils.pm installdirs: site requires: List::Util: 0 Scalar::Util: 0 Test::More: 0 version: 0 distribution_type: module generated_by: ExtUtils::MakeMaker version 6.30 Class-Std-Utils-v0.0.3/README0000644000076600007660000000140310722046022015270 0ustar macbookmacbookClass::Std::Utils version 0.0.3 This module provides three utility subroutines: anon_scalar() ident($some_ref) extract_initializers_from(\%hash) These subs simplify the creation of "inside-out" classes. See Chapters 15 and 16 of "Perl Best Practices" (O'Reilly, 2005) for details. INSTALLATION To install this module, run the following commands: perl Makefile.PL make make test make install Alternatively, to install with Module::Build, you can use the following commands: perl Build.PL ./Build ./Build test ./Build install DEPENDENCIES None. COPYRIGHT AND LICENCE Copyright (C) 2005, Damian Conway This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. Class-Std-Utils-v0.0.3/t/0000755000076600007660000000000010722071020014651 5ustar macbookmacbookClass-Std-Utils-v0.0.3/t/00.load.t0000644000076600007660000000020710225723073016204 0ustar macbookmacbookuse Test::More tests => 1; BEGIN { use_ok( 'Class::Std::Utils' ); } diag( "Testing Class::Std::Utils $Class::Std::Utils::VERSION" ); Class-Std-Utils-v0.0.3/t/anon_scalar.t0000644000076600007660000000047610243003156017330 0ustar macbookmacbookuse Class::Std::Utils; use Test::More 'no_plan'; my $anon_1 = anon_scalar(); my $anon_2 = anon_scalar(); is ref($anon_1), 'SCALAR' => 'anon_scalar returns scalar ref'; ok !defined(${$anon_1}) => 'anon_scalar returns empty scalar'; ok $anon_1 != $anon_2 => 'anon_scalar returns distinct ref'; Class-Std-Utils-v0.0.3/t/extract_initializers_from.t0000644000076600007660000000413410243005037022326 0ustar macbookmacbookuse Test::More 'no_plan'; my %hash = ( top_1 => 'def val top 1', top_2 => 'def val top 2', top_3 => 'def val top 3', Classname_1 => { top_2 => 'class 1 val top 2', }, Classname_2 => { top_1 => 'class 2 val top 1', top_2 => 'class 2 val top 2', top_3 => 'class 2 val top 3', }, Bad => 'class', ); my %orig_hash = %hash; my %expect_1 = ( top_1 => 'def val top 1', top_2 => 'class 1 val top 2', top_3 => 'def val top 3', Classname_1 => { top_2 => 'class 1 val top 2', }, Classname_2 => { top_1 => 'class 2 val top 1', top_2 => 'class 2 val top 2', top_3 => 'class 2 val top 3', }, Bad => 'class', ); my %expect_2 = ( top_1 => 'class 2 val top 1', top_2 => 'class 2 val top 2', top_3 => 'class 2 val top 3', Classname_1 => { top_2 => 'class 1 val top 2', }, Classname_2 => { top_1 => 'class 2 val top 1', top_2 => 'class 2 val top 2', top_3 => 'class 2 val top 3', }, Bad => 'class', ); package Classname_1; use Class::Std::Utils; my %args1 = extract_initializers_from(\%hash); ::is_deeply \%hash, \%orig_hash => 'Original args unchanged in class 1'; ::is_deeply \%args1, \%expect_1 => 'Extracted as expected in class 1'; package Classname_2; use Class::Std::Utils; my %args2 = extract_initializers_from(\%hash); ::is_deeply \%hash, \%orig_hash => 'Original args unchanged in class 2'; ::is_deeply \%args2, \%expect_2 => 'Extracted as expected in class 2'; package Classname_3; use Class::Std::Utils; my %args3 = extract_initializers_from(\%hash); ::is_deeply \%hash, \%orig_hash => 'Original args unchanged in class 2'; ::is_deeply \%args3, \%hash => 'Extracted as expected in class 3'; package Bad; use Class::Std::Utils; ::ok !eval { extract_initializers_from(\%hash); 1 } => 'Exception for bad specification'; ::like $@, qr/^Bad initializer must be a nested hash/ => 'Correct exception thrown'; Class-Std-Utils-v0.0.3/t/ident.t0000644000076600007660000000074310243005075016152 0ustar macbookmacbookuse Scalar::Util 'refaddr'; use Class::Std::Utils; use Test::More 'no_plan'; my @objects = ( do{\my $scalar}, { hash => 'anon' }, [ 1..10 ], sub {}, qr//, ); for my $ref (@objects) { is ident $ref, refaddr $ref => 'ident acts like refaddr on '.ref $ref; is ident $ref, int $ref => 'ident acts like int on '.ref $ref; } bless $_ for @objects; for my $ref (@objects) { is ident $ref, refaddr $ref => 'ident acts like refaddr on object'; } Class-Std-Utils-v0.0.3/t/pod-coverage.t0000644000076600007660000000025410225723073017424 0ustar macbookmacbook#!perl -T use Test::More; eval "use Test::Pod::Coverage 1.04"; plan skip_all => "Test::Pod::Coverage 1.04 required for testing POD coverage" if $@; all_pod_coverage_ok(); Class-Std-Utils-v0.0.3/t/pod.t0000644000076600007660000000021410225723073015627 0ustar macbookmacbook#!perl -T use Test::More; eval "use Test::Pod 1.14"; plan skip_all => "Test::Pod 1.14 required for testing POD" if $@; all_pod_files_ok();