Java 中与 Perl 的菱形运算符 <> 等价的是什么?

发布于 2024-12-21 19:18:40 字数 459 浏览 3 评论 0 原文

我对 Java 非常陌生(这个周末刚刚开始)并且有 Perl 背景。所以我有一个问题:

Java 相当于 Perl Diamond 运算符 (<>)?

那么基本上 Java 相当于什么:

print "What is your name?";
my $name = <>;
print "Hello, $name!";

提前致谢!


对于那些不了解 Perl 的人:

正如所使用的,<>readline(ARGV) 的缩写,其中 ARGV 是一个特殊的文件句柄它表示 STDIN(如果没有参数传递给程序)或作为参数传递给程序的文件内容的串联。 readline 不会删除行终止符。

I am extremely new to Java (just started this weekend) and have a background in Perl. So I have a question:

What is the Java equivalent to the Perl Diamond Operator (<>)?

So basically what is Java's equivalent to:

print "What is your name?";
my $name = <>;
print "Hello, $name!";

Thanks in advance!


For those not knowledgeable of Perl:

As used, <> is short for readline(ARGV), where ARGV is a special file handle that represents either STDIN (if no arguments were passed to the program) or the concatenation of the content of the files passed as arguments to the program. readline does not remove the line terminator.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

陌路终见情 2024-12-28 19:18:40

这将是 new BufferedReader(new InputStreamReader(System.in)) (或 new Scanner(System.in),具体取决于您的需要)。

但请注意,它并不严格等同于perl 的菱形运算符,因为菱形运算符受$/ 的影响。在 Java 中,您必须以不同的方式执行此操作。

为了完整起见,System.in 是一个InputStream(而且也恰好是 stdin)。

That would be new BufferedReader(new InputStreamReader(System.in)) (or new Scanner(System.in), depending on your needs).

Note however that it is not strictly equivalent to perl's diamond operator, since the diamond operator is influenced by $/. In Java, you'd have to do that differently.

And just for completeness, System.in is an InputStream (and also happens to be stdin).

时光倒影 2024-12-28 19:18:40

嗯,java 不是一种脚本语言,它是为不同的事情而设计的。

无论如何,java 中的类似代码可能如下所示:

public class Readline {
    public static void main(String[] args) throws java.io.IOException {
        System.out.print("What is your name?");
        java.io.BufferedReader stdin = new java.io.BufferedReader(new java.io.InputStreamReader(System.in));
        String name = stdin.readLine();
        System.out.println("Hello %s\n".format(name));
    }
}

Well, java is not a scripting language and it's designed for different things.

Anyhow, similar code in java might look like this:

public class Readline {
    public static void main(String[] args) throws java.io.IOException {
        System.out.print("What is your name?");
        java.io.BufferedReader stdin = new java.io.BufferedReader(new java.io.InputStreamReader(System.in));
        String name = stdin.readLine();
        System.out.println("Hello %s\n".format(name));
    }
}
花开柳相依 2024-12-28 19:18:40

我怀疑 Java 是否有一个相当于 <> 的功能不仅仅是读取STDIN

空文件句柄<>很特殊:它可以用来模拟 sed 和 awk 的行为。 <> 的输入要么来自标准输入,要么来自命令行上列出的每个文件。它的工作原理如下:第一次计算 <> 时,@ARGV 数组被检查,如果它为空,则 $ARGV[0] 设置为“-”,打开时为您提供标准输入。然后,@ARGV 数组将作为文件名列表进行处理。循环

while (<>) {
  ... # 每行代码
  }

相当于以下类似 Perl 的伪代码:

 unshift(@ARGV, '-') 除非 @ARGV;
  而($ARGV = 移位){
  打开(ARGV,$ARGV);
     而 () {
     ... # 每行代码
     }
  }

只不过说起来并不那么麻烦,并且实际上会起作用。它确实会移动 @ARGV 数组并将当前文件名放入 $ARGV 变量。它还在内部使用文件句柄 ARGV<> 只是 的同义词,很神奇。 (上面的伪代码不起作用,因为它将 视为非魔法。)

由于空文件句柄使用 open 的两个参数形式,因此它会解释特殊字符,因此如果您有这样的脚本:

while (<>) {
   打印;
}

并使用perlanger.pl'rm -rfv *|'调用它,它实际上打开一个管道,执行rm命令并读取rm< /code> 该管道的输出。如果您希望将 @ARGV 中的所有项目解释为文件名,可以使用模块 ARGV::readonly 来自 CPAN。

您可以在第一个 <> 之前修改 @ARGV,只要数组最终包含您真正想要的文件名列表。行号 ($.) 继续,就像输入是一个很大的快乐文件。请参阅 eof 中的示例,了解如何重置每个文件的行号。

I doubt there is a Java equivalent as <> does more than just read from STDIN:

The null filehandle <> is special: it can be used to emulate the behavior of sed and awk. Input from <> comes either from standard input, or from each file listed on the command line. Here's how it works: the first time <> is evaluated, the @ARGV array is checked, and if it is empty, $ARGV[0] is set to "-", which when opened gives you standard input. The @ARGV array is then processed as a list of filenames. The loop

while (<>) {
  ... # code for each line
  }

is equivalent to the following Perl-like pseudo code:

  unshift(@ARGV, '-') unless @ARGV;
  while ($ARGV = shift) {
  open(ARGV, $ARGV);
     while (<ARGV>) {
     ... # code for each line
     }
  }

except that it isn't so cumbersome to say, and will actually work. It really does shift the @ARGV array and put the current filename into the $ARGV variable. It also uses filehandle ARGV internally. <> is just a synonym for <ARGV>, which is magical. (The pseudo code above doesn't work because it treats <ARGV> as non-magical.)

Since the null filehandle uses the two argument form of open it interprets special characters, so if you have a script like this:

while (<>) {
   print;
}

and call it with perl dangerous.pl 'rm -rfv *|', it actually opens a pipe, executes the rm command and reads rm's output from that pipe. If you want all items in @ARGV to be interpreted as file names, you can use the module ARGV::readonly from CPAN.

You can modify @ARGV before the first <> as long as the array ends up containing the list of filenames you really want. Line numbers ($.) continue as though the input were one big happy file. See the example in eof for how to reset line numbers on each file.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文