I've written a goodly number of utilities over the years and in almost every case the core code is usually written with the output sent to STDOUT. As a long time fan of the 'tools' philosopy of K&R and 'nix in general this usually is a good solution. Except...well yes there is always an 'except' isn't there. As long as you apply the STDOUT solution one at a time, the problems are few-- but, as soon as you need to apply this to multiple input, then things get a little bit (sometimes more than a 'little') strained. As I'm facing just such a problem with my own code I thought I'd try and find a better approach with a small piece of perlish slight of hand.

To wit:
#!/perl/bin/perl # # wrap.pl -- proof of concept for wrapping a function tied to STDOUT, +such that you can tie it to whatever... use strict; no strict 'refs'; use warnings; use diagnostics; use IO::Scalar; use SelectSaver; for (map {glob $_} @ARGV) { my $s; wrapper((new IO::Scalar \$s),\&myFunction,$_); print "'$s'\n"; } sub wrapper { my $saver = new SelectSaver(shift); &{$_[0]}($_[1]); } sub myFunction { my $filename = shift; print "\$filename = '$filename'\n"; print <<'HERE'; template [name = "KeyValue"] (keyword,indent,value){ Keyword(text = "concat($keyword,' =')",indent = "$indent") value-of("$value") text(";") Newline() } HERE }

–hsm

"Never try to teach a pig to sing…it wastes your time and it annoys the pig."

In reply to Getting a Handle on the un-rulely STDOUT by hsmyers

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.