使用全局变量通常非常糟糕,并指向设计错误。在这种情况下,错误似乎是你不知道如何将参数传递给子。
这里是在Perl的模式:
sub I_take_arguments {
# all my arguments are in @_ array
my ($firstarg, $secondarg, @rest) = @_;
say "1st argument: $firstarg";
say "2nd argument: " .($firstarg+1). " (incremented)";
say "The rest is: [@rest]";
}
替补调用像
I_take_arguments(1, 2, "three", 4);
(不要不调用它们作为&nameOfTheSub
,这使得使用非常特殊的行为,你不通常想要。)
这将打印
1st argument: 1
2nd argument: 3
The rest is: [three 4]
子程序可以返回值,或者与return
语句或作为最后一条语句的值执行。这些潜艇是等价的:
sub foo {return "return value"}
sub bar {"return value"}
我会写你的getfileattributes
作为
sub getFileAttributes {
my ($name) = @_;
return
-r $name ? "readable" : "not readable",
-w $name ? "writable" : "not writable",
-x $name ? "executable" : "not executable";
}
这到底是怎么回事?我拿一个参数$name
,然后返回一个列表的值。 return
关键字可以省略。 return
需要一个值列表并且不需要parens,所以我将它们排除在外。 TEST ? TRUE-STATEMENT : FALSE-STATEMENT
运算符从其他语言中已知。
然后,在你的循环,子会被调用像
for my $filename (@ARGV) {
my ($r, $w, $x) = getFileAttributes($filename);
say "The file $filename is $r, $w and $x";
}
或
foreach my $file (@ARGV) {
my @attributes = getFileAttributes($file);
printf "The file $file is %s, %s and %s\n", @attributes;
}
注:
say
就像print
,但增加了一个换行符最后。要使用它,你必须有一个Perl> 5.10,你应该use 5.010
或任何版本或use feature qw(say)
。
总是use strict; use warnings;
除非你知道的更好。
通常,您可以编写程序而不必将变量分配给两次(单一赋值形式)。这可以使关于控制流的推理更容易。这就是全局变量(但不是全局常量)不好的原因。
总是使用'use strict;使用警告;'!它会立即告诉你你的错误是什么。 – ikegami
使用全局变量不是*如何简化事情 – friedo
全局变量不会“简化事情”。他们正好相反。当您在尽可能最小的范围内声明变量时,更容易知道它们来自哪里,他们如何到达那里以及下一个要去的地方。 –