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); }

In reply to Can I abstract these blocks into a function call? by dwhite20899

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.