in reply to Hash only portion of a file

Another option is to local $/ = \1_048_576; to read the first 1MB:

use strict; use warnings; use Digest::MD5 qw/md5_hex/; my $md5_hex_1m = file_md5_hex_1m('File.txt'); print $md5_hex_1m; sub file_md5_hex_1m { my ( $file, $contents ) = @_; local $/ = \1_048_576; open my $fh, '<', $file or die $!; return Digest::MD5->new()->add($contents)->hexdigest if defined( $contents = <$fh> ); }

Replies are listed 'Best First'.
Re^2: Hash only portion of a file
by Anonymous Monk on Jan 19, 2014 at 06:46 UTC
    Can you please explain this? I was under the impression $/ only matches a character or number of characters, how does it match 1 million counts of characters?

      That's the way it works. Please see  $/ in perlvar. (AKA  $INPUT_RECORD_SEPARATOR and  $RS. See also
          perldoc -v "$/"
      from the (update: Windoze) command line — use single-quotes in *nix (update: but see choroba's reply below).)

        As $/ is not a valid identifier name in shell (at least bash), you can use double quotes, too, as well as no quotes at all.
        لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
      "Setting $/ to a reference to an integer, scalar containing an integer, or scalar that's convertible to an integer will attempt to read records instead of lines, with the maximum record size being the referenced integer number of characters"

      This is really cool. I also didn't know it, because i didn't read the whole manpage ;-(

      Best regards, Karl

      «The Crux of the Biscuit is the Apostrophe»