diff options
author | Tom Ryder <tom@sanctum.geek.nz> | 2017-08-10 15:58:44 +1200 |
---|---|---|
committer | Tom Ryder <tom@sanctum.geek.nz> | 2017-08-10 15:58:44 +1200 |
commit | 1c3f274ae8f030a7bd0eee0c453e4577d034e4bf (patch) | |
tree | 8dc4d64432002adc27412287c77b07524e4e0bca /checkem | |
parent | Re-clarify module support (diff) | |
download | checkem-1c3f274ae8f030a7bd0eee0c453e4577d034e4bf.tar.gz checkem-1c3f274ae8f030a7bd0eee0c453e4577d034e4bf.zip |
Better approach to finding an algorithm
Diffstat (limited to 'checkem')
-rwxr-xr-x | checkem | 34 |
1 files changed, 18 insertions, 16 deletions
@@ -42,23 +42,25 @@ my %STATS = ( size => 7, ); -# Create a digest object, trying to safely detect ancient Perl (5.6) -# installations, or others that are bereft of Digest::SHA, in order -# to pick the algorithm. It can be foced with the CHECKEM_ALG environment -# variable. -my $dig = do { - my $alg; - if ( exists $ENV{CHECKEM_ALG} ) { - $alg = $ENV{CHECKEM_ALG}; - } - elsif ( eval { require Digest::SHA; } ) { - $alg = 'SHA-1'; - } - else { - $alg = 'MD5'; +# We need to pick and create a Digest object +my $dig; + +# We were told which algorithm to use +if ( exists $ENV{CHECKEM_ALG} ) { + $dig = Digest->new( $ENV{CHECKEM_ALG} ); +} + +# Try worse and worse algorithms until we get a digest object +else { + ALG: for my $alg (qw(SHA-256 SHA-1 MD5)) { + next ALG if not eval { $dig = Digest->new($alg); }; } - Digest->new($alg); -}; +} + +# Still no digest object, give up +if ( !defined $dig ) { + croak 'Could not create a useable Digest object'; +} # Start a hash of filesizes to file names/stats... my %sizes; |