blib/lib/Tie/Handle/Scalar.pm

filter: Statementstmt 23.2% 20 / 86 Branchbran 0.0% 0 / 22 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 35.7% 5 / 14 Podpod n/a totaltotal 20.4% 25 / 122 CC 12 SCAR 21.9
CC
12
Cov
19%
TIEHANDLE
20.0
READ
12.0
CLOSE
12.0
CRAP 8.9
↑ Summary
1 package Tie::Handle::Scalar;
2 1 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use FileHandle;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use vars qw(@ISA @EXPORT @EXPORT_OK $VERSION $AUTOLOAD $FILEHANDLE);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 require Exporter;
10  
11 1 @ISA = qw(Exporter);
12  
13 1 @EXPORT = qw();
14  
15 1 @EXPORT_OK = qw();
16  
17 1 $VERSION = "0.1";
18  
19  
20 0 ▶sub TIEHANDLE {
Subroutine TIEHANDLE: not called CC=4 SCAR=30.0
21 0 my $class = bless {}, shift;
22
23 0 my ($stringref) = @_;
24
25 0 ▶ if (! defined($stringref)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $stringref) 0 0 0
26 0 my $temp_s = '';
27 0 $stringref = \$temp_s;
28 }
29
30 0 ▶ if (ref($stringref) ne "SCALAR") {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $stringref ne "SCALAR") 0 0 0
31 0 croak "need a reference to a scalar,";
32 }
33
34 0 $class->{position} = 0;
35 0 $class->{data} = $stringref;
36 0 $class->{end} = 0;
37 0 my $tmpfile = $class->{tmpfile} = '.tmp.' . $$;
38 0 ▶ $FILEHANDLE = new FileHandle "$tmpfile", O_RDWR|O_CREAT or croak "$tmpfile: $!";
Branch: 1 branch
BranchTrueFalseTotal
$Tie::Handle::Scalar::FILEHANDLE = "FileHandle"->new("$tmpfile", 66) or croak("${tmpfile}: $!") 0 0 0
39 0 $class->{FILENO} = $FILEHANDLE->fileno();
40 0 $class;
41 }
42  
43 0 ▶sub FILENO {
Subroutine FILENO: not called CC=1 SCAR=6.9
44 0 my $class = shift;
45 0 return $class->{FILENO};
46 }
47  
48 0 ▶sub WRITE {
Subroutine WRITE: not called CC=2 SCAR=17.9
49 0 my $class = shift;
50 0 my($buf,$len,$offset) = @_;
51 0 ▶ $offset = 0 if (! defined $offset);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $offset 0 0 0
52 0 my $data = substr($buf, $offset, $len);
53 0 my $n = length($data);
54 0 $class->print($data);
55 0 return $n;
56 }
57  
58 0 ▶sub PRINT {
Subroutine PRINT: not called CC=1 SCAR=6.9
59 0 my $class = shift;
60 0 ${$class->{data}} .= join('', @_);
61 0 $class->{position} = length(${$class->{data}});
62 0 1;
63 }
64  
65 0 ▶sub PRINTF {
Subroutine PRINTF: not called CC=1 SCAR=6.9
66 0 my $class = shift;
67 0 my $fmt = shift;
68 0 $class->PRINT(sprintf $fmt, @_);
69 }
70  
71 0 ▶sub READ {
Subroutine READ: not called CC=3 SCAR=24.8
72 0 my $class = shift;
73
74 0 my ($buf,$len,$offset) = @_;
75 0 ▶ $offset = 0 if (! defined $offset);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $offset 0 0 0
76
77 0 my $data = ${ $class->{data} };
78
79 0 ▶ if ($class->{end} >= length($data)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class->{"end"} >= length $data) 0 0 0
80 0 return 0;
81 }
82 0 $buf = substr($data,$offset,$len);
83 0 $_[0] = $buf;
84 0 $class->{end} += length($buf);
85 0 return length($buf);
86 }
87  
88 0 ▶sub READLINE {
Subroutine READLINE: not called CC=2 SCAR=17.9
89 0 my $class = shift;
90 0 ▶ if ($class->{end} >= length(${ $class->{data} })) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class->{"end"} >= length ${$class->{"data"};}) 0 0 0
91 0 return undef;
92 }
93 0 my $recsep = $/;
94 0 my $rod = substr(${ $class->{data} }, $class->{end}, -1);
95 0 $rod =~ m/^(.*)$recsep{0,1}/; # use 0,1 for line sep to include possible no \n on last line
96 0 my $line = $1 . $recsep;
97 0 $class->{end} += length($line);
98 0 return $line;
99 }
100  
101 0 ▶sub CLOSE {
Subroutine CLOSE: not called CC=3 SCAR=24.8
102 0 my $class = shift;
103 0 ▶ if (-e $class->{tmpfile}) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $class->{"tmpfile"}) 0 0 0
104 0 $FILEHANDLE->close();
105 0 ▶ unlink $class->{tmpfile} or warn $!;
Branch: 1 branch
BranchTrueFalseTotal
unlink $class->{"tmpfile"} or warn $! 0 0 0
106 }
107 0 $class = undef;
108 0 1;
109 }
110  
111 0 ▶sub DESTROY {
Subroutine DESTROY: not called CC=3 SCAR=24.8
112 0 my $class = shift;
113 0 ▶ if (-e $class->{tmpfile}) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $class->{"tmpfile"}) 0 0 0
114 0 ▶ unlink $class->{tmpfile} or warn $!;
Branch: 1 branch
BranchTrueFalseTotal
unlink $class->{"tmpfile"} or warn $! 0 0 0
115 }
116 0 $class = undef;
117 0 1;undef $class;
118 }
119  
120 1 1;
121 __END__
↑ Summary