MkTempO.pm

filter: Statementstmt 98.3% 61 / 62 Branchbran 58.3% 14 / 24 Conditioncond 50.0% 3 / 6 MC/DCmcdc 25.0% 1 / 4 Subroutinesub 100% 12 / 12 Podpod 0.0% 0 / 7 totaltotal 79.1% 91 / 115 CC 15 SCAR 16.3
CC
15
Cov
85%
mktemp
9.5
new
5.8
mkstemp
3.0
CRAP 5.1
« MkTemp.pm ↑ Summary
1 #############################################################
2 # File/MkTempO.pm. Written in 1999|2000 by Travis Gummels.
3 # If you find problems with this please let me know.
4 # travis@gummels.com
5 #############################################################
6  
7 package File::MkTempO;
8  
9 1 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 ▶use FileHandle;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 1 $File::MkTemp::VERSION = '1.0.6';
17  
18 1 ▶sub new {
Subroutine new: called CC=5 SCAR=17.5
Pod undocumented
19 1 my $pkg = shift;
20  
21 1 ▶ croak("Usage: \$var = new File::MkTempO('templateXXXXXX',['dir']) ")
Branch: 1 branch
BranchTrueFalseTotal
unless @_ == 1 or @_ == 2 0 1 1
Condition33%1/3
@_ == 1 or @_ == 2
l!l&&r!l&&!r
010
MC/DC0%0/2
@_ == 1 or @_ == 2
@_ == 1 @_ == 2
Truth table33%1/3
A or B
A= @_ == 1B= @_ == 2
ABresult
1X1
011
000
22 unless(@_ == 1 || @_ == 2);
23  
24 1 ▶ croak("The template must end with at least 6 uppercase letter X")
Branch: 1 branch
BranchTrueFalseTotal
if substr($_[0], -6, 6) ne "XXXXXX" 0 1 1
25 if (substr($_[0], -6, 6) ne 'XXXXXX');
26  
27 1 my $me = bless {}, $pkg;
28 1 $me->{'template'} = $_[0];
29 1 ▶ $me->{'dir'} = $_[1] if @_ == 2;
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 2 1 0 1
30 1 $me;
31 }
32  
33 2 ▶sub mktemp {
Subroutine mktemp: called CC=9 SCAR=22.6
Pod undocumented
34 2 my $me = shift;
35 2 my ($template,$dir,$keepgen,$lookup);
36 2 my (@template,@letters);
37  
38 2 $template = $me->{'template'};
39 2 $dir = $me->{'dir'};
40  
41 2 @template = split //, $template;
42  
43 2 ▶ if ($dir){
Branch: 1 branch
BranchTrueFalseTotal
if ($dir) 2 0 2
44 2 ▶ croak("The directory in which you wish to test for duplicates, $dir, does not exist") unless (-e $dir);
Branch: 1 branch
BranchTrueFalseTotal
unless -e $dir 0 2 2
45 }
46  
47 2 @letters = split(//,"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ");
48  
49 2 $keepgen = 1;
50  
51 2 ▶ while ($keepgen){
Branch: 1 branch
BranchTrueFalseTotal
if ($keepgen) 2 2 4
52  
53 2 ▶ for (my $i = $#template; $i >= 0 && ($template[$i] eq 'X'); $i--){
Branch: 1 branch
BranchTrueFalseTotal
if ($i >= 0 and $template[$i] eq "X") 12 2 14
Condition66%2/3
$i >= 0 and $template[$i] eq "X"
!ll&&!rl&&r
0212
MC/DC50%1/2
$i >= 0 and $template[$i] eq "X"
$i >= 0 $template[$i] eq "X"
Truth table66%2/3
A and B
A= $i >= 0B= $template[$i] eq "X"
ABresult
0X0
100
111
54 12 $template[$i] = $letters[int(rand 52)];
55 }
56  
57 2 $template = pack "a" x @template, @template;
58  
59 2 ▶ if ($me->{'dir'}){
Branch: 1 branch
BranchTrueFalseTotal
if ($me->{'dir'}) { } 2 0 2
60 2 $lookup = File::Spec->catfile($dir, $template);
61 2 ▶ $keepgen = 0 unless (-e $lookup);
Branch: 1 branch
BranchTrueFalseTotal
unless -e $lookup 2 0 2
62 }else{
63 0 $keepgen = 0;
64 }
65  
66 2 ▶ next if $keepgen == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $keepgen == 0 2 0 2
67  
68 }
69  
70 2 return($template);
71 }
72  
73 1 ▶sub mkstemp {
Subroutine mkstemp: called CC=3 SCAR=11.1
Pod undocumented
74  
75 1 my $me = shift;
76  
77 1 my $dir = $me->{'dir'};
78  
79 1 ▶ croak("You must specify a directory when creating the object in order to use mkstemp")
Branch: 1 branch
BranchTrueFalseTotal
unless $dir 0 1 1
80 unless $dir;
81  
82 1 my $template = $me->mktemp;
83  
84 1 my $openup = File::Spec->catfile($dir, $template);
85  
86 1 my $fh = new FileHandle ">$openup"; #and say ahhh.
87  
88 1 ▶ croak("Could not open file: $openup")
Branch: 1 branch
BranchTrueFalseTotal
unless defined $fh 0 1 1
89 unless(defined $fh);
90  
91 1 $me->{'fhtmpl'} = $template;
92 1 $me->{'fhdirtmpl'} = $openup;
93  
94 1 return($fh);
95 }
96  
97 2 ▶sub template { my $me=shift; return $me->{'template'}; }
Subroutine template: called CC=1 SCAR=0.0
Pod undocumented
98 2 ▶sub dir { my $me=shift; return $me->{'dir'}; }
Subroutine dir: called CC=1 SCAR=0.0
Pod undocumented
99 2 ▶sub fhtmpl { my $me=shift; return $me->{'fhtmpl'}; }
Subroutine fhtmpl: called CC=1 SCAR=0.0
Pod undocumented
100 3 ▶sub fhdirtmpl { my $me=shift; return $me->{'fhdirtmpl'}; }
Subroutine fhdirtmpl: called CC=1 SCAR=0.0
Pod undocumented
101  
102 1 1;
103  
104 __END__
« MkTemp.pm ↑ Summary