Permalink
Browse files

Initial revision, ready for release 0.01

  • Loading branch information...
0 parents commit 70e51da0f34a071bbb2c516a3f2112f4438ea8e1 @dolmen dolmen committed Apr 12, 2012
Showing with 168 additions and 0 deletions.
  1. +2 −0 .gitignore
  2. +4 −0 Changes
  3. +25 −0 dist.ini
  4. +76 −0 lib/Exporter/WithBase.pm
  5. +15 −0 t/10-import-in-Mother.t
  6. +18 −0 t/20-import-in-Child.t
  7. +16 −0 t/lib/Child1.pm
  8. +12 −0 t/lib/Mother.pm
@@ -0,0 +1,2 @@
+.build
+Exporter-WithBase-*
@@ -0,0 +1,4 @@
+Revision history for Exporter-WithBase.
+
+0.01
+ Initial release.
@@ -0,0 +1,25 @@
+name = Exporter-WithBase
+author = Olivier Mengue <dolmen@cpan.org>
+license = Perl_5
+copyright_holder = Olivier Mengue
+copyright_year = 2012
+version = 0.01
+
+[@Basic]
+
+[PkgVersion]
+;[NextRelease]
+
+[GithubMeta]
+remote = github
+
+[MetaJSON]
+
+[MinimumPerl]
+
+[AutoPrereqs]
+
+[Test::Compile]
+[Test::Kwalitee]
+[PodSyntaxTests]
+[PodCoverageTests]
@@ -0,0 +1,76 @@
+use strict qw<vars subs>; # no refs
+use warnings;
+
+package Exporter::WithBase;
+# ABSTRACT: Like Exporter, but add a '-base' flag to declare a class as a child
+
+use Exporter 5.57 ();
+
+sub import
+{
+ my $caller = caller;
+ # If -base, inherit from Exporter
+ if (@_ >= 2 && $_[1] eq '-base') {
+ splice @_, 1, 1;
+ push @{"${caller}::ISA"}, $_[0] eq __PACKAGE__ ? 'Exporter' : $_[0];
+ }
+ # Inject _import as import
+ *{"${caller}::import"} = \&_import;
+ # No symbols to export
+}
+
+sub _import
+{
+ if (@_ >= 2 && $_[1] eq '-base') {
+ splice @_, 1, 1;
+ my $caller = caller;
+ push @{"${caller}::ISA"}, $_[0];
+ }
+ goto &Exporter::import;
+}
+
+1;
+__END__
+
+=head1 NAME
+
+Exporter::WithBase - Like Exporter, but add '-base' to declare a child class
+
+=head1 SYNOPSIS
+
+ # file Mother.pm
+ package Mother;
+ use Exporter::WithBase;
+ our @EXPORT = qw<ONE>;
+ use constant ONE => 1;
+ ...
+
+
+ # file Child.pm
+ package Child;
+ # instead of: use parent 'Mother'
+ use Mother -base;
+ print ONE, "\n";
+
+=head1 DESCRIPTION
+
+Does the same things as L<Exporter>, but also supports a C<-base> flag. That
+flag can be used in the C<use> statement of a class to push the class into
+C<@ISA>.
+
+=head1 SEE ALSO
+
+L<import::Base>, L<parent>, L<Mojo:Base>.
+
+=head1 AUTHOR
+
+Olivier MenguE<eacute>, L<mailto:dolmen@cpan.org>.
+
+=head1 COPYRIGHT & LICENSE
+
+Copyright E<copy> 2012 Olivier MenguE<eacute>.
+
+This is free software; you can redistribute it and/or modify it under
+the same terms as the Perl 5 programming language system itself.
+
+=cut
@@ -0,0 +1,15 @@
+use strict;
+use warnings;
+
+use Test::More tests => 4;
+
+use t::lib::Mother 'hello';
+
+ok(t::lib::Mother->can('import'));
+ok(t::lib::Mother->can('hello'));
+
+# Check that the import worked
+ok(defined &main::hello, 'hello imported');
+is(hello(), 'Hello!');
+
+done_testing;
@@ -0,0 +1,18 @@
+use strict;
+use warnings;
+
+use Test::More tests => 7;
+
+use t::lib::Child1 'hi';
+
+ok(t::lib::Child1->isa(t::lib::Child1::));
+ok(t::lib::Child1->isa(t::lib::Mother::));
+ok(t::lib::Child1->can('import'));
+ok(t::lib::Child1->can('hello'));
+ok(t::lib::Child1->can('hi'));
+
+# Check that the import worked
+ok(defined &main::hi, 'hi imported');
+is(hi, 'Hi!');
+
+done_testing;
@@ -0,0 +1,16 @@
+use strict;
+use warnings;
+
+package t::lib::Child1;
+
+use t::lib::Mother -base;
+
+our @EXPORT_OK = 'hi';
+
+sub hi
+{
+ 'Hi!'
+}
+
+
+1;
@@ -0,0 +1,12 @@
+package t::lib::Mother;
+
+use Exporter::WithBase;
+
+our @EXPORT_OK = 'hello';
+
+sub hello
+{
+ 'Hello!'
+}
+
+1;

0 comments on commit 70e51da

Please sign in to comment.