File Coverage

blib/lib/thanks.pm
Criterion Covered Total %
statement 10 10 100.0
branch 4 6 66.6
condition n/a
subroutine 3 3 100.0
pod n/a
total 17 19 89.4


line stmt bran cond sub pod time code
1             package thanks;
2              
3             $thanks::USELESS_VARIABLE = "
4             use 5.005;
5             " unless $thanks::USELESS_VARIABLE;
6              
7             BEGIN {
8 4 50   4   72156 $thanks::AUTHORITY = 'cpan:TOBYINK' unless $thanks::AUTHORITY;
9 4 50       786 $thanks::VERSION = '0.005' unless $thanks::VERSION;
10             }
11              
12             sub _module_notional_filename
13             {
14 3     3   18 (my $name = shift) =~ s!(::|')!/!g;
15 3         1887 return $name . q[.pm];
16             }
17              
18             sub unimport
19             {
20 3     3   19 my $class = shift;
21 3         23 my @caller = caller(0);
22 3 100       14 @_ = $caller[0] unless @_;
23 3         7 my $file = $caller[1];
24 3         8 for (@_)
25 3         10 { $INC{ _module_notional_filename($_) } = $file }
26             }
27              
28             1;
29              
30             __END__
31              
32             =head1 NAME
33              
34             thanks - inline packages easily
35              
36             =head1 SYNOPSIS
37              
38             BEGIN {
39             package My::Package;
40             no thanks;
41             # everything else goes here
42             };
43            
44             use My::Package; # No error message about missing
45             # file "My/Package.pm".
46             #
47              
48             Or:
49              
50             BEGIN {
51             package My::Package;
52             # everything else goes here
53             };
54            
55             no thanks qw( My::Package Another::Package Yet::Another::Package );
56            
57             use My::Package; # No error message about missing
58             # file "My/Package.pm".
59             #
60              
61             =head1 ELEVATOR PITCH
62              
63             Defining multiple Perl packages in the same file can be fraught with
64             difficulty. C<< no thanks >> makes it a bit easier. To define a package
65             just do...
66              
67             BEGIN {
68             package My::Package;
69             no thanks;
70             # everything else goes here
71             };
72              
73             Then everything should more or less work exactly if My::Package had
74             been a package defined in an external file and loaded like:
75              
76             use My::Package ();
77              
78             (The exception being that the inlined My::Package can see file-scoped
79             lexicals.)
80              
81             Why define multiple packages in the same file? Because often namespacing
82             concerns and code organisation concerns don't align. For example, you
83             have many small packages which it is important don't share namespaces,
84             but you want to be able to edit them all in the same window/tab of your
85             editor.
86              
87             =head1 DESCRIPTION
88              
89             This module asks Perl politely not to load a module you don't want loading.
90             It's just a polite request; we're not forcing Perl to do anything it doesn't
91             want to. And if the module is already loaded, then we won't try to unload
92             it or anything like that.
93              
94             Specifically, Perl's C<< use Module::Name >> syntax does two things. It reads,
95             compiles and executes C<< Module/Name.pm >>, and calls the class method
96             C<< Module::Name->import >>. This module is designed to prevent the first
97             thing happening, not the second thing.
98              
99             How does it work? Perl keeps a record of what modules have already been
100             loaded in the C<< %INC >> global hash, to avoid reloading them. This module
101             just adds an entry to that hash to trick Perl into thinking that a module
102             has already been loaded.
103              
104             C<thanks> is a deliberately light-weight module. It has no dependencies (not
105             even L<strict> or L<warnings>) and is believed to work in any release of Perl
106             5. (The installation and testing scripts have more dependencies, but if push
107             comes to shove, you can manually copy thanks.pm to an appropriate location.)
108              
109             =head2 Methods
110              
111             =over
112              
113             =item C<< unimport >>
114              
115             no thanks @LIST;
116              
117             If C<< @LIST >> is empty, then the caller package is assumed.
118              
119             =back
120              
121             =head2 Use Case 1: Multiple Packages in the Same File
122              
123             Perl's C<< use >> keyword muddies the distinction between packages (which
124             are just namespaces) and modules (which are just files). Sometimes you wish
125             to define two packages (say C<< My::Package >> and
126             C<< My::Package::Helper >>) in the same file (say C<< My/Package.pm >>). If
127             anybody tries to load C<< My::Package::Helper >> with C<use>, then they'll
128             get an error message. If C<< My/Package.pm >> includes:
129              
130             no thanks 'My::Package::Helper';
131              
132             then this will prevent C<< use My::Package::Helper >> from throwing an error
133             message, provided C<< My/Package.pm >> is already loaded.
134              
135             =head2 Use Case 2: You Really Want to Prevent a Module from Loading
136              
137             It's quite a messy thing to do, but if you really need to silently prevent
138             a module from being loaded, then C<< no thanks >> will do the trick. Just
139             make sure you do it early.
140              
141             This is almost always a bad idea.
142              
143             =head1 BUGS
144              
145             Please report any bugs to
146             L<http://rt.cpan.org/Dist/Display.html?Queue=thanks>.
147              
148             =head1 SEE ALSO
149              
150             L<again>,
151             L<Module::Reload>,
152             L<Class::Unload>.
153              
154             =head1 AUTHOR
155              
156             Toby Inkster E<lt>tobyink@cpan.orgE<gt>.
157              
158             =head1 COPYRIGHT AND LICENCE
159              
160             This software is copyright (c) 2012-2013 by Toby Inkster.
161              
162             This is free software; you can redistribute it and/or modify it under
163             the same terms as the Perl 5 programming language system itself.
164              
165             =head1 DISCLAIMER OF WARRANTIES
166              
167             THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
168             WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
169             MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
170