File Coverage

blib/lib/File/Details.pm
Criterion Covered Total %
statement 9 9 100.0
branch n/a
condition n/a
subroutine 3 3 100.0
pod n/a
total 12 12 100.0


line stmt bran cond sub pod time code
1             package File::Details;
2              
3 2     2   1246 use strict;
  2         5  
  2         68  
4 2     2   8 use warnings;
  2         3  
  2         65  
5              
6 2     2   22 use base qw/Class::Accessor/;
  2         2  
  2         971  
7              
8             use Cwd 'abs_path';
9              
10             my @stats = qw/dev ino mode nlink uid gid rdev size accessed
11             modified changed blksize blocks/;
12              
13             my @details = qw/abspath filename hashtype/;
14              
15             my @hashtypes = qw/MD5/;
16              
17             File::Details->mk_accessors( @stats );
18              
19             sub new{
20             my ( $class, $filename, $options ) = @_;
21              
22             my $self = {
23             filename => $filename,
24             abspath => abs_path($filename),
25             };
26              
27             for my $digest ( @hashtypes ) {
28             no strict 'refs';
29             $self->{ _hash_dispatch }{ $digest } = &$digest();
30             }
31              
32             if ( -e $filename ){
33             _read_attribs( $self );
34             return bless $self, $class;
35             }else{
36             die "File $filename does not exists\n";
37             }
38             }
39              
40             sub _read_attribs {
41             my ( $self ) = @_;
42              
43             @$self{ @stats } = stat( $self->{ filename } );
44             }
45              
46             sub hash {
47             my ( $self ) = @_;
48              
49             return $self->{ hash } if exists %$self->{ hash };
50              
51             my $type = $self->{ hashtype } || "MD5";
52              
53             $self->{ _hash_dispatch }{ $type }( $self );
54             }
55              
56             # plugin or something?
57             sub MD5 {
58             eval "require Digest::MD5";
59              
60             if ( !$@ ) {
61             Digest::MD5->import();
62             }
63              
64             return sub {
65             my ( $self ) = @_;
66             open my $fh , "<", $self->{ filename };
67             my $hash = Digest::MD5->new;
68             $hash->addfile( $fh );
69             close $fh;
70             return $hash->hexdigest;
71             }
72             }
73              
74              
75             1;
76              
77             =head1 NAME
78              
79             File::Details - File details in an object, stat, hash, etc..
80              
81             =head1 SYNOPSIS
82              
83             This module provides a class, File::Details that returns an
84             object with stats and optionally other information about some file.
85              
86             Instead creating hashs or even other classes to represent this, we
87             simple can use File::Details. It also works like an stat that returns
88             an object.
89              
90             use File::Details;
91              
92             my $details = File::Details->new( "filename" );
93              
94             my $size = $detais->size(); # same output as stat("filename")[7]
95              
96             # Getting the MD5 sum ( needs Digest::MD5 installed )
97              
98             my $hash = $details->hash();
99              
100             =head1 METHODS
101              
102             =head2 new
103              
104             Creates a new object, on the filename received as parameter. It generates the stat info.
105              
106             =head2 dev ino mode etc...
107              
108             The stat elements are the same, so:
109              
110             $details->blocks();
111              
112             returns the actual number of system-specific blocks allocated on disk.
113              
114             =head2 hash
115              
116             Returns the MD5sum from the contents of the file. It calculates when the hash method is called for the first time. Another calls will return the same value calculated on first time.
117              
118             =head1 AUTHOR
119              
120             RECSKY, C<< >>
121              
122             =head1 BUGS
123              
124             Please report any bugs or feature requests to C, or through the web interface at L.
125              
126             =head1 SUPPORT
127              
128             Usually we are on irc on irc.perl.org
129              
130             #sao-paulo.pm
131             #perl
132              
133             =head1 LICENSE AND COPYRIGHT
134              
135             Copyright 2015 RECSKY
136              
137             This program is free software; you can redistribute it and/or modify
138             it under the terms of the the Artistic License (2.0). You may obtain a
139             copy of the full license at:
140              
141             L
142              
143             =cut
144              
145              
146             __END__