📜  珀尔 |使用 STDIN 进行输入

📅  最后修改于: 2022-05-13 01:55:34.092000             🧑  作者: Mango

珀尔 |使用 STDIN 进行输入

Perl 允许程序员接受来自用户的输入来执行操作。这使用户更容易提供自己的输入,而不仅仅是程序员作为硬编码输入提供的输入。然后可以使用 print()函数处理和打印此输入。

Perl 程序的输入可以通过使用的键盘给出。这里,STDIN 代表Standard Input 。虽然没有必要将 STDIN 放在“钻石”或“宇宙飞船”运算符之间,即 <>。这样做是标准做法。 <>运算符也可用于写入文件。 也可用于标量和列表上下文。

例子:

#!/usr/bin/perl -w 
use strict;
use warnings;
  
print"Enter some text:";
my $string = ;
  
print "You entered $string as a String";

输入:

GeeksForGeeks

输出:

Enter some text: GeeksForGeeks
You Entered GeeksForGeeks
as a String

在上面的代码中,在给出 Input 后,需要按 ENTER。这个 ENTER 用于告诉编译器执行下一行代码。但是, 将按下的这个 ENTER 键作为给定输入的一部分,因此当我们打印该行时。在 Input 字符串之后将自动打印一个新行。为了避免这种情况,使用了函数chomp() 。此函数将删除添加到用户提供的 Input 末尾的字符。

例子:

#!/usr/bin/perl -w 
use strict;
use warnings;
  
print"Enter some text:";
my $string = ;
chomp $string;
  
print "You entered $string as a String";

输入:

GeeksForGeeks

输出:

Enter some text: GeeksForGeeks
You Entered GeeksForGeeks as a String