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