blib/lib/new.pm

filter: Statementstmt 100% 20 / 20 Branchbran 83.3% 5 / 6 Conditioncond 100% 3 / 3 MC/DCmcdc 100% 2 / 2 Subroutinesub 100% 4 / 4 Podpod n/a totaltotal 97.1% 34 / 35 CC 5 SCAR 12.5
CC
5
Cov
97%
import
5.0
BEGIN
1.0
BEGIN
1.0
CRAP 3.5
↑ Summary
1 package new;
2  
3 1 our $VERSION = '0.000002'; # v0.0.2
4  
5 1 $VERSION = eval $VERSION;
6  
7 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 5 ▶sub import {
Subroutine import: called CC=5 SCAR=16.1
11 5 my ($me, $class, @args) = @_;
12 5 ▶ return unless $class;
Branch: 1 branch
BranchTrueFalseTotal
unless $class 1 4 5
13 4 my $targ = caller;
14 4 require join('/', split '::', $class).'.pm';
15 4 ▶ my ($name) = @args && $args[0] =~ /^\$/ ? map /^\$(.*)/, shift @args : 'O';
Branch: 1 branch
BranchTrueFalseTotal
@args && $args[0] =~ /^\$/ ? : 2 2 4
Condition100%3/3
@args and $args[0] =~ /^\$/
!ll&&!rl&&r
112
MC/DC100%2/2
@args and $args[0] =~ /^\$/
@args $args[0] =~ /^\$/
Truth table100%3/3
A and B
A= @argsB= $args[0] =~ /^\$/
ABresult
0X0
100
111
16 4 ▶ my $obj = $class->can('new') ? $class->new(@args) : $class;
Branch: 1 branch
BranchTrueFalseTotal
$class->can('new') ? : 4 0 4
17 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
18 4 ${"${targ}::${name}"} = $obj;
19 }
20  
21 1 1;
22  
23 =head1 NAME
24  
25 new - Object instantiation sugar for one-liners
26  
27 =head1 SYNOPSIS
28  
29 Simplest possible usage:
30  
31 perl -Mnew=HTTP::Tiny -E \
32 'say $O->get("http://trout.me.uk/X11/vimrc")->{content}'
33  
34 With arguments:
35  
36 perl -Mnew=HTTP::Tiny,max_redirects,3 -E \
37 'say $O->get("http://trout.me.uk/X11/vimrc")->{content}'
38  
39 With custom object name:
40  
41 perl -Mnew=HTTP::Tiny,\$H -E \
42 'say $H->get("http://trout.me.uk/X11/vimrc")->{content}'
43  
44 With both:
45  
46 perl -Mnew=HTTP::Tiny,\$H,max_redirects,3 -E \
47 'say $H->get("http://trout.me.uk/X11/vimrc")->{content}'
48  
49 =head1 DESCRIPTION
50  
51 =head2 import
52  
53 new->import($class, @args)
54  
55 First we C<require> the file for C<$class>, then call
56  
57 $class->new(@args)
58  
59 then install the resulting object in C<$O> in the calling package.
60  
61 B<Caveat>: if C<$class> loads successfully but does not have a C<new> method,
62 we install the C<$class> instead since you might want to call class methods.
63  
64 If the first argument to C<import> after C<$class> begins with C<$>, this
65 is treated as the name to install the object as, so
66  
67 new->import($class, '$Obj', @args);
68  
69 will create a variable C<$Obj> in the calling package instead of C<$O>.
70  
71 =head1 AUTHOR
72  
73 mst - Matt S. Trout (cpan:MSTROUT) <mst@shadowcat.co.uk>
74  
75 =head1 CONTRIBUTORS
76  
77 None yet - maybe this software is perfect! (ahahahahahahahahaha)
78  
79 =head1 COPYRIGHT
80  
81 Copyright (c) 2020 the new L</AUTHOR> and L</CONTRIBUTORS>
82 as listed above.
83  
84 =head1 LICENSE
85  
86 This library is free software and may be distributed under the same terms
87 as perl itself.
↑ Summary