in reply to Merging files
#! /usr/bin/perl use strict; use Getopt::Std; use Symbol; use vars qw(@handles $opt_f); getopts('f:'); if ($opt_f) { open (STDOUT, ">$opt_f") or die "Cannot write to $opt_f: $!"; } unless (@ARGV) { die "Usage: mergefile [-o<output_file>] file1 file2"; } foreach my $file (@ARGV) { my $fh = gensym; # Letting someone pipe from a cmd is useful enough to allow # having someone try to wipe out a file with ">". open ($fh, $file) or die "Cannot read from $file: $!"; push @handles, $fh; } while (@handles) { my $fh = shift @handles; if (defined( my $line = <$fh> )) { print $line; push @handles, $fh; } # else $fh goes out of scope here and autocloses }
|
---|
Replies are listed 'Best First'. | |
---|---|
RE: RE (tilly) 1: Merging files
by merlyn (Sage) on Aug 27, 2000 at 19:27 UTC | |
(jcwren) RE: (2) Merging files
by jcwren (Prior) on Aug 27, 2000 at 20:40 UTC |