Class-Mix-0.006000755001750001750 013137410112 13243 5ustar00zeframzefram000000000000Class-Mix-0.006/.gitignore000444001750001750 16213137410104 15350 0ustar00zeframzefram000000000000/Build /Makefile /_build /blib /META.json /META.yml /MYMETA.json /MYMETA.yml /Makefile.PL /SIGNATURE /Class-Mix-* Class-Mix-0.006/Build.PL000444001750001750 160113137410104 14673 0ustar00zeframzefram000000000000{ use 5.006; } use warnings; use strict; use Module::Build; Module::Build->new( module_name => "Class::Mix", license => "perl", configure_requires => { "Module::Build" => 0, "perl" => "5.006", "strict" => 0, "warnings" => 0, }, build_requires => { "Module::Build" => 0, "Test::More" => 0, "perl" => "5.006", "strict" => 0, "warnings" => 0, }, requires => { "Carp" => 0, "Exporter" => 0, "Params::Classify" => 0, "constant" => 0, "if" => 0, "parent" => 0, "perl" => "5.006", "strict" => 0, "warnings" => 0, }, dynamic_config => 0, meta_add => { distribution_type => "module" }, meta_merge => { "meta-spec" => { version => "2" }, resources => { bugtracker => { mailto => "bug-Class-Mix\@rt.cpan.org", web => "https://rt.cpan.org/Public/Dist/". "Display.html?Name=Class-Mix", }, }, }, sign => 1, )->create_build_script; 1; Class-Mix-0.006/Changes000444001750001750 324313137410104 14676 0ustar00zeframzefram000000000000version 0.006; 2017-07-30 * no longer include a Makefile.PL in the distribution * in documentation, use four-column indentation for all verbatim material * in META.{yml,json}, point to public bug tracker version 0.005; 2011-10-28 * include META.json in distribution * convert .cvsignore to .gitignore * add MYMETA.json to .gitignore version 0.004; 2010-09-01 * mro option for mix_class(), to control the method resolution order of the mixture class * prefix option for mix_class(), to control where the mixture class is located * document that PREFIX missing from genpkg() means "don't care", and change the actual default from "" to "Class::Mix::" * check for required Perl version at runtime * remove extra block around each sub definition, for readability * use full stricture in test suite * in Build.PL, explicitly declare configure-time requirements * remove bogus "exit 0" from Build.PL * add MYMETA.yml to .cvsignore version 0.003; 2009-05-19 * bugfix: avoid unreliable "\w" in parsing PREFIX parameter for genpkg() * use simpler "parent" pragma in place of "base" version 0.002; 2009-03-22 * use "base" pragma to import Exporter behaviour * test POD syntax and coverage, and make a small style change in documentation to satisfy the coverage test * build with Module::Build instead of ExtUtils::MakeMaker * complete dependency list * include signature in distribution * in documentation, separate "license" section from "copyright" section version 0.001; 2006-08-12 * PREFIX parameter for genpkg() to allow it to root packages anywhere version 0.000; 2004-06-27 * initial released version Class-Mix-0.006/MANIFEST000444001750001750 27313137410104 14514 0ustar00zeframzefram000000000000.gitignore Build.PL Changes MANIFEST META.json META.yml README lib/Class/Mix.pm t/genpkg.t t/mix.t t/mro_no.t t/mro_yes.t t/pod_cvg.t t/pod_syn.t SIGNATURE Added here by Module::Build Class-Mix-0.006/META.json000444001750001750 310313137410104 15017 0ustar00zeframzefram000000000000{ "abstract" : "dynamic class mixing", "author" : [ "Andrew Main (Zefram) " ], "dynamic_config" : 0, "generated_by" : "Module::Build version 0.4224", "license" : [ "perl_5" ], "meta-spec" : { "url" : "http://search.cpan.org/perldoc?CPAN::Meta::Spec", "version" : 2 }, "name" : "Class-Mix", "prereqs" : { "build" : { "requires" : { "Module::Build" : "0", "Test::More" : "0", "perl" : "5.006", "strict" : "0", "warnings" : "0" } }, "configure" : { "requires" : { "Module::Build" : "0", "perl" : "5.006", "strict" : "0", "warnings" : "0" } }, "runtime" : { "requires" : { "Carp" : "0", "Exporter" : "0", "Params::Classify" : "0", "constant" : "0", "if" : "0", "parent" : "0", "perl" : "5.006", "strict" : "0", "warnings" : "0" } } }, "provides" : { "Class::Mix" : { "file" : "lib/Class/Mix.pm", "version" : "0.006" } }, "release_status" : "stable", "resources" : { "bugtracker" : { "mailto" : "bug-Class-Mix@rt.cpan.org", "web" : "https://rt.cpan.org/Public/Dist/Display.html?Name=Class-Mix" }, "license" : [ "http://dev.perl.org/licenses/" ] }, "version" : "0.006", "x_serialization_backend" : "JSON::PP version 2.93" } Class-Mix-0.006/META.yml000444001750001750 163313137410104 14655 0ustar00zeframzefram000000000000--- abstract: 'dynamic class mixing' author: - 'Andrew Main (Zefram) ' build_requires: Module::Build: '0' Test::More: '0' perl: '5.006' strict: '0' warnings: '0' configure_requires: Module::Build: '0' perl: '5.006' strict: '0' warnings: '0' dynamic_config: 0 generated_by: 'Module::Build version 0.4224, CPAN::Meta::Converter version 2.150010' license: perl meta-spec: url: http://module-build.sourceforge.net/META-spec-v1.4.html version: '1.4' name: Class-Mix provides: Class::Mix: file: lib/Class/Mix.pm version: '0.006' requires: Carp: '0' Exporter: '0' Params::Classify: '0' constant: '0' if: '0' parent: '0' perl: '5.006' strict: '0' warnings: '0' resources: bugtracker: https://rt.cpan.org/Public/Dist/Display.html?Name=Class-Mix license: http://dev.perl.org/licenses/ version: '0.006' x_serialization_backend: 'CPAN::Meta::YAML version 0.012' Class-Mix-0.006/README000444001750001750 77513137410104 14252 0ustar00zeframzefram000000000000NAME Class::Mix - dynamic class mixing DESCRIPTION The "mix_class" function provided by this module dynamically generates `anonymous' classes with specified inheritance. INSTALLATION perl Build.PL ./Build ./Build test ./Build install AUTHOR Andrew Main (Zefram) COPYRIGHT Copyright (C) 2004, 2006, 2009, 2010, 2011, 2017 Andrew Main (Zefram) LICENSE This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself. Class-Mix-0.006/SIGNATURE000644001750001750 274113137410112 14672 0ustar00zeframzefram000000000000This file contains message digests of all files listed in MANIFEST, signed via the Module::Signature module, version 0.81. To verify the content in this distribution, first make sure you have Module::Signature installed, then type: % cpansign -v It will check each file's integrity, as well as the signature's validity. If "==> Signature verified OK! <==" is not displayed, the distribution may already have been compromised, and you should not run its Makefile.PL or Build.PL. -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 SHA1 b4127de8d0fc5e8f5aed4ba0da24b1577ab7f6ca .gitignore SHA1 8256d0f261a77a1acf1c26073812f12d1745e3d6 Build.PL SHA1 a4c0ac165f8ea7f933bdc4f33ccd9eaaac11806f Changes SHA1 c182cd507be6faf2baac25be5783f31543cad48c MANIFEST SHA1 aa639b63bc31c223ee23a6e240c8e2e99c989eb3 META.json SHA1 9e2b6e6c49e06bf2470d5030c6b4d49d6a0398de META.yml SHA1 180e49841d81d3c43ace88f77ea9cebdba351b06 README SHA1 905b1a32374ede0f0f1704178249380c92025392 lib/Class/Mix.pm SHA1 f48777d87c382c84167a8c32d541ffe8985cb78c t/genpkg.t SHA1 cdcfc35677399d607fbcec9e1c55ec0d1238e824 t/mix.t SHA1 2f928dfd47fc5a7d29f9f3cff78a544421c51380 t/mro_no.t SHA1 b242f533d8e0c85541da36d765e80f227fc7a92f t/mro_yes.t SHA1 904d9a4f76525e2303e4b0c168c68230f223c8de t/pod_cvg.t SHA1 65c75abdef6f01a5d1588a307f2ddfe2333dc961 t/pod_syn.t -----BEGIN PGP SIGNATURE----- Version: GnuPG v1 iEYEARECAAYFAll+EEQACgkQOV9mt2VyAVGZXACfedg37Hc5yOmVk7yliTpeIzHo BdAAn1GYMbtIPKcGWXDtzw82VQJePcqm =Kkam -----END PGP SIGNATURE----- Class-Mix-0.006/lib000755001750001750 013137410104 14012 5ustar00zeframzefram000000000000Class-Mix-0.006/lib/Class000755001750001750 013137410104 15057 5ustar00zeframzefram000000000000Class-Mix-0.006/lib/Class/Mix.pm000444001750001750 1534713137410104 16341 0ustar00zeframzefram000000000000=head1 NAME Class::Mix - dynamic class mixing =head1 SYNOPSIS use Class::Mix qw(mix_class); $foobar_object = mix_class("Foo", "Bar")->new; $digest_class = mix_class("Foo", "Bar", {prefix=>"Digest::"}); use Class::Mix qw(genpkg); $package = genpkg; $package = genpkg("Digest::Foo::"); =head1 DESCRIPTION The C function provided by this module dynamically generates `anonymous' classes with specified inheritance. =cut package Class::Mix; { use 5.006; } use warnings; use strict; use constant _DO_MRO => "$]" >= 5.009005; use Carp qw(croak); use Params::Classify 0.000 qw(is_undef is_string is_ref); use if _DO_MRO, "mro"; our $VERSION = "0.006"; use parent "Exporter"; our @EXPORT_OK = qw(mix_class genpkg); BEGIN { if(_DO_MRO) { *_get_mro = \&mro::get_mro; } else { *_get_mro = sub ($) { "dfs" }; } } my $prefix_rx = qr/(?:[a-zA-Z_][0-9a-zA-Z_]*::(?:[0-9a-zA-Z_]+::)*)?/; =head1 FUNCTIONS =over =item mix_class(ITEMS ...) This function is used to dynamically generate `anonymous' classes by mixing pre-existing classes. This is useful where an incomplete class requires use of a mixin in order to become instantiable, several suitable mixins are available, and it is desired to make the choice between mixins at runtime. Each I in the argument list is either the name of a class to inherit from (a parent class) or a reference to a hash of options. The C<@ISA> list of the mixture class is set to the list of parent class names, in the order supplied. The options that may be supplied are: =over =item B Specifies the desired method resolution order (MRO) of the mixture class. See L for details of the valid values and the default determined by Perl. Typically, this should be set to B if mixing into an existing C3-based class hierarchy. =item B Specifies where the resulting package will go. May be C to indicate that the caller doesn't care (which is the default state). Otherwise it must be either the empty string (to create a top-level package) or a bareword followed by "::" (to create a package under that name). For example, "Digest::" could be specified to ensure that the resulting package has a name starting with "Digest::", so that C<< Digest->new >> will accept it as the name of a message digest algorithm. =back The function generates a class of the form described by the arguments, and returns its name. The same class will be returned by repeated invocations with the same parent class list and options. The returned name may be used to call a constructor or other class methods of the mixed class. A class name must be returned because there is no such thing as an anonymous class in Perl. Classes are referenced by name. The names that are generated by this function are unique and insignificant. See C below for more information. If fewer than two classes to inherit from are specified, the function tries to avoid generating a separate class for the mixture. If only one parent class is specified then that class may be returned, and if no parent classes are specified then C may be returned. This provides the desired inheritance without creating superfluous classes. These special cases only apply if the options are compatible with the pre-existing class. This function relies on the classes it returns remaining unmodified in order to be returned by future invocations. If you want to modify your dynamically-generated `anonymous' classes, use C (below). =cut sub genpkg(;$); my %mixtures; sub mix_class(@) { my @parents; my %options; foreach(@_) { if(is_string($_)) { push @parents, $_; } elsif(is_ref($_, "HASH")) { foreach my $k (keys %$_) { croak "clashing option `$k'" if exists $options{$k}; $options{$k} = $_->{$k}; } } else { croak "bad argument for mix_class"; } } foreach(keys %options) { croak "bad option `$_' for mix_class" unless /\A(?:mro|prefix)\z/; } $options{mro} = "dfs" unless exists $options{mro}; croak "bad mro value" unless is_string($options{mro}); $options{prefix} = undef unless exists $options{prefix}; croak "bad prefix value" unless is_undef($options{prefix}) || (is_string($options{prefix}) && $options{prefix} =~ /\A$prefix_rx\z/o); return "UNIVERSAL" if @parents == 0 && $options{mro} eq _get_mro("UNIVERSAL") && (is_undef($options{prefix}) || $options{prefix} eq ""); return $parents[0] if @parents == 1 && $options{mro} eq _get_mro($parents[0]) && (is_undef($options{prefix}) || $parents[0] =~ /\A\Q$options{prefix}\E[^:]*\z/); $options{prefix} = "Class::Mix::" unless defined $options{prefix}; my $recipe = join("", map { length($_)."_".$_ } $options{mro}, $options{prefix}, @parents); return $mixtures{$recipe} ||= do { my $pkg = genpkg($options{prefix}); no strict "refs"; @{$pkg."::ISA"} = @parents; mro::set_mro($pkg, $options{mro}) if $options{mro} ne "dfs"; $pkg; }; } =item genpkg([PREFIX]) This function selects and returns a package name that has not been previously used. The name returned is an ordinary bareword-form package name, and can be used as the second argument to C and in all other ways that package names are used. The package is initially empty. The package names returned by this function are of a type that should not be used as ordinary fixed module names. However, it is not possible to entirely prevent a clash. This function checks that the package name it is about to return has not already been used, and will avoid returning such names, but it cannot guarantee that a later-loaded module will not create a clash. PREFIX, if present, specifies where the resulting package will go. It must be either the empty string (to create a top-level package) or a bareword followed by "::" (to create a package under that name). For example, "Digest::" could be specified to ensure that the resulting package has a name starting with "Digest::", so that C<< Digest->new >> will accept it as the name of a message digest algorithm. If the PREFIX is not supplied, the caller is not expressing any preference. =cut my $n = 0; sub genpkg(;$) { my($prefix) = @_; $prefix = "Class::Mix::" unless defined $prefix; croak "`$prefix' is not a valid module name prefix" unless $prefix =~ /\A$prefix_rx\z/o; no strict "refs"; my $pkgtail; do { $pkgtail = "__GP".$n++; } while(exists ${$prefix || "::"}{$pkgtail."::"}); my $pkgname = $prefix.$pkgtail; %{$pkgname."::"} = (); return $pkgname; } =back =head1 SEE ALSO L, L =head1 AUTHOR Andrew Main (Zefram) =head1 COPYRIGHT Copyright (C) 2004, 2006, 2009, 2010, 2011, 2017 Andrew Main (Zefram) =head1 LICENSE This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself. =cut 1; Class-Mix-0.006/t000755001750001750 013137410104 13507 5ustar00zeframzefram000000000000Class-Mix-0.006/t/genpkg.t000444001750001750 61613137410104 15267 0ustar00zeframzefram000000000000use warnings; use strict; use Test::More tests => 7; BEGIN { use_ok "Class::Mix", qw(genpkg); } { no warnings; $__GP1::foo = 1; $Foo::Bar::__GP4::foo = 1; $Class::Mix::__GP7::foo = 1; } is genpkg(""), "__GP0"; is genpkg(""), "__GP2"; is genpkg("Foo::Bar::"), "Foo::Bar::__GP3"; is genpkg("Foo::Bar::"), "Foo::Bar::__GP5"; is genpkg, "Class::Mix::__GP6"; is genpkg, "Class::Mix::__GP8"; 1; Class-Mix-0.006/t/mix.t000444001750001750 513513137410104 14632 0ustar00zeframzefram000000000000use warnings; use strict; use Test::More tests => 50; BEGIN { use_ok "Class::Mix", qw(mix_class); } { package Foo; } { package Bar; } { package Baz; } { package Quux::Wibble; } # error cases foreach( [], {foo=>1}, {prefix=>"1"}, ) { eval { mix_class($_) }; isnt $@, ""; eval { mix_class("Foo", $_) }; isnt $@, ""; } # trivial cases is(mix_class(), "UNIVERSAL"); is(mix_class("Foo"), "Foo"); is(mix_class("Quux::Wibble"), "Quux::Wibble"); # basic mixing cases no strict "refs"; my $foobar = mix_class("Foo", "Bar"); is_deeply(\@{$foobar."::ISA"}, ["Foo", "Bar"]); ok($foobar->isa("Foo")); ok($foobar->isa("Bar")); ok(!$foobar->isa("Baz")); my $barfoo = mix_class("Bar", "Foo"); is_deeply(\@{$barfoo."::ISA"}, ["Bar", "Foo"]); ok($barfoo->isa("Foo")); ok($barfoo->isa("Bar")); ok(!$barfoo->isa("Baz")); # with blank options is mix_class({}), "UNIVERSAL"; is mix_class({}, "Foo"), "Foo"; is mix_class("Foo", {}), "Foo"; is mix_class({}, "Foo", "Bar"), $foobar; is mix_class("Foo", {}, "Bar"), $foobar; is mix_class("Foo", "Bar", {}), $foobar; # with blank prefix my $u_u = mix_class({ prefix=>"" }); is $u_u, "UNIVERSAL"; my $u_foo = mix_class({ prefix=>"" }, "Foo"); is $u_foo, "Foo"; my $u_foobar = mix_class("Foo", "Bar", { prefix=>"" }); like $u_foobar, qr/\A[^:]+\z/; is_deeply(\@{$u_foobar."::ISA"}, ["Foo", "Bar"]); my $u_wibble = mix_class("Quux::Wibble", { prefix=>"" }); like $u_wibble, qr/\A[^:]+\z/; is_deeply(\@{$u_wibble."::ISA"}, ["Quux::Wibble"]); # with non-blank prefix my $quux_u = mix_class({ prefix=>"Quux::" }); like $quux_u, qr/\AQuux::[^:]+\z/; is_deeply(\@{$quux_u."::ISA"}, []); my $quux_foo = mix_class({ prefix=>"Quux::" }, "Foo"); like $quux_foo, qr/\AQuux::[^:]+\z/; is_deeply(\@{$quux_foo."::ISA"}, ["Foo"]); my $quux_foobar = mix_class("Foo", "Bar", { prefix=>"Quux::" }); like $quux_foobar, qr/\AQuux::[^:]+\z/; is_deeply(\@{$quux_foobar."::ISA"}, ["Foo", "Bar"]); my $quux_wibble = mix_class("Quux::Wibble", { prefix=>"Quux::" }); is $quux_wibble, "Quux::Wibble"; # consistency of results is(mix_class(), "UNIVERSAL"); is(mix_class("Foo"), "Foo"); is(mix_class("Quux::Wibble"), "Quux::Wibble"); is(mix_class("Foo", "Bar"), $foobar); is(mix_class("Bar", "Foo"), $barfoo); is(mix_class({ prefix=>"" }), $u_u); is(mix_class("Foo", { prefix=>"" }), $u_foo); is(mix_class("Foo", { prefix=>"" }, "Bar"), $u_foobar); is(mix_class("Quux::Wibble", { prefix=>"" }), $u_wibble); is(mix_class({ prefix=>"Quux::" }), $quux_u); is(mix_class("Foo", { prefix=>"Quux::" }), $quux_foo); is(mix_class({ prefix=>"Quux::" }, "Foo", "Bar"), $quux_foobar); is(mix_class("Quux::Wibble", { prefix=>"Quux::" }), $quux_wibble); 1; Class-Mix-0.006/t/mro_no.t000444001750001750 145013137410104 15322 0ustar00zeframzefram000000000000use warnings; use strict; BEGIN { if("$]" >= 5.009005) { require Test::More; Test::More::plan(skip_all => "MRO available on this Perl"); } } use Test::More tests => 18; BEGIN { use_ok "Class::Mix", qw(mix_class); } { package Foo; } { package Bar; } { package Baz; } # error cases foreach( {mro=>[]}, {mro=>undef}, {mro=>"wibble"}, {mro=>"c3"}, ) { eval { mix_class($_) }; isnt $@, ""; eval { mix_class("Foo", $_) }; isnt $@, ""; eval { mix_class("Foo", "Bar", $_) }; isnt $@, ""; } # OK cases no strict "refs"; is mix_class({mro=>"dfs"}), "UNIVERSAL"; is mix_class("Foo", {mro=>"dfs"}), "Foo"; my $foobar = mix_class("Foo", "Bar", {mro=>"dfs"}); is_deeply(\@{$foobar."::ISA"}, ["Foo", "Bar"]); is mix_class({mro=>"dfs"}, "Foo", "Bar"), $foobar; is mix_class("Foo", "Bar"), $foobar; 1; Class-Mix-0.006/t/mro_yes.t000444001750001750 355313137410104 15514 0ustar00zeframzefram000000000000use warnings; use strict; BEGIN { if("$]" < 5.009005) { require Test::More; Test::More::plan(skip_all => "MRO not available on this Perl"); } } use Test::More tests => 35; BEGIN { use_ok "Class::Mix", qw(mix_class); } { package Foo; } { package Bar; use mro "dfs"; } { package Baz; use mro "c3"; } # error cases foreach( {mro=>[]}, {mro=>undef}, {mro=>"wibble"}, ) { eval { mix_class($_) }; isnt $@, ""; eval { mix_class("Foo", $_) }; isnt $@, ""; eval { mix_class("Foo", "Bar", $_) }; isnt $@, ""; } # dfs cases no strict "refs"; my $dfs_u = mix_class({mro=>"dfs"}); is $dfs_u, "UNIVERSAL"; is mro::get_mro($dfs_u), "dfs"; my $dfs_foo = mix_class("Foo", {mro=>"dfs"}); is $dfs_foo, "Foo"; is mro::get_mro($dfs_foo), "dfs"; my $dfs_bar = mix_class("Bar", {mro=>"dfs"}); is $dfs_bar, "Bar"; is mro::get_mro($dfs_bar), "dfs"; my $dfs_baz = mix_class("Baz", {mro=>"dfs"}); is_deeply(\@{$dfs_baz."::ISA"}, ["Baz"]); is mro::get_mro($dfs_baz), "dfs"; my $dfs_foobar = mix_class("Foo", "Bar", {mro=>"dfs"}); is_deeply(\@{$dfs_foobar."::ISA"}, ["Foo", "Bar"]); is mro::get_mro($dfs_foobar), "dfs"; # default cases must be consistent with dfs is mix_class(), $dfs_u; is mix_class("Foo"), $dfs_foo; is mix_class("Bar"), $dfs_bar; is mix_class("Baz"), $dfs_baz; is mix_class("Foo", "Bar"), $dfs_foobar; # c3 cases my $c3_u = mix_class({mro=>"c3"}); is_deeply(\@{$c3_u."::ISA"}, []); is mro::get_mro($c3_u), "c3"; my $c3_foo = mix_class("Foo", {mro=>"c3"}); is_deeply(\@{$c3_foo."::ISA"}, ["Foo"]); is mro::get_mro($c3_foo), "c3"; my $c3_bar = mix_class("Bar", {mro=>"c3"}); is_deeply(\@{$c3_bar."::ISA"}, ["Bar"]); is mro::get_mro($c3_bar), "c3"; my $c3_baz = mix_class("Baz", {mro=>"c3"}); is $c3_baz, "Baz"; is mro::get_mro($c3_baz), "c3"; my $c3_foobar = mix_class("Foo", "Bar", {mro=>"c3"}); is_deeply(\@{$c3_foobar."::ISA"}, ["Foo", "Bar"]); is mro::get_mro($c3_foobar), "c3"; 1; Class-Mix-0.006/t/pod_cvg.t000444001750001750 27313137410104 15434 0ustar00zeframzefram000000000000use warnings; use strict; use Test::More; plan skip_all => "Test::Pod::Coverage not available" unless eval "use Test::Pod::Coverage; 1"; Test::Pod::Coverage::all_pod_coverage_ok(); 1; Class-Mix-0.006/t/pod_syn.t000444001750001750 23613137410104 15465 0ustar00zeframzefram000000000000use warnings; use strict; use Test::More; plan skip_all => "Test::Pod not available" unless eval "use Test::Pod 1.00; 1"; Test::Pod::all_pod_files_ok(); 1;