简体   繁体   中英

perl: reading from a file and/or stdin without using while(<>)

For cases where one wishes to avoid the useful while(<>) syntax and manage file input manually, how does one handle the operation of reading from a list of files and/or STDIN ? To read from files, one can simply iterate through @ARGV , opening each element in turn (eg, my $file=shift @ARGV; open(my $fh,'<',$file); while(<$fh>) {...}; close($fh); . To read from standard input, one can simply use while(<STDIN>) { ...} . However, assuming the programmer expects similar types of data to be provided through STDIN and file arguments, the body of code within each while loop would have to be duplicated. I have tried unsuccessfully to assign STDIN to a filehandle (eg, my $fh = \\*STDIN or my $fh = *STDIN{IO} , each of which I have seen suggested elsewhere on this website). In essence, I would like to iterate through all files as well as STDIN and treat the input from each identically, but without using the handy while(<>) syntax. Could you please sketch a solution to this problem? Thank you.

With two-arg open (like <> uses), you could do

@ARGS = '-' if !@ARGV;

for my $qfn (@ARGV) {
    open($fh, $qfn);

    while (<$fh>) {
       ...
    }
}

Which three-arg open , I might do

@ARGV = \*STDIN if !@ARGV;

for my $qfn (@ARGV) {
    my $fh;
    if (ref($qfn)) {
       $fh = $qfn;
    } else {
       open($fh, '<', $qfn);
    }

    while (<$fh>) {
       ...
    }
}

Another way (if you can use CPAN modules) is to use IO::All .

Read a file into a scalar variable:

my $content1 < io('file1');

Another way to do it with IO::ALL:

my $content2 = io('file1')->slurp;

Or if you want it in an array, with each line as an element:

my @lines = io('file1')->slurp;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM