Import namespace-clean-0.07.tar.gz.
[p5sagit/namespace-clean.git] / README
1 NAME
2     namespace::clean - Keep imports and functions out of your namespace
3
4 VERSION
5     0.07
6
7 SYNOPSIS
8       package Foo;
9       use warnings;
10       use strict;
11
12       use Carp qw(croak);   # 'croak' will be removed
13
14       sub bar { 23 }        # 'bar' will be removed
15
16       # remove all previously defined functions
17       use namespace::clean;
18
19       sub baz { bar() }     # 'baz' still defined, 'bar' still bound
20
21       # begin to collection function names from here again
22       no namespace::clean;
23
24       sub quux { baz() }    # 'quux' will be removed
25
26       # remove all functions defined after the 'no' unimport
27       use namespace::clean;
28
29       # Will print: 'No', 'No', 'Yes' and 'No'
30       print +(__PACKAGE__->can('croak') ? 'Yes' : 'No'), "\n";
31       print +(__PACKAGE__->can('bar')   ? 'Yes' : 'No'), "\n";
32       print +(__PACKAGE__->can('baz')   ? 'Yes' : 'No'), "\n";
33       print +(__PACKAGE__->can('quux')  ? 'Yes' : 'No'), "\n";
34
35       1;
36
37 DESCRIPTION
38     When you define a function, or import one, into a Perl package, it will
39     naturally also be available as a method. This does not per se cause
40     problems, but it can complicate subclassing and, for example, plugin
41     classes that are included via multiple inheritance by loading them as
42     base classes.
43
44     The "namespace::clean" pragma will remove all previously declared or
45     imported symbols at the end of the current package's compile cycle.
46     Functions called in the package itself will still be bound by their
47     name, but they won't show up as methods on your class or instances.
48
49     By unimporting via "no" you can tell "namespace::clean" to start
50     collecting functions for the next "use namespace::clean;" specification.
51
52     You can use the "-except" flag to tell "namespace::clean" that you don't
53     want it to remove a certain function or method. A common use would be a
54     module exporting an "import" method along with some functions:
55
56       use ModuleExportingImport;
57       use namespace::clean -except => [qw( import )];
58
59     If you just want to "-except" a single sub, you can pass it directly.
60     For more than one value you have to use an array reference.
61
62   Moose
63     When using "namespace::clean" together with Moose you want to keep the
64     installed "meta" method. So your classes should look like:
65
66       package Foo;
67       use Moose;
68       use namespace::clean -except => 'meta';
69       ...
70
71     Same goes for Moose::Role.
72
73 METHODS
74     You shouldn't need to call any of these. Just "use" the package at the
75     appropriate place.
76
77   import
78     Makes a snapshot of the current defined functions and installs a
79     Scope::Guard in the current scope to invoke the cleanups.
80
81   unimport
82     This method will be called when you do a
83
84       no namespace::clean;
85
86     It will start a new section of code that defines functions to clean up.
87
88   get_class_store
89     This returns a reference to a hash in a passed package containing
90     information about function names included and excluded from removal.
91
92   get_functions
93     Takes a class as argument and returns all currently defined functions in
94     it as a hash reference with the function name as key and a typeglob
95     reference to the symbol as value.
96
97 IMPLEMENTATION DETAILS
98     This module works through the effect that a
99
100       delete $SomePackage::{foo};
101
102     will remove the "foo" symbol from $SomePackage for run time lookups
103     (e.g., method calls) but will leave the entry alive to be called by
104     already resolved names in the package itself. "namespace::clean" will
105     restore and therefor in effect keep all glob slots that aren't "CODE".
106
107     A test file has been added to the perl core to ensure that this
108     behaviour will be stable in future releases.
109
110     Just for completeness sake, if you want to remove the symbol completely,
111     use "undef" instead.
112
113 SEE ALSO
114     Scope::Guard
115
116 AUTHOR AND COPYRIGHT
117     Robert 'phaylon' Sedlacek "<rs@474.at>", with many thanks to Matt S
118     Trout for the inspiration on the whole idea.
119
120 LICENSE
121     This program is free software; you can redistribute it and/or modify it
122     under the same terms as perl itself.
123