in reply to Re^2: Unable to write output to file taken as input from command line
in thread Unable to write output to file taken as input from command line

To collect the file names from the command line, replace

print "Enter file name:\n"; chomp(my $file = <STDIN>); open(DATA,$file);

with

chomp(my $infile = shift); open(DATA,$infile) or die "Could not open $infile\n";

Add

chomp (my $outfile = shift); open OUTPUT, '>', $outfile or die "Could not open $outfile\n";

before the final for loop. Replace

printf "%-4s: %20s, %-8s %6s\n",

with

printf OUTPUT "%-4s: %20s, %-8s %6s\n",
1 Peter 4:10

Replies are listed 'Best First'.
Re^4: Unable to write output to file taken as input from command line
by graff (Chancellor) on Jul 25, 2014 at 02:48 UTC
    You don't need to use chomp on values drawn from @ARGV. There's stuff in zing's code that doesn't make sense, but whatever the purpose may be, the basic template should be something like this:
    die "Usage: $0 infile outfile\n" unless ( @ARGV == 2 ); my $infile = shift; my $outfile = shift; open( INPUT, '<', $infile ) or die "$infile: $!\n"; open( OUTPUT, '>', $outfile ) or die "$outfile: $!\n"; while (<INPUT>) { # do stuff with $_; # print something to OUTPUT }