perl read() 函数和不是引用的缓冲区背后的魔力是什么?
我不明白 Perl read($buf) 函数如何修改 $buf 变量的内容。 $buf 不是引用,因此参数是通过 copy 给出的(来自我的 c/c++ 知识)。那么为什么 $buf 变量在调用者中被修改呢?
它是一个领带变量还是什么?关于 setbuf 的 C 文档对我来说也相当难以捉摸且不清楚
# Example 1
$buf=''; # It is a scalar, not a ref
$bytes = $fh->read($buf);
print $buf; # $buf was modified, what is the magic ?
# Example 2
sub read_it {
my $buf = shift;
return $fh->read($buf);
}
my $buf;
$bytes = read_it($buf);
print $buf; # As expected, this scope $buf was not modified
I do not get to understand how the Perl read($buf) function is able to modify the content of the $buf variable. $buf is not a reference, so the parameter is given by copy (from my c/c++ knowledge). So how come the $buf variable is modified in the caller ?
Is it a tie variable or something ? The C documentation about setbuf is also quite elusive and unclear to me
# Example 1
$buf=''; # It is a scalar, not a ref
$bytes = $fh->read($buf);
print $buf; # $buf was modified, what is the magic ?
# Example 2
sub read_it {
my $buf = shift;
return $fh->read($buf);
}
my $buf;
$bytes = read_it($buf);
print $buf; # As expected, this scope $buf was not modified
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不需要任何魔法——如果你愿意的话,所有 Perl 子例程都是通过别名调用的。 perlsub:
例如:
在“示例 2”中,您的
read_it
子将@_
的第一个元素复制到词法$buf< /code>,然后通过调用
read()
来“就地”修改该副本。传入$_[0]
而不是复制,看看会发生什么:No magic is needed -- all perl subroutines are call-by-alias, if you will. Quoth perlsub:
For example:
In your "Example 2", your
read_it
sub copies the first element of@_
to the lexical$buf
, which copy is then modified "in place" by the call toread()
. Pass in$_[0]
instead of copying, and see what happens:read()
是一个内置函数,因此可以发挥神奇作用。不过,您可以通过声明 函数原型,使用自己的函数完成类似的操作:参数声明
\$
表示参数作为引用隐式传递。内置
read
的唯一魔力是,即使间接调用或作为文件句柄方法调用,它也能工作,而这对常规函数不起作用。read()
is a built-in function, and so can do magic. You can accomplish something similar with your own functions, though, by declaring a function prototype:The argument declaration
\$
means that the argument is implicitly passed as a reference.The only magic in the built-in
read
is that it works even when called indirectly or as a filehandle method, which doesn't work for regular functions.