for( 1 .. 10 ) { my $print = make_printer( "dir$_" ); next if 0.3 > rand; for( 1 .. 10 ) { next if 0.3 > rand; $print->( "file$_" ); } } #### 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->() } }