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