File Coverage

blib/lib/Call/Immediate.pm
Criterion Covered Total %
statement 27 27 100.0
branch n/a
condition n/a
subroutine 8 8 100.0
pod n/a
total 35 35 100.0


line stmt bran cond sub pod time code
1             package Call::Immediate;
2              
3 1     1   39473 use 5.006001;
  1         4  
  1         55  
4 1     1   7 use strict;
  1         3  
  1         35  
5 1     1   5 use warnings;
  1         15  
  1         48  
6              
7             our $VERSION = '0.01';
8              
9 1     1   1950 use Filter::Simple ();
  1         69839  
  1         106  
10              
11             sub import {
12 1     1   10 my ($self, @subs) = @_;
13             my $filter = sub {
14 1     1   1393 for my $sub (@subs) {
15 2         70 s/^ (\s*) ($sub) \b /$1use Call::Immediate::Call $2/xm;
16             }
17 1         5 };
18 1         3 my $caller = caller;
19 1     1   12 no strict 'refs';
  1         2  
  1         32  
20 1     1   6 no warnings 'redefine';
  1         2  
  1         122  
21 1         4 *{"$caller\::import"} = Filter::Simple::gen_filter_import($caller, $filter);
  1         14  
22 1         4 *{"$caller\::unimport"} = Filter::Simple::gen_filter_unimport($caller);
  1         38  
23             }
24              
25             1;
26              
27             =head1 NAME
28              
29             Call::Immediate - Export subs that are called as soon as they are seen at compile time
30              
31             =head1 SYNOPSIS
32              
33             package MyModule;
34              
35             sub class {
36             # muck around with symbol tables and stuff
37             }
38            
39             # still have to export manually
40             use Exporter;
41             use base 'Exporter';
42             our @EXPORT = qw;
43              
44             # Use this module to have them executed immediately
45             # when they are seen.
46             # ALWAYS use this module at the END of your module definition!
47             use Call::Immediate qw;
48              
49             =head1 DESCRIPTION
50              
51             This module installs a very simple source filter that causes the subs you
52             specify to be called as soon as they are seen, like macros (but ones that don't
53             substitute any text). This allows you to create slightly more natural
54             constructs like:
55              
56             class Foo => sub {
57             ...
58             };
59              
60             As soon as this is seen in the user's file, your C sub will be executed,
61             so that you can mess with the symbol table and affect compilation of the rest
62             of the file if you like.
63              
64             This is implemented by scanning for the subs you specify in the left column
65             (permitting optional whitespace before it) and replacing it with a "use"
66             declaration. Specifically, the call you see above would be turned into:
67              
68             use Call::Immediate::Call class Foo => sub {
69             ...
70             };
71              
72             Where C is a module that simply does nothing on import.
73              
74             =head1 AUTHOR
75              
76             Luke Palmer