What is the magic behind perl read() function and buffer which is not a ref?

£可爱£侵袭症+ 提交于 2019-12-08 19:34:26

问题


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

回答1:


No magic is needed -- all perl subroutines are call-by-alias, if you will. Quoth perlsub:

The array @_ is a local array, but its elements are aliases for the actual scalar parameters. In particular, if an element $_[0] is updated, the corresponding argument is updated (or an error occurs if it is not updatable).

For example:

sub increment {
  $_[0] += 1;
}

my $i = 0;
increment($i);  # now $i == 1

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 to read(). Pass in $_[0] instead of copying, and see what happens:

sub read_this {
  $fh->read($_[0]);  # will modify caller's variable
}
sub read_that {
  $fh->read(shift);  # so will this...
}



回答2:


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:

sub myread(\$) { ... }

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.



来源:https://stackoverflow.com/questions/3011653/what-is-the-magic-behind-perl-read-function-and-buffer-which-is-not-a-ref

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!