line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Test::MockCommand::ScalarReadline; |
2
|
1
|
|
|
1
|
|
29983
|
use strict; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
36
|
|
3
|
1
|
|
|
1
|
|
6
|
use warnings; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
26
|
|
4
|
1
|
|
|
1
|
|
5
|
use Carp qw(croak); |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
47
|
|
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
require Exporter; |
7
|
1
|
|
|
1
|
|
4
|
use vars qw(@ISA @EXPORT_OK); |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
496
|
|
8
|
|
|
|
|
|
|
@ISA = qw(Exporter); |
9
|
|
|
|
|
|
|
@EXPORT_OK = qw(scalar_readline); |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
sub scalar_readline { |
12
|
130
|
50
|
66
|
130
|
0
|
12336
|
croak "scalar context but no second parameter" unless wantarray() || @_ > 1; |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
# return empty array or undef if there's no data |
15
|
130
|
100
|
66
|
|
|
413
|
if ((!defined $_[0]) || $_[0] eq '') { |
16
|
11
|
50
|
|
|
|
20
|
return () if wantarray(); |
17
|
11
|
|
|
|
|
10
|
$_[1] = 0; |
18
|
11
|
|
|
|
|
19
|
return undef; |
19
|
|
|
|
|
|
|
} |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
# slurp mode: $/ = undef |
22
|
119
|
100
|
|
|
|
180
|
if (not defined $/) { |
23
|
2
|
100
|
|
|
|
5
|
return ($_[0]) if wantarray(); |
24
|
1
|
|
|
|
|
2
|
$_[1] = length $_[0]; |
25
|
1
|
|
|
|
|
2
|
return $_[0]; |
26
|
|
|
|
|
|
|
} |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
# record mode: $/ = \$record_size |
29
|
117
|
100
|
66
|
|
|
208
|
if (ref $/ eq 'SCALAR' && ${$/} > 0) { |
|
87
|
|
|
|
|
222
|
|
30
|
|
|
|
|
|
|
# scalar wanted: return the first record |
31
|
87
|
100
|
|
|
|
131
|
if (! wantarray()) { |
32
|
83
|
|
|
|
|
70
|
my $out = substr($_[0], 0, ${$/}); |
|
83
|
|
|
|
|
105
|
|
33
|
83
|
|
|
|
|
73
|
$_[1] = length $out; |
34
|
83
|
|
|
|
|
149
|
return $out; |
35
|
|
|
|
|
|
|
} |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
# list wanted: return all records |
38
|
4
|
|
|
|
|
7
|
my ($offset, $length, @out) = (0, length $_[0]); |
39
|
4
|
|
|
|
|
8
|
while ($offset < $length) { |
40
|
83
|
|
|
|
|
66
|
push @out, substr($_[0], $offset, ${$/}); |
|
83
|
|
|
|
|
116
|
|
41
|
83
|
|
|
|
|
538
|
$offset += ${$/}; |
|
83
|
|
|
|
|
137
|
|
42
|
|
|
|
|
|
|
} |
43
|
4
|
|
|
|
|
30
|
return @out; |
44
|
|
|
|
|
|
|
} |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
# paragraph mode: $/ = '' |
47
|
30
|
|
|
|
|
38
|
my $rs = $/; |
48
|
30
|
|
|
|
|
25
|
my $paras = 0; |
49
|
30
|
100
|
|
|
|
47
|
if ($rs eq '') { |
50
|
4
|
|
|
|
|
5
|
$rs = "\n\n"; |
51
|
4
|
|
|
|
|
4
|
$paras = 1; |
52
|
|
|
|
|
|
|
} |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
# regular or paragraph mode: scalar wanted |
55
|
30
|
100
|
|
|
|
47
|
if (! wantarray()) { |
56
|
24
|
|
|
|
|
30
|
my $found = index $_[0], $rs; |
57
|
24
|
100
|
|
|
|
30
|
if ($found < 0) { |
58
|
5
|
|
|
|
|
6
|
$_[1] = length $_[0]; |
59
|
5
|
|
|
|
|
14
|
return $_[0]; |
60
|
|
|
|
|
|
|
} |
61
|
|
|
|
|
|
|
else { |
62
|
19
|
|
|
|
|
20
|
$_[1] = $found + length $rs; |
63
|
19
|
100
|
|
|
|
30
|
if ($paras) { while (substr($_[0], $_[1], 1) eq "\n") { $_[1]++; } } |
|
2
|
|
|
|
|
8
|
|
|
1
|
|
|
|
|
2
|
|
64
|
19
|
|
|
|
|
49
|
return substr($_[0], 0, $found + length $rs); |
65
|
|
|
|
|
|
|
} |
66
|
|
|
|
|
|
|
} |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
# regular or paragraph mode: list wanted |
69
|
6
|
|
|
|
|
5
|
my @out; |
70
|
6
|
|
|
|
|
7
|
my $pos = 0; |
71
|
6
|
|
|
|
|
16
|
while ((my $found = index $_[0], $rs, $pos) >= 0) { |
72
|
19
|
|
|
|
|
18
|
my $next = $found + length $rs; |
73
|
19
|
|
|
|
|
29
|
push @out, substr($_[0], $pos, $next - $pos); |
74
|
19
|
100
|
|
|
|
28
|
if ($paras) { while (substr($_[0], $next, 1) eq "\n") { $next++; } } |
|
2
|
|
|
|
|
6
|
|
|
1
|
|
|
|
|
3
|
|
75
|
19
|
|
|
|
|
37
|
$pos = $next; |
76
|
|
|
|
|
|
|
} |
77
|
6
|
100
|
|
|
|
16
|
push @out, substr($_[0], $pos) if $pos < length $_[0]; |
78
|
|
|
|
|
|
|
|
79
|
6
|
|
|
|
|
19
|
return @out; |
80
|
|
|
|
|
|
|
} |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
1; |
83
|
|
|
|
|
|
|
__END__ |