通过使用参数运行子例程

时间:2019-02-15 04:43:14

标签: perl

我的代码:

SqlDependecy

我希望myp可以打印得到的参数。 但它说 在./arg行17的字符串中使用未初始化的值$ _。

2 个答案:

答案 0 :(得分:5)

Perl中子例程的参数在View.OnClickListener listener = new View.OnClickListener() { @Override public void onClick(View view) { String[] facts = { "Ants stretch when they wake up in the morning.", "Ostriches can run faster than horses.", "Olympic gold medals are actually made mostly of silver.", "You are born with 300 bones; by the time you are an adult you will have 206.", "It takes about 8 minutes for light from the Sun to reach Earth.", "Some bamboo plants can grow almost a meter in just one day.", "The state of Florida is bigger than England.", "Some penguins can leap 2-3 meters out of the water.", "On average, it takes 66 days to form a new habit.", "Mammoths still walked the earth when the Great Pyramid was being built." }; String fact = ""; for(int i=0;i<facts.length;i++) { fact+=facts[i]; } funFactTextView.setText(fact);//set the new string } }; buttonFact.setOnClickListener(listener);//button clicked updates new string 数组中传递。这与@_变量不同。

一个常见的习惯用法是在函数的第一行中“解压缩”这些参数,例如

$_

还可以将参数直接引用为sub example { my ($arg1, $arg2) = @_; print "$arg1 and $arg2"; } 的元素,例如就像@_一样,但这很难阅读,因此最好避免。

答案 1 :(得分:1)

我通常会做类似的事情:

my $first_arg = shift @_;
my $second_arg = shift @_;

您还可以使用其他响应的方法:

my ($first_arg, $second_arg) = @_;

但是要小心地说:

my $first_arg = @_;

因为您将获得传递给子例程的参数数量。

在引用$_时,您引用的是默认字符串变量,在这种情况下,您可能希望引用@_,如果要获取特定的参数,则必须说{{1} },如果这样做,请小心将数组传递给子例程:

$_[narg]

您将传递整个数组,因为它是参数列表,相反,您应该说:

some_sub(@myarray);