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

In reply to Re: Can I abstract these blocks into a function call? by GrandFather
in thread 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.