in reply to Re^3: reading files to different output files.
in thread reading files to different output files.

yes, I tried it this way:

#! /usr/bin/perl use strict; use warnings; use IO::File; my @fh; my @file_names = ("output1.txt", "output2.txt", "output3.txt"); while ( <> ){ for (my $i=0; $i<=$#file_names; $i++){ $fh[$i]= IO::File->new( ">$file_names[$i]" ) || die "Cannot op +en $file_names[$i]: $!.\n"; print {$fh[$i]} "$_"; } } print "done.\n";

this script produces three output files, each contains the input filename that the user writes into the command line. Do I have to somehow open the input files in the script?

Replies are listed 'Best First'.
Re^5: reading files to different output files.
by marinersk (Priest) on May 28, 2017 at 14:05 UTC

    Do I have to somehow open the input files in the script?

    Yes.

    open my $input_file, '<', $input_filename or die "Cannot open input fi +le $input_filename: $!.\n"; while (my $input_line = <$input_file>) { chomp $input_line; print "[$input_line]\n"; } close $input_file;

      the input file should come from the command line. i don't want to determine the file within the script. Doesn't <> within the while loop open the files the users enters in the command line? If not, how can i implement the open command in my script?
        Doesn't <> within the while loop open the files the users enters in the command line?

        Yes, and there's more to it. Read about the null filehandle in I/O Operators, there's much to learn about @ARGV, $ARGV and ARGV, all three being "magical".

        perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'

        shmem's guidance is spot on, as usual. Be careful about command line filenames with wildcards, as these are handled differently in Windows vs. Linux.