| 1 | ## | ||||||||||||||||||||||||||||
| 2 | # name: perl5 | ||||||||||||||||||||||||||||
| 3 | # abstract: Use a Perl 5 group of modules/features | ||||||||||||||||||||||||||||
| 4 | # author: Ingy döt Net <ingy@ingy.net> | ||||||||||||||||||||||||||||
| 5 | # license: perl | ||||||||||||||||||||||||||||
| 6 | # copyright: 2011 | ||||||||||||||||||||||||||||
| 7 | # see: | ||||||||||||||||||||||||||||
| 8 | # - perl5i | ||||||||||||||||||||||||||||
| 9 | # - perl5::i | ||||||||||||||||||||||||||||
| 10 | # - perl5::ingy | ||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | # To do: | ||||||||||||||||||||||||||||
| 13 | # - Turn die to croak (with tests) | ||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 1 | ▶ | use 5.008; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | package use::perl5; | ||||||||||||||||||||||||||||
| 17 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 1 | ▶ | use version 0.77 (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 21 | 1 | ▶ | use Hook::LexWrap 0.24; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 1 | our $VERSION = '0.08'; | |||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 1 | my $requested_perl_version = 0; | |||||||||||||||||||||||||||
| 26 | 1 | my $perl_version = 10; | |||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 0 | ▶ | sub VERSION { | ||||||||||||||||||||||||||
|
Subroutine
VERSION: not called CC=3 SCAR=20.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 29 | 0 | my ($class, $version) = @_; | |||||||||||||||||||||||||||
| 30 | 0 | $version = version->parse($version); | |||||||||||||||||||||||||||
| 31 | 0 | ▶ | if ($version < 10) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 32 | 0 | my $this_version = do { | |||||||||||||||||||||||||||
| 33 | 1 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 34 | 0 | version->parse(${$class . '::VERSION'}); | |||||||||||||||||||||||||||
| 35 | }; | ||||||||||||||||||||||||||||
| 36 | 0 | ▶ | if ($version > $this_version) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 37 | 0 | require Carp; | |||||||||||||||||||||||||||
| 38 | 0 | Carp::croak( | |||||||||||||||||||||||||||
| 39 | "$class version $version required" . | ||||||||||||||||||||||||||||
| 40 | "--this is only version $this_version" | ||||||||||||||||||||||||||||
| 41 | ); | ||||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||
| 44 | else { | ||||||||||||||||||||||||||||
| 45 | 0 | $requested_perl_version = $version; | |||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 0 | ▶ | sub version_check { | ||||||||||||||||||||||||||
|
Subroutine
version_check: not called CC=6 SCAR=37.4
Pod
undocumented
| |||||||||||||||||||||||||||||
| 50 | 0 | my ($class, $args) = @_; | |||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 0 | ▶ | if (defined $args->[0]) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 53 | 0 | my $version = $args->[0]; | |||||||||||||||||||||||||||
| 54 | 0 | $version =~ s/^-//; | |||||||||||||||||||||||||||
| 55 | 0 | ▶ | if (version::is_lax($version) and version->parse($version) >= 10) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
version::is_lax($version) and "version"->parse($version) >= 10
MC/DC0%0/2
version::is_lax($version) and "version"->parse($version) >= 10
version::is_lax($version)
"version"->parse($version) >= 10
Truth table0%0/3
A and B
A= version::is_lax($version)B= "version"->parse($version) >= 10
| |||||||||||||||||||||||||||||
| 56 | 0 | $requested_perl_version = version->parse($version); | |||||||||||||||||||||||||||
| 57 | 0 | shift(@$args); | |||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||
| 60 | 0 | ▶ | if ($requested_perl_version) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 61 | 0 | my $version = $requested_perl_version->numify / 1000 + 5; | |||||||||||||||||||||||||||
| 62 | 0 | $perl_version = $requested_perl_version; | |||||||||||||||||||||||||||
| 63 | 0 | $requested_perl_version = 0; | |||||||||||||||||||||||||||
| 64 | 0 | eval "use $version"; | |||||||||||||||||||||||||||
| 65 | 0 | ▶ | do { require Carp; Carp::croak($@) } if $@; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 0 | ▶ | sub import { | ||||||||||||||||||||||||||
|
Subroutine
import: not called CC=7 SCAR=40.3
| |||||||||||||||||||||||||||||
| 70 | 0 | ▶ | return unless @_; # XXX not sure why | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 71 | 0 | my $class = shift; | |||||||||||||||||||||||||||
| 72 | 0 | $class->version_check(\@_); | |||||||||||||||||||||||||||
| 73 | 0 | my $arg = shift; | |||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 0 | ▶ | if ($class ne 'perl5') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 76 | 0 | (my $usage = $class) =~ s/::/-/; | |||||||||||||||||||||||||||
| 77 | 0 | die "Don't 'use $class'. Try 'use $usage'"; | |||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | 0 | ▶ | die "Too many arguments for 'use perl5...'" if @_; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 0 | ▶ | my $subclass = | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 82 | not(defined($arg)) ? __PACKAGE__ : | ||||||||||||||||||||||||||||
| 83 | $arg =~ /^-(\w+)$/ ?__PACKAGE__ . "::$1" : | ||||||||||||||||||||||||||||
| 84 | die "'$arg' is an invalid first argument to 'use perl5...'"; | ||||||||||||||||||||||||||||
| 85 | 0 | ▶ | eval "require $subclass; 1" or die $@; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 0 | @_ = ($subclass); | |||||||||||||||||||||||||||
| 88 | 0 | goto &{$class->can('importer')}; | |||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | 1 | ▶ | sub importer { | ||||||||||||||||||||||||||
|
Subroutine
importer: called CC=7 SCAR=21.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 92 | 1 | my $class = shift; | |||||||||||||||||||||||||||
| 93 | 1 | ▶ | my @imports = scalar(@_) ? @_ : $class->imports; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 94 | 1 | my @wrappers; | |||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 1 | ▶ | my $important = sub {}; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 1 | ▶ | while (@imports) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 99 | 1 | my $name = shift(@imports); | |||||||||||||||||||||||||||
| 100 | 1 | ▶ | my $version = (@imports and version::is_lax($imports[0])) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
@imports and version::is_lax($imports[0])
MC/DC0%0/2
@imports and version::is_lax($imports[0])
@imports
version::is_lax($imports[0])
Truth table33%1/3
A and B
A= @importsB= version::is_lax($imports[0])
| |||||||||||||||||||||||||||||
| 101 | ? version->parse(shift(@imports))->numify : ''; | ||||||||||||||||||||||||||||
| 102 | 1 | ▶ | my $arguments = (@imports and ref($imports[0]) eq 'ARRAY') | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
@imports and ref $imports[0] eq "ARRAY"
MC/DC0%0/2
@imports and ref $imports[0] eq "ARRAY"
@imports
ref $imports[0] eq "ARRAY"
Truth table33%1/3
A and B
A= @importsB= ref $imports[0] eq "ARRAY"
| |||||||||||||||||||||||||||||
| 103 | ? shift(@imports) : undef; | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 1 | ▶ | $important = wrap $important => post => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=6 SCAR=19.4
| |||||||||||||||||||||||||||||
| 106 | 1 | ▶ | eval "use $name $version (); 1" or die $@; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 107 | 1 | ▶ | return if $arguments and not @$arguments; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$arguments and not @$arguments
MC/DC0%0/2
$arguments and not @$arguments
$arguments
not @$arguments
Truth table33%1/3
A and not(B)
A= $argumentsB= not @$arguments
| |||||||||||||||||||||||||||||
| 108 | 1 | ▶ | my $importee = $name->can('import') or return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 109 | 1 | ▶ | @_ = ($name, @{$arguments || []}); | ||||||||||||||||||||||||||
|
Condition50%1/2
$arguments || []
MC/DC0%0/1
$arguments || []
$arguments
Truth table50%1/2
A
A= $arguments
| |||||||||||||||||||||||||||||
| 110 | 1 | goto &$importee; | |||||||||||||||||||||||||||
| 111 | 1 | }; | |||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 1 | goto &$important; | |||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 0 | ▶ | sub imports { | ||||||||||||||||||||||||||
|
Subroutine
imports: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 118 | 0 | my $subversion = int($perl_version); | |||||||||||||||||||||||||||
| 119 | return ( | ||||||||||||||||||||||||||||
| 120 | 0 | 'strict', | |||||||||||||||||||||||||||
| 121 | 'warnings', | ||||||||||||||||||||||||||||
| 122 | 'feature' => [":5.$subversion"], | ||||||||||||||||||||||||||||
| 123 | ); | ||||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | 1 | 1; | |||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | Use a version of Perl and its feature set: | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | use perl5; # Same as 'use perl5 v5.10.0;' | ||||||||||||||||||||||||||||
| 133 | use perl5 v14.1; | ||||||||||||||||||||||||||||
| 134 | use perl5 14.1; | ||||||||||||||||||||||||||||
| 135 | use perl5-14.1; | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | Use a bundled feature set from a C<perl5> plugin: | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | use perl5-i; | ||||||||||||||||||||||||||||
| 140 | use perl5-2i; | ||||||||||||||||||||||||||||
| 141 | use perl5-modern; | ||||||||||||||||||||||||||||
| 142 | use perl5-yourShinyPlugin; | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | Or both: | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | use perl5 v14.1 -shiny; | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | The C<perl5> module lets you C<use> a well known set of modules in one | ||||||||||||||||||||||||||||
| 151 | command. | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | It allows people to create plugins like C<perl5::foo> and C<perl5::bar> that | ||||||||||||||||||||||||||||
| 154 | are sets of useful modules that have been tested together and are known to | ||||||||||||||||||||||||||||
| 155 | create joy. | ||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | This module, C<perl5>, is generally the base class to such a plugin. | ||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | =head1 USAGE | ||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | This: | ||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | use perl5-foo; | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | Is equivalent in Perl to: | ||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | use perl5 '-foo'; | ||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | The C<perl5> module takes the first argument in the C<use> command, and uses | ||||||||||||||||||||||||||||
| 170 | it to find a plugin, like C<perl5::foo> in this case. | ||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | C<perl5::foo> is typically just a subclass of L<perl5>. It invokes a set of | ||||||||||||||||||||||||||||
| 173 | modules for its caller. | ||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | If you use it with a version, like this: | ||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | use perl5 v14; | ||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | It is the same as saying: | ||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | use v5.14; | ||||||||||||||||||||||||||||
| 182 | use strict; | ||||||||||||||||||||||||||||
| 183 | use warnings; | ||||||||||||||||||||||||||||
| 184 | use feature ':5.14'; | ||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | If you use C<perl5> with no arguments, like this: | ||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | use perl5; | ||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | It is the same as saying: | ||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | use perl5 v10; | ||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | =head1 PLUGIN API | ||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||
| 196 | This module uses lexically-wrapped-goto-chaining-magic to correctly load a set | ||||||||||||||||||||||||||||
| 197 | of modules (including optional version requirements and import options) into | ||||||||||||||||||||||||||||
| 198 | the user's code. The API for specifying a perl5 plugin is very simple. | ||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||
| 200 | To create a plugin called C<perl5::foo> that gets called like this: | ||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||
| 202 | use perl5-foo; | ||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | Write some code like this: | ||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||
| 206 | package perl5::foo; | ||||||||||||||||||||||||||||
| 207 | use base 'perl5'; | ||||||||||||||||||||||||||||
| 208 | our $VERSION = 0.12; | ||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||
| 210 | # These is the list of modules (with optional version and arguments) | ||||||||||||||||||||||||||||
| 211 | sub imports { | ||||||||||||||||||||||||||||
| 212 | return ( | ||||||||||||||||||||||||||||
| 213 | strict => | ||||||||||||||||||||||||||||
| 214 | warnings => | ||||||||||||||||||||||||||||
| 215 | features => [':5.10'], | ||||||||||||||||||||||||||||
| 216 | SomeModule => 0.22, | ||||||||||||||||||||||||||||
| 217 | OtherModule => 0.33, [option1 => 2], | ||||||||||||||||||||||||||||
| 218 | Module => [], # Don't invoke Module's import() method | ||||||||||||||||||||||||||||
| 219 | ); | ||||||||||||||||||||||||||||
| 220 | } | ||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||
| 222 | 1; | ||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||
| 224 | =head1 INSPIRATION | ||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||
| 226 | This module was inspired by Michael Schwern's L<perl5i>, and the talk he gave | ||||||||||||||||||||||||||||
| 227 | about it at the 2010 OSDC in Melbourne. By "inspired" I mean that I was | ||||||||||||||||||||||||||||
| 228 | perturbed by Schwern's non-TMTOWTDI attitude towards choosing a standard set | ||||||||||||||||||||||||||||
| 229 | of Perl modules for all of us. | ||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||
| 231 | THIS IS PERL! THERE ARE NO STANDARDS! | ||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||
| 233 | ...and I told him so. I also promised that I would show him my feelings in | ||||||||||||||||||||||||||||
| 234 | code. Schwern, I<this> is how I feel! (See also: L<perl5::i>) | ||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||
| 236 | =head1 THANKS | ||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||
| 238 | Special thanks to schwern, mstrout, audreyt, rodrigo and jesse for ideas and | ||||||||||||||||||||||||||||
| 239 | support. | ||||||||||||||||||||||||||||