parent.pm 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. package parent;
  2. use strict;
  3. use vars qw($VERSION);
  4. $VERSION = '0.225';
  5. sub import {
  6. my $class = shift;
  7. my $inheritor = caller(0);
  8. if ( @_ and $_[0] eq '-norequire' ) {
  9. shift @_;
  10. } else {
  11. for ( my @filename = @_ ) {
  12. if ( $_ eq $inheritor ) {
  13. warn "Class '$inheritor' tried to inherit from itself\n";
  14. };
  15. s{::|'}{/}g;
  16. require "$_.pm"; # dies if the file is not found
  17. }
  18. }
  19. {
  20. no strict 'refs';
  21. push @{"$inheritor\::ISA"}, @_;
  22. };
  23. };
  24. "All your base are belong to us"
  25. __END__
  26. =encoding utf8
  27. =head1 NAME
  28. parent - Establish an ISA relationship with base classes at compile time
  29. =head1 SYNOPSIS
  30. package Baz;
  31. use parent qw(Foo Bar);
  32. =head1 DESCRIPTION
  33. Allows you to both load one or more modules, while setting up inheritance from
  34. those modules at the same time. Mostly similar in effect to
  35. package Baz;
  36. BEGIN {
  37. require Foo;
  38. require Bar;
  39. push @ISA, qw(Foo Bar);
  40. }
  41. By default, every base class needs to live in a file of its own.
  42. If you want to have a subclass and its parent class in the same file, you
  43. can tell C<parent> not to load any modules by using the C<-norequire> switch:
  44. package Foo;
  45. sub exclaim { "I CAN HAS PERL" }
  46. package DoesNotLoadFooBar;
  47. use parent -norequire, 'Foo', 'Bar';
  48. # will not go looking for Foo.pm or Bar.pm
  49. This is equivalent to the following code:
  50. package Foo;
  51. sub exclaim { "I CAN HAS PERL" }
  52. package DoesNotLoadFooBar;
  53. push @DoesNotLoadFooBar::ISA, 'Foo', 'Bar';
  54. This is also helpful for the case where a package lives within
  55. a differently named file:
  56. package MyHash;
  57. use Tie::Hash;
  58. use parent -norequire, 'Tie::StdHash';
  59. This is equivalent to the following code:
  60. package MyHash;
  61. require Tie::Hash;
  62. push @ISA, 'Tie::StdHash';
  63. If you want to load a subclass from a file that C<require> would
  64. not consider an eligible filename (that is, it does not end in
  65. either C<.pm> or C<.pmc>), use the following code:
  66. package MySecondPlugin;
  67. require './plugins/custom.plugin'; # contains Plugin::Custom
  68. use parent -norequire, 'Plugin::Custom';
  69. =head1 DIAGNOSTICS
  70. =over 4
  71. =item Class 'Foo' tried to inherit from itself
  72. Attempting to inherit from yourself generates a warning.
  73. package Foo;
  74. use parent 'Foo';
  75. =back
  76. =head1 HISTORY
  77. This module was forked from L<base> to remove the cruft
  78. that had accumulated in it.
  79. =head1 CAVEATS
  80. =head1 SEE ALSO
  81. L<base>
  82. =head1 AUTHORS AND CONTRIBUTORS
  83. Rafaël Garcia-Suarez, Bart Lateur, Max Maischein, Anno Siegel, Michael Schwern
  84. =head1 MAINTAINER
  85. Max Maischein C< corion@cpan.org >
  86. Copyright (c) 2007-10 Max Maischein C<< <corion@cpan.org> >>
  87. Based on the idea of C<base.pm>, which was introduced with Perl 5.004_04.
  88. =head1 LICENSE
  89. This module is released under the same terms as Perl itself.
  90. =cut