blib/lib/File/fgets.pm

filter: Statementstmt 100% 42 / 42 Branchbran 100% 16 / 16 Conditioncond 66.6% 2 / 3 MC/DCmcdc 50.0% 1 / 2 Subroutinesub 100% 8 / 8 Podpod 50.0% 1 / 2 totaltotal 95.8% 70 / 73 CC 11 SCAR 15.6
CC
11
Cov
98%
fgets
8.0
perl_fgets
4.0
BEGIN
1.0
CRAP 4.8
↑ Summary lib/File/fgets.xs »
1 package File::fgets;
2  
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 3 ▶use version; our $VERSION = qv("v0.0.4");
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 3 ▶use XSLoader;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 XSLoader::load __PACKAGE__, $VERSION;
10  
11 3 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 our @EXPORT = qw(fgets);
13  
14 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16  
17 =head1 NAME
18  
19 File::fgets - Read either one line or X characters from a file
20  
21 =head1 SYNOPSIS
22  
23 use File::fgets;
24  
25 open my $fh, $file;
26  
27 # Read either one line or the first 10 characters, which ever comes first
28 my $line = fgets($fh, 10);
29  
30 =head1 DESCRIPTION
31  
32 An implementation of the C fgets() function.
33  
34 =head3 fgets
35  
36 my $string = fgets($fh, $limit);
37  
38 Reads either one line or at most $limit bytes from the $fh.
39  
40 Returns undef at end of file.
41  
42 NOTE: unlike C's fgets, this will read $limit characters not $limit -
43 1. Perl doesn't have to leave room for a null byte.
44  
45 =cut
46  
47 135 ▶sub fgets {
Subroutine fgets: called CC=8 SCAR=20.8
Pod documented
48 135 my($fh, $limit) = @_;
49  
50 135 ▶ croak "Invalid filehandle supplied to fgets()" unless defined $fh;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $fh 1 134 135
51 134 ▶ croak "No limit supplied to fgets()" unless defined $limit;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $limit 1 133 134
52 133 ▶ croak "fgets() on closed filehandle" if do { tell($fh) == -1; };
Branch: 1 branch
BranchTrueFalseTotal
if do { tell $fh == -1 } 1 132 133
53 132 ▶ return if eof $fh;
Branch: 1 branch
BranchTrueFalseTotal
if eof $fh 1 131 132
54  
55 # fgets() is often buggy, returning garbage or silently reading
56 # one character. Let's just not get it involved.
57 131 ▶ return "" if $limit == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $limit == 0 1 130 131
58  
59 130 my $fd = eval { fileno($fh) };
60 130 ▶ my $has_fd = $fd && $fd != -1;
Condition66%2/3
$fd && $fd != -1
!ll&&!rl&&r
02128
MC/DC50%1/2
$fd && $fd != -1
$fd $fd != -1
Truth table66%2/3
A && B
A= $fdB= $fd != -1
ABresult
0X0
100
111
61 130 ▶ return $has_fd ? xs_fgets($fh, $limit) : perl_fgets($fh, $limit);
Branch: 1 branch
BranchTrueFalseTotal
$has_fd ? : 128 2 130
62 }
63  
64 # For dealing with filehandles that aren't real file descriptors
65 2 ▶sub perl_fgets {
Subroutine perl_fgets: called CC=4 SCAR=13.9
Pod undocumented
66 2 my($fh, $limit) = @_;
67  
68 2 my $char; # avoid reallocating it every iteration
69 2 my $str = '';
70 2 for(1..$limit) {
71 8 $char = getc $fh;
72 8 ▶ last unless defined $char;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $char 1 7 8
73 7 $str .= $char;
74 7 ▶ last if $char eq "\n";
Branch: 1 branch
BranchTrueFalseTotal
if $char eq "\n" 1 6 7
75 }
76  
77 2 return $str;
78 }
79  
80 3 1;
81  
82 =head1 EXAMPLE
83  
84 The following example demonstrates using fgets() to read in at most 5
85 characters at a time.
86  
87 use File::fgets;
88  
89 open my $write_fh, ">", $file;
90 print $write_fh <<END;
91 this is
92 an example
93 of use
94 END
95 close $write_fh;
96  
97 open my $fh, "<", $file;
98 while( my $string = fgets($fh, 5) ) {
99 $string =~ s{\n}{\\n}; # make newlines show up
100 print "--$string--\n";
101 }
102  
103 The result will be:
104  
105 --this --
106 --is\n--
107 --an ex--
108 --ample--
109 --\n--
110 --of us--
111 --e\n--
112  
113  
114 =head1 NOTES
115  
116 This is implemented as a wrapper around the C fgets() function and is
117 extremely efficient UNLESS the filehandle does not have an underlying
118 fileno. For example, if its given a tied filehandle. Then it falls
119 back to a Perl implementation.
120  
121 =head1 LICENSE
122  
123 Copyright 2010 by Michael G Schwern E<lt>schwern@pobox.comE<gt>.
124  
125 This program is free software; you can redistribute it and/or
126 modify it under the same terms as Perl itself.
127  
128 See F<http://www.perl.com/perl/misc/Artistic.html>
129  
130 Send bugs, feedback, ideas and suggestions via
131 L<https://rt.cpan.org/Public/Dist/Display.html?Name=File-fgets> or
132 E<lt>bugs-File-fgets@rt.cpan.orgE<gt>
133  
134 The latest version of this software can be found at L<http://github.com/schwern/File-fgets>
135  
136 =head1 SEE ALSO
137  
138 L<File::GetLineMaxLength>
139  
140 =cut
↑ Summary lib/File/fgets.xs »