Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在命令行上将值传递给Perl子例程参数?_Perl_Parameters_Subroutine - Fatal编程技术网

如何在命令行上将值传递给Perl子例程参数?

如何在命令行上将值传递给Perl子例程参数?,perl,parameters,subroutine,Perl,Parameters,Subroutine,我的test.pl脚本如下 #!C:\Perl\bin\perl.exe use strict; use warnings; sub printargs { print "@_\n"; } &printargs("hello", "world"); # Example prints "hello world" 如果我替换了printargs(“你好”,“世界”)带有打印($a,$b) 当我在命令行运行perl test.pl hello world时,如何将'hello'

我的test.pl脚本如下

#!C:\Perl\bin\perl.exe
use strict;
use warnings;


sub printargs
{
    print "@_\n";
}

&printargs("hello", "world"); # Example prints "hello world"
如果我替换了
printargs(“你好”,“世界”)带有
打印($a,$b)


当我在命令行运行perl test.pl hello world时,如何将'hello','world'传递给$a,$b,谢谢。

$ARGV[0]包含第一个参数,$ARGV[1]包含第二个参数,等等


$#ARGV是@ARGV数组最后一个元素的下标,因此命令行上的参数数为$#ARGV+1。

$ARGV[0]包含第一个参数,$ARGV[1]包含第二个参数,依此类推

$#ARGV是@ARGV数组最后一个元素的下标,因此命令行上的参数数为$#ARGV+1。

您想在Perl中访问“命令行参数”

基本上,Perl将实际脚本名称后面传递的字符串视为一个名为@ARGV的数组

以下是一个简短的教程:

只需搜索“perl命令行参数”了解更多信息。

您想访问perl中的“命令行参数”

基本上,Perl将实际脚本名称后面传递的字符串视为一个名为@ARGV的数组

以下是一个简短的教程:


只需搜索“perl命令行参数”了解更多信息。

命令行参数位于
@ARGV
数组中。只需将其传递给函数:

&print( @ARGV );

可能最好避免像
print
这样的名称-可能会与相同名称的内置函数混淆。

命令行参数位于
@ARGV
数组中。只需将其传递给函数:

&print( @ARGV );

可能最好避免使用像
print
这样的名称-可能会与相同名称的内置函数混淆。

基本上,正如其他人所说,@ARGV列表就是您问题的答案


现在,如果您想更进一步,为您的程序定义命令行选项,您还应该有一个loogat

基本上,正如其他人所说,@ARGV列表就是您问题的答案

现在,如果您想更进一步,为您的程序定义命令行选项,您还应该在中有一个loog at

Do read about in.

Do read about in.

这还将从命令行参数打印“hello world”,同时根据请求将数据传递给$a和$b

#!/usr/bin/perl -w

use warnings;
use strict;

sub printargs($$)
{
    print $_[0] . " " . $_[1] . "\n";
}

my($a,$b) = ($ARGV[0],$ARGV[1]);
printargs($a,$b);
这还将从命令行参数打印“hello world”,同时根据请求将数据传递给$a和$b

#!/usr/bin/perl -w

use warnings;
use strict;

sub printargs($$)
{
    print $_[0] . " " . $_[1] . "\n";
}

my($a,$b) = ($ARGV[0],$ARGV[1]);
printargs($a,$b);

非常感谢。修改印刷品,印刷品谢谢。将print修改为printarg您最好从
和printargs(…)
中省略
。您不需要它,并且在子调用前面加上
&
可能会产生您可能没有预料到的副作用。您最好从
&printargs(…)
中省略
&
。您不需要它,并且在子调用前面加上
&
可能会产生您可能没有预料到的副作用。您也可以使用@ARGV来获取元素数。例如:如果(@ARGV<2){die”用法:“.$0.\n”}您也可以使用@ARGV来获取元素的数量。例如:if(@ARGV<2){die”用法:“.$0.\n”;}