1 package lib::with::preamble;
4 use warnings FATAL => 'all';
6 use PerlIO::via::dynamic;
8 our $VERSION = '0.001000'; # 0.1.0
10 sub require_with_preamble {
11 my ($arrayref, $filename) = @_;
12 my (undef, $preamble, @libs) = @$arrayref;
13 foreach my $cand (map File::Spec->catfile($_, $filename), @libs) {
15 if (open my $fh, '<', $cand) {
16 return with_preamble($preamble."\n#line 1 $cand\n", $fh);
23 my ($preamble, $fh) = @_;
24 PerlIO::via::dynamic->new(untranslate => sub {
25 $preamble and $_[1] =~ s/\A/$preamble/, undef($preamble);
31 my ($class, $preamble, @libs) = @_;
32 return unless defined($preamble) and @libs;
33 unshift @INC, [ \&require_with_preamble, $preamble, @libs ];
40 lib::with::preamble - invent your own default perl setup
44 use lib::with::preamble 'use v5.16; use strictures 1;', 'lib';
46 The above will load .pm files from lib/ - but they'll act as if your code
47 always started with 'use v5.16; use strictures 1;'.
49 =head1 USING THIS IN A DISTRIBUTION
51 To use this in a dist, you'll want to create two files -
54 use lib::with::preamble 'use v5.16; use strictures 1;', 'lib';
58 print "use v5.16;\nuse strictures 1;\n#line 1\n";
59 while (<STDIN>) { print }
61 and then tell your Makefile.PL to use the filter -
65 PM_FILTER => 'perl my/filter'
68 Then during development instead of doing
70 $ perl -Ilib bin/script-to-test
74 $ perl -Mmy::lib bin/script-to-test
78 $ PERL5OPT=-Mmy::lib prove t/some-test.t
84 your blib/ will get populated with files that already have your
87 $ prove -b t/some-test.t
89 will just work, as will
93 and when your users install your module, the .pm files will already have
94 the preamble at the top, so your installed files will look like
103 and everything should work, without you even needing to add this module
106 Patches to document an equivalent for those of you using L<Dist::Zilla>
107 (and L<Module::Build>, even if I don't like the bedamned thing) would be
112 This is as much a proof of concept as anything else at this point, so the
113 interface is NOT guaranteed to be stable. Especially since this is meant
114 to be a sort of implicit sugar, and history has proven that other people
115 are much better at designing APIs to sugar than I am.
117 But provided you're using it the way I describe above, the my/filter script
118 isn't dependent on anything, so your users will be insulated from that.
120 So please do have a play around and see if it works for you.
124 mst - Matt S. Trout (cpan:MSTROUT) <mst@shadowcat.co.uk>
128 None yet. Well volunteered? :)
132 Copyright (c) 2013 the lib::with::preamble L</AUTHOR> and L</CONTRIBUTORS>
137 This library is free software and may be distributed under the same terms