Commit | Line | Data |
40aef9d6 |
1 | NAME |
6c0ece9b |
2 | namespace::clean - Keep imports and functions out of your namespace |
40aef9d6 |
3 | |
4 | VERSION |
6c0ece9b |
5 | 0.03 |
40aef9d6 |
6 | |
7 | SYNOPSIS |
8 | package Foo; |
9 | use warnings; |
10 | use strict; |
11 | |
6c0ece9b |
12 | use Carp qw(croak); # 'croak' will be removed |
40aef9d6 |
13 | |
6c0ece9b |
14 | sub bar { 23 } # 'bar' will be removed |
40aef9d6 |
15 | |
6c0ece9b |
16 | # remove all previously defined functions |
40aef9d6 |
17 | use namespace::clean; |
18 | |
6c0ece9b |
19 | sub baz { bar() } # 'baz' still defined, 'bar' still bound |
40aef9d6 |
20 | |
6c0ece9b |
21 | # begin to collection function names from here again |
9b680ffe |
22 | no namespace::clean; |
23 | |
6c0ece9b |
24 | sub quux { baz() } # 'quux' will be removed |
9b680ffe |
25 | |
6c0ece9b |
26 | # remove all functions defined after the 'no' unimport |
9b680ffe |
27 | use namespace::clean; |
28 | |
6c0ece9b |
29 | # Will print: 'No', 'No', 'Yes' and 'No' |
40aef9d6 |
30 | print +(__PACKAGE__->can('croak') ? 'Yes' : 'No'), "\n"; |
31 | print +(__PACKAGE__->can('bar') ? 'Yes' : 'No'), "\n"; |
32 | print +(__PACKAGE__->can('baz') ? 'Yes' : 'No'), "\n"; |
9b680ffe |
33 | print +(__PACKAGE__->can('quux') ? 'Yes' : 'No'), "\n"; |
40aef9d6 |
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 |
6c0ece9b |
41 | classes that are included via multiple inheritance by loading them as |
42 | base classes. |
40aef9d6 |
43 | |
44 | The "namespace::clean" pragma will remove all previously declared or |
6c0ece9b |
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. |
40aef9d6 |
51 | |
52 | METHODS |
53 | You shouldn't need to call any of these. Just "use" the package at the |
54 | appropriate place. |
55 | |
56 | import |
57 | Makes a snapshot of the current defined functions and registers a |
6c0ece9b |
58 | Filter::EOF cleanup routine to remove those symbols at the end of the |
59 | compile-time. |
40aef9d6 |
60 | |
9b680ffe |
61 | unimport |
62 | This method will be called when you do a |
63 | |
64 | no namespace::clean; |
65 | |
66 | It will start a new section of code that defines functions to clean up. |
67 | |
68 | get_class_store |
6c0ece9b |
69 | This returns a reference to a hash in a passed package containing |
9b680ffe |
70 | information about function names included and excluded from removal. |
71 | |
40aef9d6 |
72 | get_functions |
73 | Takes a class as argument and returns all currently defined functions in |
74 | it as a hash reference with the function name as key and a typeglob |
75 | reference to the symbol as value. |
76 | |
6c0ece9b |
77 | IMPLEMENTATION DETAILS |
78 | This module works through the effect that a |
79 | |
80 | delete $SomePackage::{foo}; |
81 | |
82 | will remove the "foo" symbol from $SomePackage for run time lookups |
83 | (e.g., method calls) but will leave the entry alive to be called by |
84 | already resolved names in the package itself. |
85 | |
86 | A test file has been added to the perl core to ensure that this |
87 | behaviour will be stable in future releases. |
88 | |
89 | Just for completeness sake, if you want to remove the symbol completely, |
90 | use "undef" instead. |
91 | |
40aef9d6 |
92 | SEE ALSO |
93 | Filter::EOF |
94 | |
95 | AUTHOR AND COPYRIGHT |
96 | Robert 'phaylon' Sedlacek "<rs@474.at>", with many thanks to Matt S |
97 | Trout for the inspiration on the whole idea. |
98 | |
99 | LICENSE |
100 | This program is free software; you can redistribute it and/or modify it |
101 | under the same terms as perl itself. |
102 | |