| 1 | package Package::Tent; | ||||||||||||||||||||
| 2 | 2 | ▶ | $VERSION = eval{require version}?version::qv($_):$_ for(0.0.1); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 3 | |||||||||||||||||||||
| 4 | 2 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | 2 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | 2 | ▶ | use Carp; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | |||||||||||||||||||||
| 8 | =head1 NAME | ||||||||||||||||||||
| 9 | |||||||||||||||||||||
| 10 | Package::Tent - temporary package infrastructure | ||||||||||||||||||||
| 11 | |||||||||||||||||||||
| 12 | =head1 SYNOPSIS | ||||||||||||||||||||
| 13 | |||||||||||||||||||||
| 14 | This module allows you to setup camp inside an existing module/program | ||||||||||||||||||||
| 15 | with minimal commitment to filenames, while still being able to use the | ||||||||||||||||||||
| 16 | same require/use statements of normal code. | ||||||||||||||||||||
| 17 | |||||||||||||||||||||
| 18 | use Package::Tent sub { | ||||||||||||||||||||
| 19 | package Who::sYourDaddy; | ||||||||||||||||||||
| 20 | sub thing { | ||||||||||||||||||||
| 21 | print "hello world\n"; | ||||||||||||||||||||
| 22 | } | ||||||||||||||||||||
| 23 | 1; | ||||||||||||||||||||
| 24 | }; | ||||||||||||||||||||
| 25 | use Package::Tent sub { | ||||||||||||||||||||
| 26 | package What::sInAName; | ||||||||||||||||||||
| 27 | use base 'Who::sYourDaddy'; | ||||||||||||||||||||
| 28 | sub method { | ||||||||||||||||||||
| 29 | $_[0]->thing; | ||||||||||||||||||||
| 30 | } | ||||||||||||||||||||
| 31 | __PACKAGE__; | ||||||||||||||||||||
| 32 | }; | ||||||||||||||||||||
| 33 | |||||||||||||||||||||
| 34 | use What::sInAName; | ||||||||||||||||||||
| 35 | What::sInAName->method; | ||||||||||||||||||||
| 36 | |||||||||||||||||||||
| 37 | =head1 USAGE | ||||||||||||||||||||
| 38 | |||||||||||||||||||||
| 39 | The 'use Package::Tent sub {...}' statement is equivalent to wrapping | ||||||||||||||||||||
| 40 | your package in a BEGIN block and setting an entry in %INC. | ||||||||||||||||||||
| 41 | |||||||||||||||||||||
| 42 | Note that the first example simply returns a true value, while the | ||||||||||||||||||||
| 43 | second explicitly returns the package name using the __PACKAGE__ token. | ||||||||||||||||||||
| 44 | You may use either method. | ||||||||||||||||||||
| 45 | |||||||||||||||||||||
| 46 | The implicit form will cause Package::Tent to attempt opening and | ||||||||||||||||||||
| 47 | scanning the file containing the calling code. The latter is more | ||||||||||||||||||||
| 48 | robust in strange (PAR, @INC hooks, etc.) environments, but is less | ||||||||||||||||||||
| 49 | convenient. | ||||||||||||||||||||
| 50 | |||||||||||||||||||||
| 51 | =head1 NOTES | ||||||||||||||||||||
| 52 | |||||||||||||||||||||
| 53 | It is not wise to install a datacenter in a tent. Need I say more? | ||||||||||||||||||||
| 54 | |||||||||||||||||||||
| 55 | This module was designed to reduce development time by allowing me to | ||||||||||||||||||||
| 56 | maintain my train of thought. The scenario is that you're coding along | ||||||||||||||||||||
| 57 | in some module or program and you realize the need for a support module | ||||||||||||||||||||
| 58 | (or even a few of them.) Package::Tent allows you to keep writing code | ||||||||||||||||||||
| 59 | while you're thinking rather than stopping long enough to commit to a | ||||||||||||||||||||
| 60 | name, create a file, maybe add it to version control, etc. | ||||||||||||||||||||
| 61 | |||||||||||||||||||||
| 62 | It should be similarly useful in single-file prototypes or other | ||||||||||||||||||||
| 63 | experimental code. Hopefully, lowering the file-juggling overhead | ||||||||||||||||||||
| 64 | encourages you to start your code with a modular style. When the | ||||||||||||||||||||
| 65 | prototype becomes the finished product (as it so often does), the | ||||||||||||||||||||
| 66 | refactoring is nearly mechanical as opposed to a difficult untangling of | ||||||||||||||||||||
| 67 | ad-hoc variables. | ||||||||||||||||||||
| 68 | |||||||||||||||||||||
| 69 | =cut | ||||||||||||||||||||
| 70 | |||||||||||||||||||||
| 71 | =head1 Methods | ||||||||||||||||||||
| 72 | |||||||||||||||||||||
| 73 | =head2 import | ||||||||||||||||||||
| 74 | |||||||||||||||||||||
| 75 | use Package::Tent sub { | ||||||||||||||||||||
| 76 | ... | ||||||||||||||||||||
| 77 | }; | ||||||||||||||||||||
| 78 | |||||||||||||||||||||
| 79 | Package::Tent->import($subref); | ||||||||||||||||||||
| 80 | |||||||||||||||||||||
| 81 | =cut | ||||||||||||||||||||
| 82 | |||||||||||||||||||||
| 83 | 12 | ▶ | sub import { | ||||||||||||||||||
|
Subroutine
import: called CC=9 SCAR=22.7
| |||||||||||||||||||||
| 84 | 12 | my $self = shift; | |||||||||||||||||||
| 85 | 12 | ▶ | @_ or return; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 86 | 11 | my ($subref) = @_; | |||||||||||||||||||
| 87 | |||||||||||||||||||||
| 88 | 11 | ▶ | ((ref($subref) || '') eq 'CODE') or croak("must be a subref"); | ||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
ref $subref || ''
MC/DC0%0/1
ref $subref || ''
ref $subref
Truth table50%1/2
A
A= ref $subref
| |||||||||||||||||||||
| 89 | |||||||||||||||||||||
| 90 | 11 | my ($p, $fn, $line) = caller; | |||||||||||||||||||
| 91 | |||||||||||||||||||||
| 92 | 11 | my $v = eval {$subref->()}; | |||||||||||||||||||
| 93 | 11 | ▶ | $@ and die; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 94 | 11 | ▶ | $v or croak("$subref did not return a true value"); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 95 | |||||||||||||||||||||
| 96 | 11 | ▶ | unless($v =~ m/^[a-z][a-z0-9:_]*$/i) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 97 | 9 | $v = eval {$self->_find_package($fn, $line)}; | |||||||||||||||||||
| 98 | 9 | ▶ | if($@) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 99 | 0 | croak($@, "\n\n -- you should use the __PACKAGE__ tag\n\n "); | |||||||||||||||||||
| 100 | } | ||||||||||||||||||||
| 101 | 9 | ▶ | $v or croak("failed to determine package name"); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 102 | } | ||||||||||||||||||||
| 103 | |||||||||||||||||||||
| 104 | 11 | my $pf = $v . '.pm'; | |||||||||||||||||||
| 105 | 11 | $pf =~ s#::#/#g; | |||||||||||||||||||
| 106 | 11 | $INC{$pf} = $fn; | |||||||||||||||||||
| 107 | } # end subroutine import definition | ||||||||||||||||||||
| 108 | ######################################################################## | ||||||||||||||||||||
| 109 | |||||||||||||||||||||
| 110 | =head2 _find_package | ||||||||||||||||||||
| 111 | |||||||||||||||||||||
| 112 | Package::Tent->_find_package($file, $line); | ||||||||||||||||||||
| 113 | |||||||||||||||||||||
| 114 | =cut | ||||||||||||||||||||
| 115 | |||||||||||||||||||||
| 116 | 9 | ▶ | sub _find_package { | ||||||||||||||||||
|
Subroutine
_find_package: called CC=11 SCAR=24.3
| |||||||||||||||||||||
| 117 | 9 | my $self = shift; | |||||||||||||||||||
| 118 | 9 | my ($file, $num) = @_; | |||||||||||||||||||
| 119 | 9 | ▶ | open(my $fh, '<', $file) or die("cannot open $file"); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 120 | |||||||||||||||||||||
| 121 | 9 | ▶ | my $pname = ref($self) || $self; | ||||||||||||||||||
|
Condition33%1/3
ref $self || $self
MC/DC0%0/2
ref $self || $self
ref $self
$self
Truth table33%1/3
A || B
A= ref $selfB= $self
| |||||||||||||||||||||
| 122 | |||||||||||||||||||||
| 123 | 2 | ▶ | use constant DBG => 0; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 124 | |||||||||||||||||||||
| 125 | 9 | DBG and warn "XX pname is $pname\n"; | |||||||||||||||||||
| 126 | |||||||||||||||||||||
| 127 | 9 | my $ln = 0; | |||||||||||||||||||
| 128 | 9 | my $in_use = 0; | |||||||||||||||||||
| 129 | 9 | my $got_pack; | |||||||||||||||||||
| 130 | 9 | ▶ | while(my $line = <$fh>) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 131 | 45 | $ln++; | |||||||||||||||||||
| 132 | 45 | DBG and warn "## $line"; | |||||||||||||||||||
| 133 | 45 | ▶ | if($line =~ m/^\s*use \Q$pname\E *\(? *sub *\{(.*)/) { | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 134 | 14 | ▶ | if(my $also = $1) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 135 | 6 | ▶ | if($also =~ m/\bpackage ([\w:]+)/) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 136 | 6 | $got_pack = $1; | |||||||||||||||||||
| 137 | } | ||||||||||||||||||||
| 138 | } | ||||||||||||||||||||
| 139 | else { | ||||||||||||||||||||
| 140 | 8 | $got_pack = undef; | |||||||||||||||||||
| 141 | } | ||||||||||||||||||||
| 142 | 14 | DBG and warn "XX in_use\n"; | |||||||||||||||||||
| 143 | 14 | $in_use = 1; | |||||||||||||||||||
| 144 | } | ||||||||||||||||||||
| 145 | ▶ | elsif($in_use and ! defined($got_pack)) { | |||||||||||||||||||
|
Condition66%2/3
$in_use and not defined $got_pack
MC/DC50%1/2
$in_use and not defined $got_pack
$in_use
not defined $got_pack
Truth table66%2/3
A and not(B)
A= $in_useB= not defined $got_pack
| |||||||||||||||||||||
| 146 | 8 | DBG and warn "XX check\n"; | |||||||||||||||||||
| 147 | 8 | ▶ | if($line =~ m/(?:^|;)\s*package ([\w:]+)/) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 148 | 8 | $got_pack = $1; | |||||||||||||||||||
| 149 | 8 | DBG and warn "%% got $got_pack\n"; | |||||||||||||||||||
| 150 | } | ||||||||||||||||||||
| 151 | } | ||||||||||||||||||||
| 152 | 45 | ▶ | ($ln >= $num) and last; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 153 | } | ||||||||||||||||||||
| 154 | |||||||||||||||||||||
| 155 | 9 | return($got_pack); | |||||||||||||||||||
| 156 | } # end subroutine _find_package definition | ||||||||||||||||||||
| 157 | ######################################################################## | ||||||||||||||||||||
| 158 | |||||||||||||||||||||
| 159 | =head1 AUTHOR | ||||||||||||||||||||
| 160 | |||||||||||||||||||||
| 161 | Eric Wilhelm @ <ewilhelm at cpan dot org> | ||||||||||||||||||||
| 162 | |||||||||||||||||||||
| 163 | http://scratchcomputing.com/ | ||||||||||||||||||||
| 164 | |||||||||||||||||||||
| 165 | =head1 BUGS | ||||||||||||||||||||
| 166 | |||||||||||||||||||||
| 167 | If you found this module on CPAN, please report any bugs or feature | ||||||||||||||||||||
| 168 | requests through the web interface at L<http://rt.cpan.org>. I will be | ||||||||||||||||||||
| 169 | notified, and then you'll automatically be notified of progress on your | ||||||||||||||||||||
| 170 | bug as I make changes. | ||||||||||||||||||||
| 171 | |||||||||||||||||||||
| 172 | If you pulled this development version from my /svn/, please contact me | ||||||||||||||||||||
| 173 | directly. | ||||||||||||||||||||
| 174 | |||||||||||||||||||||
| 175 | =head1 COPYRIGHT | ||||||||||||||||||||
| 176 | |||||||||||||||||||||
| 177 | Copyright (C) 2007 Eric L. Wilhelm, All Rights Reserved. | ||||||||||||||||||||
| 178 | |||||||||||||||||||||
| 179 | =head1 NO WARRANTY | ||||||||||||||||||||
| 180 | |||||||||||||||||||||
| 181 | Absolutely, positively NO WARRANTY, neither express or implied, is | ||||||||||||||||||||
| 182 | offered with this software. You use this software at your own risk. In | ||||||||||||||||||||
| 183 | case of loss, no person or entity owes you anything whatsoever. You | ||||||||||||||||||||
| 184 | have been warned. | ||||||||||||||||||||
| 185 | |||||||||||||||||||||
| 186 | =head1 LICENSE | ||||||||||||||||||||
| 187 | |||||||||||||||||||||
| 188 | This program is free software; you can redistribute it and/or modify it | ||||||||||||||||||||
| 189 | under the same terms as Perl itself. | ||||||||||||||||||||
| 190 | |||||||||||||||||||||
| 191 | =cut | ||||||||||||||||||||
| 192 | |||||||||||||||||||||
| 193 | # vi:ts=2:sw=2:et:sta | ||||||||||||||||||||
| 194 | 2 | 1; | |||||||||||||||||||