File Coverage

blib/lib/here.pm
Criterion Covered Total %
statement 24 32 75.0
branch 4 6 66.6
condition n/a
subroutine 7 8 87.5
pod 0 3 0.0
total 35 49 71.4


line stmt bran cond sub pod time code
1             package here;
2 3     3   58209 use warnings;
  3         76  
  3         139  
3 3     3   18 use strict;
  3         5  
  3         113  
4 3     3   4604 use Filter::Util::Call qw(filter_add filter_del);
  3         4637  
  3         2199  
5             # fear not a filter that filters not not a filter be
6             our $DEBUG;
7            
8             sub import {
9 24     24   61 shift;
10 24 100       1659 if (@_) {
11 20         29 my $code = join ';' => map {(my $x = $_) =~ s/;\s*$//; $x} @_;
  44         133  
  44         106  
12 20         56 my (undef, $file, $line) = caller;
13             filter_add sub {
14 20 50   20   333 if ($DEBUG) {
15 0         0 (my $msg = $code) =~ s/\s+/ /g;
16 0         0 warn "use here: $msg at $file line $line.\n";
17             }
18 20         69 $_ = "# line $line\n$code;\n# line $line\n\n";
19 20         30 filter_del;
20 20         1033 1
21             }
22 20         142 }
23             }
24            
25             sub croak {
26 0     0 0 0 s/\s+/ /g for my $msg = "@_";
27 0         0 my $i;
28 0         0 1 while (caller ++$i) =~ /^here(::.+)?$/;
29 0         0 my (undef, $file, $line) = caller $i;
30 0         0 die "$msg at $file line $line.\n"
31             }
32            
33             my ($key, %data);
34             sub store {
35 29     29 0 69 $data{++$key} = $_[0];
36 29         73 "here::fetch($key)"
37             }
38            
39             sub fetch {
40 29 50   29 0 123 if (exists $data{$_[0]}) {
  0            
41 29         1560 delete $data{$_[0]}
42             }
43             else {croak "here::fetch: invalid key '$_[0]'"}
44             }
45            
46             our $VERSION = '0.03';
47            
48            
49             =head1 NAME
50            
51             here - insert generated source here
52            
53             =head1 VERSION
54            
55             version 0.03
56            
57             =head1 SYNOPSIS
58            
59             this module replaces a call to C< use here LIST; > with the contents of
60             C< LIST > at compile time. perl then compiles C< LIST > and the remaining code.
61             there is B an implicit block around C< LIST >
62            
63             an example is probably best:
64            
65             my $x;
66             use here 'my $y';
67             my $z;
68            
69             is exactly equivalent to:
70            
71             my $x;
72             my $y;
73             my $z;
74            
75             the important thing here is that C< $y > is still in scope, which would not be
76             the case with a runtime C< eval >:
77            
78             my $x;
79             eval 'my $y';
80             my $z; # $y is not in scope here!
81            
82             =head1 EXPORT
83            
84             this module does not export anything, and must always be invoked at compile time
85             as:
86            
87             use here LIST;
88            
89             it is intended to be used with a transformation function to allow new syntactic
90             sugar:
91            
92             sub my_0 {map {"my \$$_ = 0"} @_}
93            
94             use here my_0 qw(x y z);
95            
96             which results in perl compiling:
97            
98             my $x = 0; my $y = 0; my $z = 0;
99            
100             note the inserted semicolons (between every element of C and at the end).
101            
102             you can utilize the C< here::install > mechanism to make the code even shorter:
103            
104             use here::install my_0 => sub {map {"my \$$_ = 0"} @_};
105            
106             use my_0 qw(x y z);
107            
108             C< here::install > has dynamic lexical scope if L is
109             available. otherwise it is global and you can call:
110            
111             no here::install 'my_0';
112            
113             when you are done with the macro if you want to clean up.
114            
115             =head1 SEE ALSO
116            
117             see L and L for additional examples.
118            
119             see L to view what C is doing.
120            
121             =head1 AUTHOR
122            
123             Eric Strom, C<< >>
124            
125             =head1 BUGS
126            
127             code following a C< use here ...; > line must be placed on a new line if that
128             code needs to be in the scope of the C< use here >
129            
130             $first->()
131             use here '$second->()'; # comments are fine
132             $third->();
133            
134             $first->();
135             use here '$third->()'; $second->(); # but this is out of order
136             $fourth->();
137            
138             use here 'my $x = 1'; # $x not in scope
139             # $x in scope
140            
141             as far as i can tell, this is a limitation of perl /C< Filter::Util::Call > and
142             not of this module. patches welcome if this is not the case.
143            
144             please don't fear that i've mentioned that this module uses
145             L, since this module filters naught. all it does is insert
146             C< LIST > at the top of perl's queue of lines to compile. the filter is removed
147             at the same time, never to be called again. so fear not a filter that filters
148             not not a filter be.
149            
150             write C< use here::debug; > before a C< use here LIST; > line to carp the
151             contents of C< LIST > when it is inserted into the source.
152            
153             please report any bugs or feature requests to C, or
154             through the web interface at
155             L. I will be notified, and
156             then you'll automatically be notified of progress on your bug as I make changes.
157            
158             =head1 LICENSE AND COPYRIGHT
159            
160             copyright 2011 Eric Strom.
161            
162             this program is free software; you can redistribute it and/or modify it
163             under the terms of either: the GNU General Public License as published
164             by the Free Software Foundation; or the Artistic License.
165            
166             See http://dev.perl.org/licenses/ for more information.
167            
168             =cut
169            
170             1