写这个是否正确?
sub foobar {
local $_ = $_[0] if @_;
s/foo/bar/;
$_;
}
的想法是采取$_
如果没有给出chomp
参数做。然后,我可以写
foobar($_);
或
&foobar;
写这个是否正确?
sub foobar {
local $_ = $_[0] if @_;
s/foo/bar/;
$_;
}
的想法是采取$_
如果没有给出chomp
参数做。然后,我可以写
foobar($_);
或
&foobar;
这是不正确的,没有。麻烦的是,你不能有条件地local
- 它是本地化的,或者不是。
而是这样做的,我的建议是您本地化它,然后有条件地复制从@_
local $_ = $_;
$_ = shift if @_;
这样,$_
总是局部的,但只能有条件地从第一位置参数复制如果存在的话。
我必须问 - 你究竟想在这里完成什么?
它看起来像你想要一个像一些'builtins'像chomp
一样工作的子。我会建议这是不好的做法。
$_
会产生非常奇怪的后果。问题:$_
的范围是什么?
答案:“它很复杂”,因为它是全球性的,但有时它是隐式地本地化的。有时它本身并不是一个变量,它是一个别名,通过修改它可以改变原来的。
这意味着从代码可维护性的角度来看,这只是一个坏消息。
来源:http://perldoc.perl.org/perlvar.html#General-Variables
$_ is by default a global variable. However, as of perl v5.10.0, you can use a lexical version of $_ by declaring it in a file or in a block with my. Moreover, declaring our $_ restores the global $_ in the current scope. Though this seemed like a good idea at the time it was introduced, lexical $_ actually causes more problems than it solves. If you call a function that expects to be passed information via $_ , it may or may not work, depending on how the function is written, there not being any easy way to solve this. Just avoid lexical $_ , unless you are feeling particularly masochistic. For this reason lexical $_ is still experimental and will produce a warning unless warnings have been disabled. As with other experimental features, the behavior of lexical $_ is subject to change without notice, including change into a fatal error.
如果你想通过使用一个外$ _的函数,你可以使用 “_” 原型:
# dolund.pl
#
use strict;
sub dolund (_)
{ my $p1 = $_[0];
print "passed parameter is $p1\n";
}
dolund 12; # prints 12
my $fred = 21; # prints 21
dolund $fred;
$_ = 'not 12';
dolund; # prints "not 12"
Obiously,你可以如果你喜欢,请使用$p1=~ s/foo/bar/;
。我只是想证明$_
的隐含传球。
local $_ = ... if @_;
只会本地化$_
如果子接收到一个参数,这意味着它不会保护呼叫者的$_
是子没有收到参数,这不是你想要的。
最小的修复
sub sfoobar {
local $_ = @_ ? shift : $_;
s/foo/bar/;
return $_;
}
但你不妨在这一点上使用命名变量。
sub sfoobar {
my $s = @_ ? shift : $_;
$s =~ s/foo/bar/;
return $s;
}
5.10+介绍了_
原型。
sub sfoobar(_) {
my ($s) = @_;
$s =~ s/foo/bar/;
return $s;
}
5.14+介绍s///r
。
sub sfoobar(_) {
return $_[0] =~ s/foo/bar/r;
}
缺少最终的'/'在替代。 – choroba 2014-11-03 09:52:46
谢谢,我忘了:) – nowox 2014-11-03 09:59:25
此外,使用[警告](http://p3rl.org/warnings)告诉你:'标量值@_ [0]更好写成$ _ [0]'。 – choroba 2014-11-03 10:07:05