File Coverage

blib/lib/File/Details.pm
Criterion Covered Total %
statement 38 39 97.4
branch 3 6 50.0
condition 1 2 50.0
subroutine 10 10 100.0
pod 2 3 66.6
total 54 60 90.0


line stmt bran cond sub pod time code
1             package File::Details;
2              
3 3     3   1453 use strict;
  3         4  
  3         83  
4 3     3   9 use warnings;
  3         3  
  3         75  
5              
6 3     3   15 use base qw/Class::Accessor/;
  3         3  
  3         1311  
7              
8 3     3   3894 use Cwd 'abs_path';
  3         4  
  3         318  
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, @details ) );
18              
19             sub new{
20 3     3 1 987 my ( $class, $filename, $options ) = @_;
21              
22 3         108 my $self = {
23             filename => $filename,
24             abspath => abs_path($filename),
25             };
26              
27 3         6 for my $digest ( @hashtypes ) {
28 3     3   13 no strict 'refs';
  3         3  
  3         676  
29 3         7 $self->{ _hash_dispatch }{ $digest } = &$digest();
30             }
31              
32 3 50       42 if ( -e $filename ){
33 3         5 _read_attribs( $self );
34 3         12 return bless $self, $class;
35             }else{
36 0         0 die "File $filename does not exists\n";
37             }
38             }
39              
40             sub _read_attribs {
41 3     3   4 my ( $self ) = @_;
42              
43 3         78 @$self{ @stats } = stat( $self->{ filename } );
44             }
45              
46             sub hash {
47 1     1 1 6 my ( $self ) = @_;
48              
49 1 50       7 return $self->{ hash } if exists $self->{ hash };
50              
51 1   50     9 my $type = $self->{ hashtype } || "MD5";
52              
53 1         2 $self->{ _hash_dispatch }{ $type }( $self );
54             }
55              
56             # plugin or something?
57             sub MD5 {
58 3     3 0 174 eval "require Digest::MD5";
59              
60 3 50       15 if ( !$@ ) {
61 3         47 Digest::MD5->import();
62             }
63              
64             return sub {
65 1     1   2 my ( $self ) = @_;
66 1         24 open my $fh , "<", $self->{ filename };
67 1         5 my $hash = Digest::MD5->new;
68 1         13 $hash->addfile( $fh );
69 1         5 close $fh;
70 1         9 return $hash->hexdigest;
71             }
72 3         16 }
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__