#! /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] 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 }