File Coverage

blib/lib/Class/BlackHole.pm
Criterion Covered Total %
statement 11 12 91.6
branch n/a
condition n/a
subroutine 5 6 83.3
pod 0 1 0.0
total 16 19 84.2


line stmt bran cond sub pod time code
1              
2             # Time-stamp: "2004-12-29 20:04:51 AST"
3             require 5;
4             package Class::BlackHole;
5 2     2   6670 use strict;
  2         4  
  2         61  
6 2     2   8 use vars qw(@ISA $Debug $VERSION);
  2         3  
  2         293  
7              
8             $VERSION = "0.04";
9             @ISA = ();
10             $Debug = 0 unless defined $Debug;
11              
12             =head1 NAME
13              
14             Class::BlackHole - base class to treat unhandled method calls as no-ops
15              
16             =head1 SYNOPSIS
17              
18             use Class::BlackHole;
19              
20             # set up a class X, to inherit from Class::BlackHole
21             @X::ISA = qw(Class::BlackHole);
22             # put a method in it
23             sub X::zaz { 123123; }
24            
25             print "Zaz is <", X->zaz, ">\n";
26             print "Flork is <", X->flork, ">\n";
27             print "can zaz : <", X->can('zaz'), ">\n";
28             print "can flork : <", X->can('flork'), ">\n";
29            
30            
31             The above code prints:
32             Zaz is <123123>
33             Flork is <>
34             can zaz :
35             can flork : <>
36              
37             =head1 DESCRIPTION
38              
39             Normally, if you try to call a method that there's no handler for,
40             you get an error:
41              
42             Can't locate object method "flork" via package "X".
43              
44             But for classes that inherit from Class::BlackHole, unhandled methods
45             become just no-operations.
46              
47             =head1 CAVEATS
48              
49             Be sure to have Class::BlackHole be the absolute last item in your
50             class's ISA list.
51              
52             This class will almost definitely not work right as part of any ISA
53             tree that has multiple inheritance.
54              
55             =head1 IMPLEMENTATION
56              
57             Class::BlackHole just traps everything with an AUTOLOAD sub that is a
58             no-operation.
59              
60             HOWEVER, what makes Class::Blackhole different than merely:
61              
62             @Class::BlackHole::ISA = ();
63             sub Class::BlackHole::AUTOLOAD { }
64              
65             is that this would (unhappily) trap calls to the helpful methods in
66             UNIVERSAL, like C, C, and C. Class::BlackHole
67             aliases those methods (or better said, all subs in package UNIVERSAL)
68             into its own package, so that they'll be accessible instead of being
69             caught by the AUTOLOAD.
70              
71             =head1 FUNCTIONS AND METHODS
72              
73             This module provides no functions or methods.
74              
75             It exports no symbols into the calling package or anywhere else.
76              
77             =head1 DISCLAIMER
78              
79             This program is distributed in the hope that it will be useful,
80             but B; without even the implied warranty of
81             B or B.
82              
83             But let me know if it gives you any problems, OK?
84              
85             =head1 COPYRIGHT
86              
87             Copyright 1999, 2000, Sean M. Burke C, all rights
88             reserved. This program is free software; you can redistribute it
89             and/or modify it under the same terms as Perl itself.
90              
91             =head1 AUTHOR
92              
93             Sean M. Burke C
94              
95             =cut
96              
97             ###########################################################################
98 1     1   559 sub AUTOLOAD { return; } # no-op -- the famed black hole!
99 1     1   1650 sub import { return; } # no-op
100 0     0 0   sub export { return; } # no-op
101              
102             ###########################################################################
103             # Now copy (well, alias) Universal's subs up into this class
104              
105             foreach my $symbol (keys %UNIVERSAL::) {
106 2     2   8 no strict 'refs';
  2         10  
  2         228  
107             if(defined &{"UNIVERSAL::$symbol"}) {
108             print "aliasing my $symbol to sub UNIVERSAL::$symbol\n" if $Debug;
109             *{$symbol} = \&{"UNIVERSAL::$symbol"} unless defined &{$symbol};
110             } else {
111             print "there's no sub UNIVERSAL::$symbol\n" if $Debug;
112             }
113             }
114              
115             ###########################################################################
116             1;
117              
118             __END__