| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package foundation; | 
| 2 |  |  |  |  |  |  |  | 
| 3 | 1 |  |  | 1 |  | 707 | use strict; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 25 |  | 
| 4 | 1 |  |  | 1 |  | 4 | no strict 'refs'; | 
|  | 1 |  |  |  |  | 1 |  | 
|  | 1 |  |  |  |  | 23 |  | 
| 5 | 1 |  |  | 1 |  | 5 | use vars qw($VERSION @ISA @EXPORT); | 
|  | 1 |  |  |  |  | 3 |  | 
|  | 1 |  |  |  |  | 414 |  | 
| 6 |  |  |  |  |  |  | $VERSION = '0.03'; | 
| 7 |  |  |  |  |  |  |  | 
| 8 |  |  |  |  |  |  | require Exporter; | 
| 9 |  |  |  |  |  |  | @ISA = qw(Exporter); | 
| 10 |  |  |  |  |  |  | @EXPORT = qw(SUPER foundation); | 
| 11 |  |  |  |  |  |  |  | 
| 12 |  |  |  |  |  |  |  | 
| 13 |  |  |  |  |  |  | =pod | 
| 14 |  |  |  |  |  |  |  | 
| 15 |  |  |  |  |  |  | =head1 NAME | 
| 16 |  |  |  |  |  |  |  | 
| 17 |  |  |  |  |  |  | foundation - Inheritance without objects | 
| 18 |  |  |  |  |  |  |  | 
| 19 |  |  |  |  |  |  |  | 
| 20 |  |  |  |  |  |  | =head1 SYNOPSIS | 
| 21 |  |  |  |  |  |  |  | 
| 22 |  |  |  |  |  |  | package Foo; | 
| 23 |  |  |  |  |  |  |  | 
| 24 |  |  |  |  |  |  | sub fooble { 42 } | 
| 25 |  |  |  |  |  |  |  | 
| 26 |  |  |  |  |  |  | package Bar; | 
| 27 |  |  |  |  |  |  |  | 
| 28 |  |  |  |  |  |  | sub mooble { 23 } | 
| 29 |  |  |  |  |  |  | sub hooble { 13 } | 
| 30 |  |  |  |  |  |  |  | 
| 31 |  |  |  |  |  |  | package FooBar; | 
| 32 |  |  |  |  |  |  | use foundation; | 
| 33 |  |  |  |  |  |  | foundation(qw(Foo Bar)); | 
| 34 |  |  |  |  |  |  |  | 
| 35 |  |  |  |  |  |  | sub hooble { 31 } | 
| 36 |  |  |  |  |  |  |  | 
| 37 |  |  |  |  |  |  | print fooble();       # prints 42 | 
| 38 |  |  |  |  |  |  | print moodle();       # prints 23 | 
| 39 |  |  |  |  |  |  | print hooble();       # prints 31 (FooBar overrides hooble() from Bar) | 
| 40 |  |  |  |  |  |  | print SUPER('hooble');     # prints 13 (Bar's hooble()) | 
| 41 |  |  |  |  |  |  |  | 
| 42 |  |  |  |  |  |  |  | 
| 43 |  |  |  |  |  |  | =head1 DESCRIPTION | 
| 44 |  |  |  |  |  |  |  | 
| 45 |  |  |  |  |  |  | Haven't drunk the OO Kool-Aid yet?  Think object-oriented has | 
| 46 |  |  |  |  |  |  | something to do with Ayn Rand?  Do you eat Java programmers for | 
| 47 |  |  |  |  |  |  | breakfast? | 
| 48 |  |  |  |  |  |  |  | 
| 49 |  |  |  |  |  |  | If the answer to any of those is yes, than this is the module for you! | 
| 50 |  |  |  |  |  |  | C adds the power of inheritance without getting into a | 
| 51 |  |  |  |  |  |  | class-war! | 
| 52 |  |  |  |  |  |  |  | 
| 53 |  |  |  |  |  |  | Simply C | 
| 54 |  |  |  |  |  |  | "inherit".  It then sucks in all the symbols from those libraries into | 
| 55 |  |  |  |  |  |  | the current one. | 
| 56 |  |  |  |  |  |  |  | 
| 57 |  |  |  |  |  |  | =head2 Functions | 
| 58 |  |  |  |  |  |  |  | 
| 59 |  |  |  |  |  |  | =over 4 | 
| 60 |  |  |  |  |  |  |  | 
| 61 |  |  |  |  |  |  | =item B | 
| 62 |  |  |  |  |  |  |  | 
| 63 |  |  |  |  |  |  | foundation(@libraries); | 
| 64 |  |  |  |  |  |  |  | 
| 65 |  |  |  |  |  |  | Declares what libraries you are founded on.  Similar to C | 
| 66 |  |  |  |  |  |  |  | 
| 67 |  |  |  |  |  |  | =cut | 
| 68 |  |  |  |  |  |  |  | 
| 69 |  |  |  |  |  |  | #'# | 
| 70 |  |  |  |  |  |  | sub foundation { | 
| 71 | 2 |  |  | 2 | 1 | 102 | my(@libraries) = @_; | 
| 72 | 2 |  |  |  |  | 5 | my $caller = caller; | 
| 73 |  |  |  |  |  |  |  | 
| 74 | 2 |  |  |  |  | 5 | foreach my $library (@libraries) { | 
| 75 |  |  |  |  |  |  | #        next if FOUNDED_ON($library, $caller); | 
| 76 | 3 |  |  |  |  | 4 | push @{$caller.'::__FOUNDATION'}, $library; | 
|  | 3 |  |  |  |  | 18 |  | 
| 77 |  |  |  |  |  |  |  | 
| 78 | 3 |  |  |  |  | 230 | eval "require $library"; | 
| 79 |  |  |  |  |  |  | # only ignore "Can't locate" errors. | 
| 80 | 3 | 50 | 33 |  |  | 49 | die if $@ && $@ !~ /^Can't locate .*? at \(eval /; #' | 
| 81 |  |  |  |  |  |  |  | 
| 82 | 3 |  |  |  |  | 5 | while( my($name, $stuff) = each %{$library.'::'} ) { | 
|  | 16 |  |  |  |  | 78 |  | 
| 83 | 13 |  |  |  |  | 14 | my $call_glob = ${$caller.'::'}{$name}; | 
|  | 13 |  |  |  |  | 35 |  | 
| 84 |  |  |  |  |  |  |  | 
| 85 | 10 |  |  |  |  | 21 | *{$caller.'::'.$name} = \&$stuff | 
|  | 13 |  |  |  |  | 53 |  | 
| 86 | 13 | 100 |  |  |  | 14 | unless defined &{$caller.'::'.$name}; | 
| 87 | 13 |  |  |  |  | 20 | *{$caller.'::'.$name} = \$$stuff; | 
|  | 13 |  |  |  |  | 25 |  | 
| 88 | 13 |  |  |  |  | 23 | *{$caller.'::'.$name} = \@$stuff; | 
|  | 13 |  |  |  |  | 22 |  | 
| 89 | 13 |  |  |  |  | 25 | *{$caller.'::'.$name} = \%$stuff; | 
|  | 13 |  |  |  |  | 42 |  | 
| 90 |  |  |  |  |  |  | } | 
| 91 |  |  |  |  |  |  | } | 
| 92 |  |  |  |  |  |  |  | 
| 93 | 2 |  |  |  |  | 4 | *{$caller.'::SUPER'} = \&SUPER; | 
|  | 2 |  |  |  |  | 10 |  | 
| 94 |  |  |  |  |  |  | } | 
| 95 |  |  |  |  |  |  |  | 
| 96 |  |  |  |  |  |  | =pod | 
| 97 |  |  |  |  |  |  |  | 
| 98 |  |  |  |  |  |  | =item B | 
| 99 |  |  |  |  |  |  |  | 
| 100 |  |  |  |  |  |  | my @results = SUPER($function, @args); | 
| 101 |  |  |  |  |  |  |  | 
| 102 |  |  |  |  |  |  | Calls the named $function of the current package's foundation with the | 
| 103 |  |  |  |  |  |  | given @args. | 
| 104 |  |  |  |  |  |  |  | 
| 105 |  |  |  |  |  |  | Similar to C<$obj->SUPER::meth();> | 
| 106 |  |  |  |  |  |  |  | 
| 107 |  |  |  |  |  |  | =cut | 
| 108 |  |  |  |  |  |  |  | 
| 109 |  |  |  |  |  |  | sub SUPER { | 
| 110 | 1 |  |  | 1 | 1 | 45 | my($func) = shift; | 
| 111 | 1 |  |  |  |  | 5 | my($lib) = caller; | 
| 112 |  |  |  |  |  |  |  | 
| 113 | 1 |  |  |  |  | 2 | my $super_func; | 
| 114 |  |  |  |  |  |  |  | 
| 115 |  |  |  |  |  |  | # Fortunately, we can do a linear search. | 
| 116 | 1 |  |  |  |  | 2 | foreach my $foundation (@{$lib.'::__FOUNDATION'}) { | 
|  | 1 |  |  |  |  | 5 |  | 
| 117 | 2 | 100 |  |  |  | 2 | if( defined &{$foundation.'::'.$func} ) { | 
|  | 2 |  |  |  |  | 12 |  | 
| 118 | 1 |  |  |  |  | 1 | $super_func = \&{$foundation.'::'.$func}; | 
|  | 1 |  |  |  |  | 5 |  | 
| 119 | 1 |  |  |  |  | 2 | last; | 
| 120 |  |  |  |  |  |  | } | 
| 121 |  |  |  |  |  |  | } | 
| 122 |  |  |  |  |  |  |  | 
| 123 | 1 |  |  |  |  | 5 | goto &$super_func; | 
| 124 |  |  |  |  |  |  | } | 
| 125 |  |  |  |  |  |  |  | 
| 126 |  |  |  |  |  |  |  | 
| 127 |  |  |  |  |  |  | =pod | 
| 128 |  |  |  |  |  |  |  | 
| 129 |  |  |  |  |  |  | =head1 BUGS | 
| 130 |  |  |  |  |  |  |  | 
| 131 |  |  |  |  |  |  | Plenty, I'm sure.  This is a quick proof-of-concept knock off. | 
| 132 |  |  |  |  |  |  |  | 
| 133 |  |  |  |  |  |  | =head1 AUTHOR | 
| 134 |  |  |  |  |  |  |  | 
| 135 |  |  |  |  |  |  | Michael G Schwern | 
| 136 |  |  |  |  |  |  |  | 
| 137 |  |  |  |  |  |  | =head1 SEE ALSO | 
| 138 |  |  |  |  |  |  |  | 
| 139 |  |  |  |  |  |  | L, L | 
| 140 |  |  |  |  |  |  |  | 
| 141 |  |  |  |  |  |  | =cut | 
| 142 |  |  |  |  |  |  |  | 
| 143 |  |  |  |  |  |  | 1; |