# Eine moderne objektorientierte File-API in Perl5 # Zugriff auf Dateiattribute like stat() # Zugriff auf Dateiinhalt package File; use strict; use warnings; use IO::File; #use Carp qw(carp croak confess); our @ISA = qw(IO::File); our @EXPORT = @IO::File::EXPORT; sub new{ my $class = shift; my $filename = shift; my $open_mode = shift || O_RDONLY|O_BINARY; eval{ my $self = bless{ filename => $filename, FH => IO::File->new, O_MODE => $open_mode }, $class; # Prüfen ob O_CREAT gesetzt ist # wenn ja wird die Datei hier geöffnet if( $open_mode >> 8 & 1 ){ $self->{FH}->open($filename, $open_mode) or die "$!\n"; } my @stat = stat($filename) ? stat($filename) : (); my %h = (); @h{qw( dev ino mode nlink uid gid rdev size atime mtime ctime blksize blocks )} = @stat; $h{filename} = $filename; $self->{stat} = \%h; $self; }; } sub stat{ my $self = shift; return $self->{stat}; } sub tell{ my $self = shift; $self->_open unless $self->{FH}->fileno; $self->{FH}->tell(@_); } sub seek{ my $self = shift; my $offs = shift; $self->_open unless $self->{FH}->fileno; $self->{FH}->seek($offs, SEEK_SET); } sub truncate{ my $self = shift; $self->_open unless $self->{FH}->fileno; $self->{FH}->truncate(@_); } sub print{ my $self = shift; my $content = shift; $self->_open unless $self->{FH}->fileno; $self->{FH}->print($content); } sub string{ my $self = shift; $self->_open unless $self->{FH}->fileno; $self->{FH}->seek(0,0); read( $self->{FH}, my $bin, -s $self->{FH}); return $bin; } sub read{ my $self = shift; $self->_open unless $self->{FH}->fileno; $self->{FH}->seek(0,0); my $size = shift || $self->size; read( $self->{FH}, my $bin, $size); return $bin; } sub flock{ my $self = shift; my $mode = shift; $self->_open unless $self->{FH}->fileno; return flock($self->{FH}, $mode); } sub _open{ my $self = shift; $self->{FH}->open($self->{filename}, $self->{O_MODE}) or die "$!\n"; } # getter sub AUTOLOAD{ my $self = shift; return do{ our $AUTOLOAD =~ /(\w+)$/; $self->{stat}{$1} || ''; }; } sub DESTROY{ my $self = shift; $self->{FH}->close; } ############################################################################# 1;########################################################################### __END__ =head1 NAME File - A modern FileAPI written in Perl 5 =head1 SYNOPSIS use File; # Constructor # OPEN MODE per Default: O_RDONLY|O_BINARY my $f = File->new( "/tmp/a.txt" ) || die $@; # Custom OPEN MODE $f = File->new("foo", O_CREAT|O_RDWR) or die $@; # Access Attributes named like stat() print scalar localtime $f->mtime; # Sun Aug 5 14:05:13 2018 print $f->size; # 6 # stat Attributes as HashRef $stat = $f->stat; print Dumper $stat; # This will prints all attributes like this $VAR1 = { 'atime' => 1571561139, 'blksize' => '', 'blocks' => '', 'ctime' => 1533470713, 'dev' => 3, 'filename' => 'd:/tmp/a.txt', 'gid' => 0, 'ino' => 0, 'mode' => 33206, 'mtime' => 1571561139, 'nlink' => 1, 'rdev' => 3, 'size' => '6', 'uid' => 0 }; # Not all fields are supported on all filesystem types! # truncate like IO::File $f->truncate(0) || die $!; # seek like IO::File $f->seek(0,0); # this will prints a new content in file $f->print("123") or die $!; # print out the filecontent print $f->string; print $f->read; # LOCK_EX $f->flock or warn $! =head1 DESCRIPTION File is a simple interface for files that allows access to all file attributes named in the perl function stats(). Furthermore, this API delegates methods to the IO::File class. These are the methods print(), truncate(), tell() and seek(). The API method string() returns the file content. AUTOLOAD allows access to the attributes as virtual methods, such as ctime(), mtime(), and so on. =head2 EXPORT All Constants from IO::File, ie. O_CREAT, O_RDWR etc. =head1 SEE ALSO stat(), IO::File =head1 AUTHOR Rolf Rost =head1 COPYRIGHT AND LICENSE Copyright (C) 2019 by Rolf Rost This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.16.3 or, at your option, any later version of Perl 5 you may have available. =cut