File Coverage

blib/lib/new.pm
Criterion Covered Total %
statement 17 17 100.0
branch 5 6 83.3
condition 3 3 100.0
subroutine 4 4 100.0
pod n/a
total 29 30 96.6


line stmt bran cond sub pod time code
1             package new;
2              
3             our $VERSION = '0.000002'; # v0.0.2
4              
5             $VERSION = eval $VERSION;
6              
7 1     1   57047 use strict;
  1         2  
  1         24  
8 1     1   4 use warnings;
  1         3  
  1         119  
9              
10             sub import {
11 5     5   2418 my ($me, $class, @args) = @_;
12 5 100       1235 return unless $class;
13 4         7 my $targ = caller;
14 4         288 require join('/', split '::', $class).'.pm';
15 4 100 100     166 my ($name) = @args && $args[0] =~ /^\$/ ? map /^\$(.*)/, shift @args : 'O';
16 4 50       26 my $obj = $class->can('new') ? $class->new(@args) : $class;
17 1     1   6 no strict 'refs';
  1         2  
  1         73  
18 4         20 ${"${targ}::${name}"} = $obj;
  4         17  
19             }
20              
21             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 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: if C<$class> loads successfully but does not have a C method,
62             we install the C<$class> instead since you might want to call class methods.
63              
64             If the first argument to C 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)
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 and L
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.