in reply to Stream processor?
Or maybe something more succinct:#!/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 perhaps this is "cleaner" for your taste:#!/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; }
#!/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 }
|
|---|