blib/lib/File/Details.pm

filter: Statementstmt 97.7% 43 / 44 Branchbran 50.0% 3 / 6 Conditioncond 50.0% 1 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 100% 10 / 10 Podpod 66.6% 2 / 3 totaltotal 89.3% 59 / 66 CC 6 SCAR 6.8
CC
6
Cov
90%
hash
3.1
new
3.0
MD5
2.0
CRAP 2.0
↑ Summary
1 package File::Details;
2  
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 3 ▶use base qw/Class::Accessor/;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 3 ▶use Cwd 'abs_path';
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 3 my @stats = qw/dev ino mode nlink uid gid rdev size accessed
11 modified changed blksize blocks/;
12  
13 3 my @details = qw/abspath filename hashtype/;
14  
15 3 my @hashtypes = qw/MD5/;
16  
17 3 File::Details->mk_accessors(( @stats, @details ) );
18  
19 3 ▶sub new{
Subroutine new: called CC=3 SCAR=11.1
Pod documented
20 3 my ( $class, $filename, $options ) = @_;
21  
22 3 my $self = {
23 filename => $filename,
24 abspath => abs_path($filename),
25 };
26  
27 3 for my $digest ( @hashtypes ) {
28 3 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
29 3 $self->{ _hash_dispatch }{ $digest } = &$digest();
30 }
31  
32 3 ▶ if ( -e $filename ){
Branch: 1 branch
BranchTrueFalseTotal
if (-e $filename) { } 3 0 3
33 3 _read_attribs( $self );
34 3 return bless $self, $class;
35 }else{
36 0 die "File $filename does not exists\n";
37 }
38 }
39  
40 3 ▶sub _read_attribs {
Subroutine _read_attribs: called CC=1 SCAR=0.0
41 3 my ( $self ) = @_;
42  
43 3 @$self{ @stats } = stat( $self->{ filename } );
44 }
45  
46 1 ▶sub hash {
Subroutine hash: called CC=3 SCAR=11.4
Pod documented
47 1 my ( $self ) = @_;
48  
49 1 ▶ return $self->{ hash } if exists $self->{ hash };
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"hash"} 0 1 1
50  
51 1 ▶ my $type = $self->{ hashtype } || "MD5";
Condition50%1/2
$self->{'hashtype'} || 'MD5'
l!l
01
MC/DC0%0/1
$self->{'hashtype'} || 'MD5'
$self->{'hashtype'}
Truth table50%1/2
A
A= $self->{'hashtype'}
Aresult
11
00
52  
53 1 $self->{ _hash_dispatch }{ $type }( $self );
54 }
55  
56 # plugin or something?
57 3 ▶sub MD5 {
Subroutine MD5: called CC=2 SCAR=6.9
Pod undocumented
58 3 eval "require Digest::MD5";
59  
60 3 ▶ if ( !$@ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($@) 3 0 3
61 3 Digest::MD5->import();
62 }
63  
64 1 ▶ return sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
65 1 my ( $self ) = @_;
66 1 open my $fh , "<", $self->{ filename };
67 1 my $hash = Digest::MD5->new;
68 1 $hash->addfile( $fh );
69 1 close $fh;
70 1 return $hash->hexdigest;
71 }
72 3 }
73  
74  
75 3 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<< <recsky@cpan.org> >>
121  
122 =head1 BUGS
123  
124 Please report any bugs or feature requests to C<bug-file-details at rt.cpan.org>, or through the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=File-Details>.
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<http://www.perlfoundation.org/artistic_license_2_0>
142  
143 =cut
144  
145  
146 __END__
↑ Summary