I have a bunch of scripts that process several batches of “things”, whatever those may be, where each batch may or may not contain processible “things”. F.ex, that might be a list of directories of which each may or may not contain files to process, or an email folder full of messages that may have one or more files attached (see attachments — mass-dumps file attachments from mail), or such.

Usually, I want progress indication in the form of

dir1: file2, file8, file9
dir3: file6
dir4: file1, file2, file3

The following snippet contains a function generator that does the job. Use it as in:

for( 1 .. 10 ) { my $print = make_printer( "dir$_" ); next if 0.3 > rand; for( 1 .. 10 ) { next if 0.3 > rand; $print->( "file$_" ); } }

If you are processing batches with very large items, don't forget to unbuffer the output handle so you see the intermediate updates immediately.

sub make_printer { my $hdr = shift; my $count; return bless sub { my $item = shift || do { print "\n" if $count; return; }; print( ( $count ? "," : "$hdr:") , " ", $item ); ++$count; }, 'PRINTER'; sub PRINTER::DESTROY { shift->() } }

In reply to Batch processing progress printer by Aristotle

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.