dwhite20899 has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to build a flexible script that returns various SHA algorithms and string types.

I have it to the point where I am repeating very similar blocks of code, and I can't see a way to abstract them into a clean function call. I've gone down a couple of dead ends, and am just banging my head against a wall. Does anyone have any insights to share?

The full script is in my public scratchpad , but here are two examples of blocks I wish I could turn into one abstract function call:

# # There has to be a way to abstract these do_xxx blocks... # if ($do_sha1) { if (! open(FILE,"$fileName")) { print STDERR "$0 : cannot open file + \"$fileName\"\n"; return(0); } binmode FILE ; my $s1 = Digest::SHA->new(1) ; my $sha1; if ($family{sha1}) { $s1->addfile(*FILE); $sha1 = $s1->digest; my $ps = unpack("B*",$sha1); while (length($ps) < 160) { $ps = "0$ps"; } print "sha1($fileName)= $ps\n"; seek(FILE,0,0); } # The block above seeks back to start-of-file, # because addfile(*FILE) needs to reread the data, # but that's horrible! # I don't know if I can slurp it all into a variable, though... if ($family{sha1_hex}) { $s1->addfile(*FILE); $sha1 = lc($s1->hexdigest); if ($opt_u) { $sha1 = uc($sha1); } print "sha1_hex($fileName)= $sha1\n"; seek(FILE,0,0); } if ($family{sha1_b64}) { $s1->addfile(*FILE); $sha1 = $s1->b64digest; while (length($sha1) % 4) { $sha1 .= '='; } print "sha1_b64($fileName)= $sha1\n"; } close(FILE); } if ($do_256) { if (! open(FILE,"$fileName")) { print STDERR "$0 : cannot open file + \"$fileName\"\n"; return(0); } binmode FILE ; my $s256 = Digest::SHA->new(256) ; my $sha256; if ($family{sha256}) { $s256->addfile(*FILE); $sha256 = $s256->digest; my $ps = unpack("B*",$sha256); while (length($ps) < 160) { $ps = "0$ps"; } print "sha1($fileName)= $ps\n"; seek(FILE,0,0); } if ($family{sha256_hex}) { $s256->addfile(*FILE); $sha256 = lc($s256->hexdigest); if ($opt_u) { $sha256 = uc($sha256); } print "sha256_hex($fileName)= $sha256\n"; seek(FILE,0,0); } if ($family{sha256_b64}) { $s256->addfile(*FILE); $sha256 = $s256->b64digest; while (length($sha256) % 4) { $sha256 .= '='; } print "sha256_b64($fileName)= $sha256\n"; } close(FILE); }

Replies are listed 'Best First'.
Re: Can I abstract these blocks into a function call?
by almut (Canon) on Mar 26, 2010 at 20:28 UTC

    The main difference between the two blocks seems to be the use of $s1 / $sha1 vs. $s256 / $sha256, and some places where you need the substring "1" vs. "256" as part of something else.

    So why not use two variables named $s / $sha (instead of the four you have now), and pass the SHA type (1, 256,...) to the routine, assigning it to another variable, e.g. $sha_type, which you can then interpolate as required?

Re: Can I abstract these blocks into a function call?
by GrandFather (Saint) on Mar 27, 2010 at 01:40 UTC

    I'd roll it into a sub and pass in the mode etc:

    sub calcDigest { my ($fileName, $mode, $opt_u, %family) = @_; my $inFile; if (!open ($inFile, '<', $fileName)) { print STDERR "$0 : cannot open file \"$fileName\"\n"; return 0; } binmode $inFile; my $dObj = Digest::SHA->new ($mode); my $digest; if ($family{"sha$mode"}) { $dObj->addfile ($inFile); $digest = $dObj->digest; my $ps = unpack ("B*", $digest); while (length ($ps) < 160) {$ps = "0$ps";} print "sha$mode($fileName)= $ps\n"; seek (FILE, 0, 0); } if ($family{"sha${mode}_hex"}) { $dObj->addfile ($inFile); $digest = lc ($dObj->hexdigest); if ($opt_u) {$digest = uc ($digest);} print "sha${mode}_hex($fileName)= $digest\n"; seek (FILE, 0, 0); } if ($family{"sha${mode}_b64"}) { $dObj->addfile ($inFile); $digest = $dObj->b64digest; while (length ($digest) % 4) {$digest .= '=';} print "sha${mode}_b64($fileName)= $digest\n"; } close ($inFile); }

    Note that the original code printed sha1($fileName)= for the sha256 case in the $do_256 branch. I assumed that was copy and paste error and 'corrected' it.

    Update: type globs removed per jwkrahn's reply (doh!).


    True laziness is hard work
      my $inFile; ... if (!open ($inFile, '<', $fileName)) { ... if ($family{"sha$mode"}) { $dObj->addfile (*$inFile); ... if ($family{"sha${mode}_hex"}) { $dObj->addfile (*$inFile); ... if ($family{"sha${mode}_b64"}) { $dObj->addfile (*$inFile); ...

      You can't use a typeglob with a lexical variable.    Remove the '*' from in front of $inFile.

      That's what I was thinking of, and I just couldn't pull it together - sweet! Now I have to get rid of the seeks, and not reread the file every time.