File Coverage

blib/lib/lib/with/preamble.pm
Criterion Covered Total %
statement 28 28 100.0
branch 6 8 75.0
condition 1 3 33.3
subroutine 9 9 100.0
pod 0 2 0.0
total 44 50 88.0


line stmt bran cond sub pod time code
1 1     1   18741 use strict;
  1         2  
  1         45  
2             #line 1
3 1     1   4 package lib::with::preamble;
  1         1  
  1         22  
4 1     1   3  
  1         10  
  1         88  
5 1     1   4 use strict;
  1         1  
  1         22  
6 1     1   489 use warnings FATAL => 'all';
  1         6102  
  1         229  
7             use File::Spec;
8             use PerlIO::via::dynamic;
9              
10             our $VERSION = '0.001002'; # 0.1.2
11 9     9 0 118768  
12 9         20 sub require_with_preamble {
13 9         98 my ($arrayref, $filename) = @_;
14 9 100       4191 my (undef, $preamble, @libs) = @$arrayref;
15 1 50       23 foreach my $cand (map File::Spec->catfile($_, $filename), @libs) {
16 1         4 if (-f $cand) {
17             if (open my $fh, '<', $cand) {
18             return with_preamble($preamble."\n#line 1 $cand\n", $fh);
19             }
20             }
21             }
22             }
23 1     1 0 2  
24             sub with_preamble {
25 14 100   14   493 my ($preamble, $fh) = @_;
26 1         8 PerlIO::via::dynamic->new(untranslate => sub {
27 1         355 $preamble and $_[1] =~ s/\A/$preamble/, undef($preamble);
28             })->via($fh);
29             return $fh;
30             }
31 1     1   8  
32 1 50 33     8 sub import {
33 1         24 my ($class, $preamble, @libs) = @_;
34             return unless defined($preamble) and @libs;
35             unshift @INC, [ \&require_with_preamble, $preamble, @libs ];
36             }
37              
38             1;
39              
40             =head1 NAME
41              
42             lib::with::preamble - invent your own default perl setup
43              
44             =head1 SYNOPSIS
45              
46             use lib::with::preamble 'use v5.16; use strictures 1;', 'lib';
47              
48             The above will load .pm files from lib/ - but they'll act as if your code
49             always started with 'use v5.16; use strictures 1;'.
50              
51             =head1 USING THIS IN A DISTRIBUTION
52              
53             To use this in a dist, you'll want to create two files -
54              
55             # my/lib.pm
56             use lib::with::preamble 'use v5.16; use strictures 1;', 'lib';
57             1;
58              
59             # my/filter
60             print "use v5.16;\nuse strictures 1;\n#line 1\n";
61             while () { print }
62              
63             and then tell your Makefile.PL to use the filter -
64              
65             WriteMakefile(
66             ...
67             PM_FILTER => 'perl my/filter'
68             );
69              
70             Then during development instead of doing
71              
72             $ perl -Ilib bin/script-to-test
73              
74             you'll want to do -
75              
76             $ perl -Mmy::lib bin/script-to-test
77              
78             and for prove -
79              
80             $ PERL5OPT=-Mmy::lib prove t/some-test.t
81              
82             but once you run
83              
84             $ make
85              
86             your blib/ will get populated with files that already have your
87             preamble added, so
88              
89             $ prove -b t/some-test.t
90              
91             will just work, as will
92              
93             $ make test
94              
95             and when your users install your module, the .pm files will already have
96             the preamble at the top, so your installed files will look like
97              
98             # My/Foo.pm
99             use v5.16;
100             use strictures 1;
101             # line 1
102             package My::Foo;
103             ...
104              
105             and everything should work, without you even needing to add this module
106             as a dependency.
107              
108             Patches to document an equivalent for those of you using L
109             (and L, even if I don't like the bedamned thing) would be
110             very welcome.
111              
112             =head1 WARNING
113              
114             This is as much a proof of concept as anything else at this point, so the
115             interface is NOT guaranteed to be stable. Especially since this is meant
116             to be a sort of implicit sugar, and history has proven that other people
117             are much better at designing APIs to sugar than I am.
118              
119             But provided you're using it the way I describe above, the my/filter script
120             isn't dependent on anything, so your users will be insulated from that.
121              
122             So please do have a play around and see if it works for you.
123              
124             =head1 AUTHOR
125              
126             mst - Matt S. Trout (cpan:MSTROUT)
127              
128             =head1 CONTRIBUTORS
129              
130             None yet. Well volunteered? :)
131              
132             =head1 COPYRIGHT
133              
134             Copyright (c) 2013 the lib::with::preamble L and L
135             as listed above.
136              
137             =head1 LICENSE
138              
139             This library is free software and may be distributed under the same terms
140             as perl itself.
141              
142             =cut