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