File Coverage

lib/Rex/FS/File.pm
Criterion Covered Total %
statement 47 52 90.3
branch 10 16 62.5
condition 5 9 55.5
subroutine 10 11 90.9
pod 6 6 100.0
total 78 94 82.9


line stmt bran cond sub pod time code
1             #
2             # (c) Jan Gehring
3             #
4              
5             =head1 NAME
6              
7             Rex::FS::File - File Class
8              
9             =head1 DESCRIPTION
10              
11             This is the File Class used by I and I.
12              
13             =head1 SYNOPSIS
14              
15             use Rex::Interface::File;
16             my $fh = Rex::Interface::File->create('Local');
17             $fh->open( '<', 'filename' );
18              
19             my $file = Rex::FS::File->new(fh => $fh);
20             $file->read($len);
21             $file->read_all;
22             $file->write($buf);
23             $file->close;
24              
25             =head1 CLASS METHODS
26              
27             =cut
28              
29             package Rex::FS::File;
30              
31 45     45   580 use v5.12.5;
  45         154  
32 45     45   214 use warnings;
  45         87  
  45         1156  
33 45     45   1772 use Rex::Interface::File;
  45         83  
  45         430  
34              
35             our $VERSION = '1.14.3'; # VERSION
36              
37 45     45   1939 use constant DEFAULT_READ_LEN => 64;
  45         133  
  45         33286  
38              
39             =head2 new
40              
41             This is the constructor. You need to set the filehandle which the object should work on
42             or pass a filename. If you pass a filehandle, it has to be a C
43             object
44              
45             my $fh = Rex::Interface::File->create('Local');
46             $fh->open( '<', 'filename' );
47              
48             my $file = Rex::FS::File->new(fh => $fh);
49              
50             Create a C object with a filename
51              
52             # open a local file in read mode
53             my $file = Rex::FS::File->new(
54             filename => 'filename',
55             mode => 'r', # or '<'
56             type => 'Local',
57             );
58              
59             # or shorter
60             my $file = Rex::FS::File->new( filename => 'filename' );
61              
62             # open a local file in write mode
63             my $file = Rex::FS::File->new(
64             filename => 'filename',
65             mode => 'w', # or '>'
66             );
67              
68             Allowed modes:
69              
70             < read
71             r read
72             > write
73             w write
74             >> append
75             a append
76              
77             For allowed C see documentation of L.
78              
79             =cut
80              
81             sub new {
82 189     189 1 868 my $that = shift;
83 189   33     1603 my $proto = ref($that) || $that;
84 189         907 my $self = {@_};
85              
86 189         5063 my %modes = (
87             'w' => '>',
88             'r' => '<',
89             'a' => '>>',
90             '<' => '<',
91             '>' => '>',
92             '>>' => '>>',
93             );
94              
95 189 100       940 if ( $self->{filename} ) {
96 5   100     37 $self->{mode} ||= '<';
97              
98 5   50     20 my $mode = $modes{ $self->{mode} } || '<';
99 5   50     65 $self->{fh} = Rex::Interface::File->create( $self->{type} || 'Local' );
100 5         16 $self->{fh}->open( $mode, $self->{filename} );
101             }
102              
103 189         973 bless( $self, $proto );
104              
105 189 50       2962 if ( ref $self->{fh} !~ m{Rex::Interface::File} ) {
106 0         0 die "Need an Rex::Interface::File object";
107             }
108              
109 189         1126 return $self;
110             }
111              
112             sub DESTROY {
113 189     189   7882 my ($self) = @_;
114 189 50       2754 if ( ref $self->{'fh'} =~ m/^Rex::Interface::File/ ) {
115 0 0       0 $self->close if ( $self->{'fh'}->{'fh'} );
116             }
117             else {
118 189 50       1082 $self->close if ( $self->{'fh'} );
119             }
120             }
121              
122             =head2 write($buf)
123              
124             Write $buf into the filehandle.
125              
126             $file->write("Hello World");
127              
128             =cut
129              
130             sub write {
131              
132 247     247 1 1858 my ( $self, @buf ) = @_;
133 247         445 my $fh = $self->{fh};
134              
135 247 100       536 if ( scalar(@buf) > 1 ) {
136 3         21 for my $line (@buf) {
137 12         31 $fh->write($line);
138 12         28 $fh->write($/);
139             }
140             }
141             else {
142 244         591 $fh->write( $buf[0] );
143             }
144             }
145              
146             =head2 seek($offset)
147              
148             Seek to the file position $offset.
149              
150             Set the file pointer to the 5th byte.
151              
152             $file->seek(5);
153              
154             =cut
155              
156             sub seek {
157 0     0 1 0 my ( $self, $offset ) = @_;
158              
159 0         0 my $fh = $self->{'fh'};
160 0         0 $fh->seek($offset);
161             }
162              
163             =head2 read($len)
164              
165             Read $len bytes out of the filehandle.
166              
167             my $content = $file->read(1024);
168              
169             =cut
170              
171             sub read {
172 757     757 1 1460 my ( $self, $len ) = @_;
173 757 50       1472 $len = DEFAULT_READ_LEN if ( !$len );
174              
175 757         1107 my $fh = $self->{'fh'};
176 757         1787 return $fh->read($len);
177             }
178              
179             =head2 read_all
180              
181             Read everything out of the filehandle.
182              
183             my $content = $file->read_all;
184              
185             =cut
186              
187             sub read_all {
188 143     143 1 3202 my ($self) = @_;
189              
190 143         494 my $all = '';
191 143         939 while ( my $in = $self->read() ) {
192 614         1799 $all .= $in;
193             }
194 143 100       859 if (wantarray) {
195 6         31 return split( /\n/, $all );
196             }
197 137         689 return $all;
198             }
199              
200             =head2 close
201              
202             Close the file.
203              
204             $file->close;
205              
206             =cut
207              
208             sub close {
209 372     372 1 1149 my ($self) = @_;
210 372         819 my $fh = $self->{'fh'};
211 372         1480 $fh->close;
212             }
213              
214             1;