File Coverage

blib/lib/Sub/Clean.pm
Criterion Covered Total %
statement 23 23 100.0
branch n/a
condition n/a
subroutine 9 9 100.0
pod n/a
total 32 32 100.0


line stmt bran cond sub pod time code
1             package Sub::Clean;
2              
3             # attribute handing requires 5.008
4 1     1   40839 use 5.008;
  1         148  
  1         46  
5              
6 1     1   7 use strict;
  1         1  
  1         27  
7 1     1   5 use warnings;
  1         2  
  1         49  
8              
9             our $VERSION = 1.00;
10              
11             # load all the magic
12 1     1   3068 use Sub::Identify qw(sub_name);
  1         1278  
  1         81  
13             require Attribute::Lexical;
14 1     1   1010 use B::Hooks::EndOfScope;
  1         23981  
  1         7  
15 1     1   1818 use namespace::clean;
  1         5831  
  1         6  
16              
17             sub _cleaner {
18 1     1   2940 my ($uboat, undef, undef, $state) = @_;
19             on_scope_end {
20 1     1   249 namespace::clean->clean_subroutines($state->[0],sub_name($uboat));
21             }
22 1         15 };
23              
24             sub import {
25 1     1   15 Attribute::Lexical->import("CODE:Cleaned" => \&_cleaner);
26 1         106 return;
27             }
28              
29             1;
30              
31             __END__
32              
33             =head1 NAME
34              
35             Sub::Clean - Clean subroutines with an attribute
36              
37             =head1 VERSION
38              
39             version 0.01
40              
41             =head1 SYNOPSIS
42              
43             use Sub::Clean;
44            
45             sub whatever :Cleaned {
46             return "This cannot be called as a method";
47             }
48              
49             =head1 DESCRIPTION
50              
51             This module 'cleans' subroutines you mark with the C<Cleaned> attribute,
52             preventing them being called from places they haven't been already been
53             bound by the perl compiler.
54              
55             The main advantage in this is that you can declare subroutines and use them
56             in your code as functions, but prevent them from being called as a method.
57             This is particularly useful if your superclass (maybe unbeknownst to you)
58             has a method of the same name as the function which would normally be
59             'shadowed' by your method.
60              
61             For example, this:
62              
63             package Banner;
64            
65             sub text { "example text\n" }
66             sub bar { "-"x20 . "\n" }
67            
68             sub message {
69             my $class = shift;
70             return $class->bar . $class->text . $class->bar
71             }
72            
73             package PubBanner;
74             use base qw(Banner);
75             use Sub::Clean;
76            
77             sub text { bar() . "\n" }
78            
79             sub bar :Cleaned {
80             return (("Cheers","Quark's","Moe's")[rand(3)])
81             }
82            
83             package main;
84             print PubBanner->message();
85              
86             Print out the right thing:
87              
88             --------------------
89             Moe's
90             --------------------
91              
92             Rather than just printing three horizontal lines.
93              
94             =head2 Lexical Scope
95              
96             Please note that this module enables the C<Cleaned> attribute only in lexical
97             scope of the C<use Sub::Clean> statement. This prevents namespace pollution.
98              
99             =head1 AUTHOR
100              
101             Written by Mark Fowler E<lt>mark@twoshortplanks.comE<gt>
102              
103             Copryright Mark Fowler 2010. All Rights Reserved.
104              
105             This program is free software; you can redistribute it
106             and/or modify it under the same terms as Perl itself.
107              
108             =head1 BUGS
109              
110             No bugs have been reported.
111              
112             Please report any bugs or feature requests through the web
113             interface at http://rt.cpan.org/Public/Dist/Display.html?Name=Sub-Clean
114              
115             =head1 AVAILABILITY
116              
117             The latest version of this module is available from the
118             Comprehensive Perl Archive Network (CPAN).
119             isit http://www.perl.com/CPAN/ to find a CPAN site near you,
120             or see http://search.cpan.org/dist/Sub-Clean/
121              
122             The development version lives at http://github.com/2shortplanks/Sub-Clean/.
123             Instead of sending patches, please fork this project using the standard
124             git and github infrastructure.
125              
126             =head1 SEE ALSO
127              
128             L<namespace::clean> allows you to clean your namespace by dividing your
129             code up into sections that should be cleaned or not cleaned.
130              
131             L<namespace::autoclean> automatically can clean imported subroutines
132             from other namespaces.
133              
134             L<Lexical::Sub> allows you to declare subroutines that only exist in
135             lexical scope.
136              
137             L<Attribute::Lexical> was used to create lexical attributes.
138              
139             =cut