blib/lib/Filter/EOF.pm

filter: Statementstmt 100% 37 / 37 Branchbran 83.3% 5 / 6 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 10 / 10 Podpod 100% 1 / 1 totaltotal 98.1% 53 / 54 CC 5 SCAR 6.2
CC
5
Cov
98%
import
3.0
__ANON__
3.0
BEGIN
1.0
CRAP 1.9
↑ Summary
1 package Filter::EOF;
2  
3 =head1 NAME
4  
5 Filter::EOF - Run a callback after a file has been compiled
6  
7 =cut
8  
9 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 =head1 VERSION
13  
14 0.04
15  
16 =cut
17  
18 5 our $VERSION = 0.04;
19  
20 =head1 SYNOPSIS
21  
22 package MyPackage;
23 use warnings;
24 use strict;
25  
26 use Filter::EOF;
27  
28 sub import {
29 my ($class, @args) = @_;
30 my $caller = scalar caller;
31  
32 # set the COMPILE_TIME package var to a false value
33 # when the file was compiled
34 Filter::EOF->on_eof_call(sub {
35 no strict 'refs';
36 ${ $caller . '::COMPILE_TIME' } = 0;
37 });
38  
39 # set the COMPILE_TIME package var to a true value when
40 # we start compiling it.
41 { no strict 'refs';
42 ${ $caller . '::COMPILE_TIME' } = 1;
43 }
44 }
45  
46 1;
47 ...
48  
49 package MyUsingPackage;
50 use warnings;
51 use strict;
52  
53 our $COMPILE_TIME;
54 use MyPackage;
55  
56 # prints 'yes'
57 BEGIN { print +( $COMPILE_TIME ? 'yes' : 'no' ), "\n" }
58  
59 # prints 'no'
60 print +( $COMPILE_TIME ? 'yes' : 'no' ), "\n";
61  
62 1;
63  
64 =head1 DESCRIPTION
65  
66 This module utilises Perl's source filters to provide you with a
67 mechanism to run some code after a file using your module has been
68 processed.
69  
70 =cut
71  
72 5 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
73 5 ▶use Filter::Util::Call;
Subroutine BEGIN: called CC=1 SCAR=0.0
74  
75 =head1 METHODS
76  
77 =head2 C<import( @functions )>
78  
79 Currently, only a function equivalent of the C<on_eof_call> method
80 is provided for export.
81  
82 use Filter::EOF qw( on_eof_call );
83  
84 sub import {
85 my ($class) = @_;
86 ...
87 on_eof_call { ... };
88 }
89 ...
90  
91 =cut
92  
93 my %Export = (
94 1 ▶ on_eof_call => sub { my $class = shift; sub (&) { $class->on_eof_call(@_) } },
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
95 5 );
96  
97 4 ▶sub import {
Subroutine import: called CC=3 SCAR=11.0
98 4 my ($class, @names) = @_;
99 4 for my $name (@names) {
100 1 ▶ Carp::croak "Unknown function '$name'" unless exists $Export{ $name };
Branch: 1 branch
BranchTrueFalseTotal
unless exists $Export{$name} 0 1 1
101 5 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
102 1 *{ scalar(caller) . '::' . $name } = $Export{ $name }->($class);
103 }
104 4 return 1;
105 }
106  
107 =head2 C<on_eof_call( $code_ref )>
108  
109 Call this method in your own C<import> method to register a code
110 reference that should be called when the file C<use>ing yours was
111 compiled.
112  
113 The code reference will get a scalar reference as first argument
114 to an empty string. if you change this string to something else,
115 it will be appended at the end of the source.
116  
117 # call C<some_function()> after runtime.
118 Filter->on_eof_call(sub {
119 my $append = shift;
120 $$append .= '; some_function(); 1;';
121 });
122  
123 =cut
124  
125 3 ▶sub on_eof_call {
Subroutine on_eof_call: called CC=1 SCAR=0.0
Pod documented
126 3 my ($class, $code) = @_;
127  
128 3 my $past_eof;
129 23 ▶ filter_add( sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
130 23 my $status = filter_read;
131 23 ▶ return $status if $past_eof;
Branch: 1 branch
BranchTrueFalseTotal
if $past_eof 3 20 23
132 20 ▶ if ($status == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($status == 0) 3 17 20
133 3 $code->(\$_);
134 3 $status = 1;
135 3 $past_eof = 1;
136 }
137 20 return $status;
138 3 });
139 }
140  
141 =head1 EXPORTS
142  
143 =head2 on_eof_call
144  
145 You can optionally import the C<on_eof_call> function into your namespace.
146  
147 =head1 EXAMPLES
148  
149 You can find the example mentioned in L</SYNOPSIS> in the distribution
150 directory C<examples/synopsis/>.
151  
152 =head1 SEE ALSO
153  
154 L<Filter::Call::Util>,
155 L<Exporter/Exporting without using Exporter's import method>
156  
157 =head1 AUTHOR AND COPYRIGHT
158  
159 Robert 'phaylon' Sedlacek - C<E<lt>rs@474.atE<gt>>. Many thanks to
160 Matt S Trout for the idea and inspirations on this module.
161  
162 =head1 LICENSE
163  
164 This program is free software; you can redistribute it and/or modify
165 it under the same terms as perl itself.
166  
167 =cut
168  
169 5 1;
↑ Summary