Perl程序通过STDIN获取两个不同的输入

Perl程序通过STDIN获取两个不同的输入,perl,Perl,我正在尝试编写一个perl程序: print "Enter the filename:"; $filename = readline STDIN; print "Enter the string to be compared"; $string1 = readline STDIN; 为了一次性组合这两个输入,我做了以下工作: print "Enter the filename and string to be compared:"; my $i

我正在尝试编写一个perl程序:

    print "Enter the filename:";
    $filename = readline STDIN;

    print "Enter the string to be compared";
    $string1 = readline STDIN;
为了一次性组合这两个输入,我做了以下工作:

    print "Enter the filename and string to be compared:";
    my $input1 = readline STDIN; 
    my @name = split(' ',$input1); //split the input parameters by space
    $filename = $name[0];
    $string1 = $name[1];
    chomp $string1;
这是功能代码,我想知道是否有其他方法可以实现此逻辑的更优化版本

谢谢, DD

关于perl secret的内容呢

use warnings;
use strict;
print "Enter the filename and string to be compared:";
chomp( my @ar = (~~<>, ~~<> ) );
print @ar;        
使用警告;
严格使用;
打印“输入要比较的文件名和字符串:”;
chomp(my@ar=(~~,~~);
打印@ar;
关于perl secret的内容呢

use warnings;
use strict;
print "Enter the filename and string to be compared:";
chomp( my @ar = (~~<>, ~~<> ) );
print @ar;        
使用警告;
严格使用;
打印“输入要比较的文件名和字符串:”;
chomp(my@ar=(~~,~~);
打印@ar;

这是您得到的最大优化。在确定需要之前,不要费心手动优化代码。考虑到这是对IO的等待,这无关紧要,因为IO比您在代码中所做的任何事情都要慢

但如果你的意思是更简洁:

print“输入要比较的文件名和字符串:”;
chomp(我的($filename,$string)=拆分“”,);
这将使用
-这是神奇的文件句柄,并读取STDIN或命令行上指定的文件名。(工作原理类似于
grep
/
sed
/
awk


我们在一个标量上下文中在一个空格上拆分它,然后将拆分后的值传递给
$filename
$string
的列表赋值。然后将其咀嚼以剥离换行符

这差不多是你得到的最佳结果。在确定需要之前,不要费心手动优化代码。考虑到这是对IO的等待,这无关紧要,因为IO比您在代码中所做的任何事情都要慢

但如果你的意思是更简洁:

print“输入要比较的文件名和字符串:”;
chomp(我的($filename,$string)=拆分“”,);
这将使用
-这是神奇的文件句柄,并读取STDIN或命令行上指定的文件名。(工作原理类似于
grep
/
sed
/
awk


我们在一个标量上下文中在一个空格上拆分它,然后将拆分后的值传递给
$filename
$string
的列表赋值。然后将其咀嚼以剥离换行符

尝试将下面的代码与 `comand:perl your_script.pl-file_name'some_file'-string'some_string'

use Getopt::Long;

my ( $file_name, $string );
GetOptions(
'file_name=s' => \$file_name,
'string=s' => \$string,
) or die "Could not parse options";

if($file_name eq $string )
{
print "The file name is the same as the string\n";
}else{
print "Not a match\n";
}

尝试将下面的代码与 `comand:perl your_script.pl-file_name'some_file'-string'some_string'

use Getopt::Long;

my ( $file_name, $string );
GetOptions(
'file_name=s' => \$file_name,
'string=s' => \$string,
) or die "Could not parse options";

if($file_name eq $string )
{
print "The file name is the same as the string\n";
}else{
print "Not a match\n";
}

定义优化!我不建议您以这种方式进行优化。如果文件名包含空格,您将怎么做?逻辑会增加。看看@mkHun unswer。看看define优化!我不建议您以这种方式进行优化。如果文件名包含空格,您将怎么做?逻辑会增加。看看@mkHun unswer。尽管我很聪明,但我并不认为“perl secret”中的任何内容都应该在实际代码中使用,因为它是模糊的。
scalar()
”。
将不那么“机密”。//请注意,在返回EOF后读取文件句柄是不允许的,因此这种方法本身就有缺陷。虽然很聪明,但我并不认为“perl secret”中的任何内容都应该在实际代码中使用,因为它是模糊的。
scalar()
”。
将不那么“机密”。//请注意,在返回EOF后读取文件句柄是不允许的,因此这种方法本身就有缺陷。