Import namespace-clean-0.01.tar.gz.
[p5sagit/namespace-clean.git] / lib / namespace / clean.pm
CommitLineData
40aef9d6 1package namespace::clean;
2
3=head1 NAME
4
5namespace::clean - Keep imports out of your namespace
6
7=cut
8
9use warnings;
10use strict;
11
12use vars qw( $VERSION );
13use Symbol qw( qualify_to_ref );
14use Filter::EOF;
15
16=head1 VERSION
17
180.01
19
20=cut
21
22$VERSION = 0.01;
23
24=head1 SYNOPSIS
25
26 package Foo;
27 use warnings;
28 use strict;
29
30 use Carp qw(croak); # will be removed
31
32 sub bar { 23 } # will be removed
33
34 use namespace::clean;
35
36 sub baz { bar() } # still defined, 'bar' still bound
37
38 ### Will print:
39 # No
40 # No
41 # Yes
42 print +(__PACKAGE__->can('croak') ? 'Yes' : 'No'), "\n";
43 print +(__PACKAGE__->can('bar') ? 'Yes' : 'No'), "\n";
44 print +(__PACKAGE__->can('baz') ? 'Yes' : 'No'), "\n";
45
46 1;
47
48=head1 DESCRIPTION
49
50When you define a function, or import one, into a Perl package, it will
51naturally also be available as a method. This does not per se cause
52problems, but it can complicate subclassing and, for example, plugin
53classes that are included by loading them as base classes.
54
55The C<namespace::clean> pragma will remove all previously declared or
56imported symbols at the end of the current package's compile cycle.
57This means that functions are already bound by their name, and calls to
58them still work. But they will not be available as methods on your class
59or instances.
60
61=head1 METHODS
62
63You shouldn't need to call any of these. Just C<use> the package at the
64appropriate place.
65
66=head2 import
67
68Makes a snapshot of the current defined functions and registers a
69L<Filter::EOF> cleanup routine to remove those symbols from the package
70at the end of the compile-time.
71
72=cut
73
74sub import {
75 my ($pragma) = @_;
76
77 my $cleanee = caller;
78 my $functions = $pragma->get_functions($cleanee);
79
80 Filter::EOF->on_eof_call(sub {
81 for my $f (keys %$functions) {
82 next unless $functions->{ $f }
83 and *{ $functions->{ $f } }{CODE};
84 { no strict 'refs';
85 delete ${ "${cleanee}::" }{ $f };
86 }
87 }
88 });
89}
90
91=head2 get_functions
92
93Takes a class as argument and returns all currently defined functions
94in it as a hash reference with the function name as key and a typeglob
95reference to the symbol as value.
96
97=cut
98
99sub get_functions {
100 my ($pragma, $class) = @_;
101
102 return {
103 map { @$_ }
104 grep { *{ $_->[1] }{CODE} }
105 map { [$_, qualify_to_ref( $_, $class )] }
106 grep { $_ !~ /::$/ }
107 do { no strict 'refs'; keys %{ "${class}::" } }
108 };
109}
110
111=head1 SEE ALSO
112
113L<Filter::EOF>
114
115=head1 AUTHOR AND COPYRIGHT
116
117Robert 'phaylon' Sedlacek C<E<lt>rs@474.atE<gt>>, with many thanks to
118Matt S Trout for the inspiration on the whole idea.
119
120=head1 LICENSE
121
122This program is free software; you can redistribute it and/or modify
123it under the same terms as perl itself.
124
125=cut
126
1271;