| 1 | ############################################################# | ||||||||||||||||||||||||||||||||||||||||||
| 2 | # File/MkTemp.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::MkTemp; | ||||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||
| 9 | 1 | require Exporter; | |||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||
| 11 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||
| 13 | 1 | ▶ | use vars qw($VERSION @ISA @EXPORT); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||
| 15 | 1 | ▶ | use FileHandle; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 16 | 1 | ▶ | use File::Spec; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 17 | 1 | ▶ | use Carp; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||||||||
| 19 | 1 | @ISA=qw(Exporter); | |||||||||||||||||||||||||||||||||||||||||
| 20 | 1 | @EXPORT=qw(mktemp mkstemp mkstempt); | |||||||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||||||
| 22 | 1 | $VERSION = '1.0.6'; | |||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||||
| 24 | 2 | ▶ | sub mkstempt { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
mkstempt: called CC=4 SCAR=14.1
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 25 | 2 | my ($template,$openup,$fh); | |||||||||||||||||||||||||||||||||||||||||
| 26 | 2 | my @retval; | |||||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||||||
| 28 | 2 | ▶ | croak("Usage: mkstempt('templateXXXXXX','dir',['ext']) ") | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
@_ == 2 or @_ == 3
MC/DC0%0/2
@_ == 2 or @_ == 3
@_ == 2
@_ == 3
Truth table66%2/3
A or B
A= @_ == 2B= @_ == 3
| |||||||||||||||||||||||||||||||||||||||||||
| 29 | unless(@_ == 2 || @_ == 3); | ||||||||||||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||||||||||||
| 31 | 2 | $template = mktemp(@_); | |||||||||||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||||||||||||
| 33 | 2 | $openup = File::Spec->catfile($_[1], $template); | |||||||||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||||||||||
| 35 | 2 | $fh = new FileHandle ">$openup"; #and say ahhh. | |||||||||||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||||||||||||
| 37 | 2 | ▶ | croak("Could not open file: $openup") | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 38 | unless(defined $fh); | ||||||||||||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||||||||||||
| 40 | 2 | @retval = ($fh,$template); | |||||||||||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||||||||||
| 42 | 2 | return(@retval); | |||||||||||||||||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||||||||||||
| 45 | 2 | ▶ | sub mkstemp { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
mkstemp: called CC=4 SCAR=14.2
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 46 | 2 | my ($template,$openup,$fh); | |||||||||||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||||||||||
| 48 | 2 | ▶ | croak("Usage: mkstemp('templateXXXXXX','dir',['ext']) ") | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
@_ == 2 or @_ == 3
MC/DC0%0/2
@_ == 2 or @_ == 3
@_ == 2
@_ == 3
Truth table66%2/3
A or B
A= @_ == 2B= @_ == 3
| |||||||||||||||||||||||||||||||||||||||||||
| 49 | unless(@_ == 2 || @_ == 3); | ||||||||||||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||||||||||||
| 51 | 2 | $template = mktemp(@_); | |||||||||||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||||||||||
| 53 | 2 | $openup = File::Spec->catfile($_[1], $template); | |||||||||||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||||||||||||
| 55 | 2 | $fh = new FileHandle ">$openup"; #and say ahhh. | |||||||||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||||||||||
| 57 | 2 | ▶ | croak("Could not open file: $openup") | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 58 | unless(defined $fh); | ||||||||||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||||||||||
| 60 | 2 | return($fh); | |||||||||||||||||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||||||||||||
| 63 | 6 | ▶ | sub mktemp { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
mktemp: called CC=14 SCAR=27.7
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 64 | 6 | my ($template,$dir,$ext,$keepgen,$lookup); | |||||||||||||||||||||||||||||||||||||||||
| 65 | 6 | my (@template,@letters); | |||||||||||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||||||||||||
| 67 | 6 | ▶ | croak("Usage: mktemp('templateXXXXXX',['/dir'],['ext']) ") | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%4/6
@_ == 1 or @_ == 2 or @_ == 3
@_ == 1 or @_ == 2
MC/DC0%0/3
@_ == 1 or @_ == 2 or @_ == 3
@_ == 1
@_ == 2
@_ == 3
Truth table50%2/4
A or B or C
A= @_ == 1B= @_ == 2C= @_ == 3
| |||||||||||||||||||||||||||||||||||||||||||
| 68 | unless(@_ == 1 || @_ == 2 || @_ == 3); | ||||||||||||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||||||||||||
| 70 | 6 | ($template,$dir,$ext) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 71 | 6 | @template = split //, $template; | |||||||||||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||||||||||||
| 73 | 6 | ▶ | croak("The template must end with at least 6 uppercase letter X") | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 74 | if (substr($template, -6, 6) ne 'XXXXXX'); | ||||||||||||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||||||||||||
| 76 | 6 | ▶ | if ($dir){ | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 77 | 6 | ▶ | croak("The directory in which you wish to test for duplicates, $dir, does not exist") unless (-e $dir); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||||||||||||
| 80 | 6 | @letters = split(//,"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"); | |||||||||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||||||||||
| 82 | 6 | $keepgen = 1; | |||||||||||||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||||||||||||||
| 84 | 6 | ▶ | while ($keepgen){ | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 85 | 6 | ▶ | for (my $i = $#template; $i >= 0 && ($template[$i] eq 'X'); $i--){ | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$i >= 0 and $template[$i] eq "X"
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"
| |||||||||||||||||||||||||||||||||||||||||||
| 86 | 36 | $template[$i] = $letters[int(rand 52)]; | |||||||||||||||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||||||||||||
| 89 | 6 | undef $template; | |||||||||||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||||||||||||
| 91 | 6 | $template = pack "a" x @template, @template; | |||||||||||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||||||||||||
| 93 | 6 | ▶ | $template = $template . $ext if ($ext); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||||||||||||
| 95 | 6 | ▶ | if ($dir){ | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 96 | 6 | $lookup = File::Spec->catfile($dir, $template); | |||||||||||||||||||||||||||||||||||||||||
| 97 | 6 | ▶ | $keepgen = 0 unless (-e $lookup); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 98 | }else{ | ||||||||||||||||||||||||||||||||||||||||||
| 99 | 0 | $keepgen = 0; | |||||||||||||||||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||||||||||||
| 102 | 6 | ▶ | next if $keepgen == 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||||||||||||
| 105 | 6 | return($template); | |||||||||||||||||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||||||||||||||||
| 107 | 1 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||||||||||||
| 109 | __END__ | ||||||||||||||||||||||||||||||||||||||||||