珀尔 |使用 STDIN 进行输入
Perl 允许程序员接受来自用户的输入来执行操作。这使用户更容易提供自己的输入,而不仅仅是程序员作为硬编码输入提供的输入。然后可以使用 print()函数处理和打印此输入。
Perl 程序的输入可以通过使用Standard Input
。虽然没有必要将 STDIN 放在“钻石”或“宇宙飞船”运算符之间,即 <>。这样做是标准做法。 <>运算符也可用于写入文件。
Syntax: $x =
例子:
#!/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 用于告诉编译器执行下一行代码。但是,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