简体   繁体   English

以编程方式从STDIN读取或在Perl中输入文件

[英]Programmatically read from STDIN or input file in Perl

在stl中以编程方式从stdin或输入文件(如果提供)中读取的最简单的方法是什么?

while (<>) {
print;
}

will read either from a file specified on the command line or from stdin if no file is given 将从命令行中指定的文件或stdin读取(如果没有给出文件)

If you are required this loop construction in command line, then you may use -n option: 如果在命令行中需要此循环构造,则可以使用-n选项:

$ perl -ne 'print;'

Here you just put code between {} from first example into '' in second 在这里,你只要把代码之间{}从第一到例如''在第二

This provides a named variable to work with: 这提供了一个命名变量来使用:

foreach my $line ( <STDIN> ) {
    chomp( $line );
    print "$line\n";
}

To read a file, pipe it in like this: 要读取文件,请将其管道输入:

program.pl < inputfile

The "slickest" way in certain situations is to take advantage of the -n switch . 在某些情况下,“最简单”的方法是利用-n开关 It implicitly wraps your code with a while(<>) loop and handles the input flexibly. 它隐式地用while(<>)循环包装你的代码并灵活地处理输入。

In slickestWay.pl : slickestWay.pl

#!/usr/bin/perl -n

BEGIN: {
  # do something once here
}

# implement logic for a single line of input
print $result;

At the command line: 在命令行:

chmod +x slickestWay.pl

Now, depending on your input do one of the following: 现在,根据您的输入,执行以下操作之一:

  1. Wait for user input 等待用户输入

     ./slickestWay.pl 
  2. Read from file(s) named in arguments (no redirection required) 从参数中命名的文件中读取(不需要重定向)

     ./slickestWay.pl input.txt ./slickestWay.pl input.txt moreInput.txt 
  3. Use a pipe 使用管道

     someOtherScript | ./slickestWay.pl 

The BEGIN block is necessary if you need to initialize some kind of object-oriented interface, such as Text::CSV or some such, which you can add to the shebang with -M . 如果你需要初始化某种面向对象的接口,比如Text :: CSV或其他一些,你可以使用-M添加到shebang, BEGIN块是必需的。

-l and -p are also your friends. -l-p也是你的朋友。

You need to use <> operator: 你需要使用<>运算符:

while (<>) {
    print $_; # or simply "print;"
}

Which can be compacted to: 哪个可以压缩到:

print while (<>);

Arbitrary file: 任意档案:

open F, "<file.txt" or die $!;
while (<F>) {
    print $_;
}
close F;

If there is a reason you can't use the simple solution provided by ennuikiller above, then you will have to use Typeglobs to manipulate file handles. 如果有一个原因你不能使用上面ennuikiller提供的简单解决方案,那么你将不得不使用Typeglobs来操作文件句柄。 This is way more work. 这是更多的工作。 This example copies from the file in $ARGV[0] to that in $ARGV[1] . 此示例从$ARGV[0]的文件复制到$ARGV[1] It defaults to STDIN and STDOUT respectively if files are not specified. 如果未指定文件,则分别默认为STDINSTDOUT

use English;

my $in;
my $out;

if ($#ARGV >= 0){
    unless (open($in,  "<", $ARGV[0])){
      die "could not open $ARGV[0] for reading.";
    }
}
else {
    $in  = *STDIN;
}

if ($#ARGV >= 1){
    unless (open($out, ">", $ARGV[1])){
      die "could not open $ARGV[1] for writing.";
    }
}
else {
    $out  = *STDOUT;
}

while ($_ = <$in>){
    $out->print($_);
}

Do

$userinput =  <STDIN>; #read stdin and put it in $userinput
chomp ($userinput);    #cut the return / line feed character

if you want to read just one line 如果你只想读一行

Here is how I made a script that could take either command line inputs or have a text file redirected. 以下是我创建一个可以采用命令行输入或重定向文本文件的脚本。

if ($#ARGV < 1) {
    @ARGV = ();
    @ARGV = <>;
    chomp(@ARGV);
}


This will reassign the contents of the file to @ARGV, from there you just process @ARGV as if someone was including command line options. 这将把文件的内容重新分配给@ARGV,从那里你只需要处理@ARGV就像有人包含命令行选项一样。

WARNING 警告

If no file is redirected, the program will sit their idle because it is waiting for input from STDIN. 如果没有重定向文件,程序将处于空闲状态,因为它正在等待STDIN的输入。

I have not figured out a way to detect if a file is being redirected in yet to eliminate the STDIN issue. 我还没有找到一种方法来检测文件是否被重定向,以消除STDIN问题。

if(my $file = shift) { # if file is specified, read from that
  open(my $fh, '<', $file) or die($!);
  while(my $line = <$fh>) {
    print $line;
  }
}
else { # otherwise, read from STDIN
  print while(<>);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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