| 1 |
NAME |
| 2 |
Filter::EOF - Run a callback after a file has been compiled |
| 3 |
|
| 4 |
VERSION |
| 5 |
0.04 |
| 6 |
|
| 7 |
SYNOPSIS |
| 8 |
package MyPackage; |
| 9 |
use warnings; |
| 10 |
use strict; |
| 11 |
|
| 12 |
use Filter::EOF; |
| 13 |
|
| 14 |
sub import { |
| 15 |
my ($class, @args) = @_; |
| 16 |
my $caller = scalar caller; |
| 17 |
|
| 18 |
# set the COMPILE_TIME package var to a false value |
| 19 |
# when the file was compiled |
| 20 |
Filter::EOF->on_eof_call(sub { |
| 21 |
no strict 'refs'; |
| 22 |
${ $caller . '::COMPILE_TIME' } = 0; |
| 23 |
}); |
| 24 |
|
| 25 |
# set the COMPILE_TIME package var to a true value when |
| 26 |
# we start compiling it. |
| 27 |
{ no strict 'refs'; |
| 28 |
${ $caller . '::COMPILE_TIME' } = 1; |
| 29 |
} |
| 30 |
} |
| 31 |
|
| 32 |
1; |
| 33 |
... |
| 34 |
|
| 35 |
package MyUsingPackage; |
| 36 |
use warnings; |
| 37 |
use strict; |
| 38 |
|
| 39 |
our $COMPILE_TIME; |
| 40 |
use MyPackage; |
| 41 |
|
| 42 |
# prints 'yes' |
| 43 |
BEGIN { print +( $COMPILE_TIME ? 'yes' : 'no' ), "\n" } |
| 44 |
|
| 45 |
# prints 'no' |
| 46 |
print +( $COMPILE_TIME ? 'yes' : 'no' ), "\n"; |
| 47 |
|
| 48 |
1; |
| 49 |
|
| 50 |
DESCRIPTION |
| 51 |
This module utilises Perl's source filters to provide you with a |
| 52 |
mechanism to run some code after a file using your module has been |
| 53 |
processed. |
| 54 |
|
| 55 |
METHODS |
| 56 |
"import( @functions )" |
| 57 |
Currently, only a function equivalent of the "on_eof_call" method is |
| 58 |
provided for export. |
| 59 |
|
| 60 |
use Filter::EOF qw( on_eof_call ); |
| 61 |
|
| 62 |
sub import { |
| 63 |
my ($class) = @_; |
| 64 |
... |
| 65 |
on_eof_call { ... }; |
| 66 |
} |
| 67 |
... |
| 68 |
|
| 69 |
"on_eof_call( $code_ref )" |
| 70 |
Call this method in your own "import" method to register a code |
| 71 |
reference that should be called when the file "use"ing yours was |
| 72 |
compiled. |
| 73 |
|
| 74 |
The code reference will get a scalar reference as first argument to an |
| 75 |
empty string. if you change this string to something else, it will be |
| 76 |
appended at the end of the source. |
| 77 |
|
| 78 |
# call C<some_function()> after runtime. |
| 79 |
Filter->on_eof_call(sub { |
| 80 |
my $append = shift; |
| 81 |
$$append .= '; some_function(); 1;'; |
| 82 |
}); |
| 83 |
|
| 84 |
EXPORTS |
| 85 |
on_eof_call |
| 86 |
You can optionally import the "on_eof_call" function into your |
| 87 |
namespace. |
| 88 |
|
| 89 |
EXAMPLES |
| 90 |
You can find the example mentioned in "SYNOPSIS" in the distribution |
| 91 |
directory "examples/synopsis/". |
| 92 |
|
| 93 |
SEE ALSO |
| 94 |
Filter::Call::Util, "Exporting without using Exporter's import method" |
| 95 |
in Exporter |
| 96 |
|
| 97 |
AUTHOR AND COPYRIGHT |
| 98 |
Robert 'phaylon' Sedlacek - "<[email protected]>". Many thanks to Matt S Trout |
| 99 |
for the idea and inspirations on this module. |
| 100 |
|
| 101 |
LICENSE |
| 102 |
This program is free software; you can redistribute it and/or modify it |
| 103 |
under the same terms as perl itself. |
| 104 |
|