Is this along the lines of what you had in mind?
#!/usr/bin/perl use strict; use warnings; my $i = 0; my @out_files; foreach ( qw( op0 op1 op2 op3 ) ) { unless ( open $out_files[ $i ], '>', $_ ) { die "Cannot open $_ for writing: $!\n"; } $i++; } while ( <> ) { foreach my $out ( @out_files ) { print $out $_; } }
Or maybe something more succinct:
#!/usr/bin/perl use strict; use warnings; my $line; my %out = map { $_ => undef } qw( op0 op1 op2 op3 ); ( open $out{ $_ }, '>', $_ or die "can't: $!\n" ) for keys %out; while ( $line = <> ) { print $_ $line for values %out; }
Or perhaps this is "cleaner" for your taste:
#!/usr/bin/perl use strict; use warnings; my @out = qw( op0 op1 op2 op3 ); my %out = map { $_ => undef } @out; for ( @out ) { open $out{ $_ }, '>', $_ || die "Can't: $!\n" } while ( my $l = <> ) { print $_ $l for values %out }

In reply to Re: Stream processor? by mr_mischief
in thread Stream processor? by ursus

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.