perl子例程参数列表 - "通过别名"?

Cha*_*hap 6 parameters perl arguments pass-by-reference subroutine

我只是难以置信地看着这个序列:

my $line;
$rc = getline($line); # read next line and store in $line
Run Code Online (Sandbox Code Playgroud)

我一直都知道Perl参数是通过值传递的,所以每当我需要传入一个大型结构,或者传入一个变量来进行更新时,我都通过了一个ref.

但是,在perldoc中读取精细打印,我已经知道@_由参数列表中提到的变量的别名组成.读取下一位数据后,getline()返回$ _ [0] = $ data; ,将$数据直接存储到$ line.

我喜欢这个 - 就像在C++中通过引用传递一样.但是,我还没有找到为$ _ [0]分配更有意义的名称的方法.有没有?

Joe*_*ger 7

你可以,它不是很漂亮:

use strict;
use warnings;

sub inc {
  # manipulate the local symbol table 
  # to refer to the alias by $name
  our $name; local *name = \$_[0];

  # $name is an alias to first argument
  $name++;
}

my $x = 1;
inc($x);
print $x; # 2
Run Code Online (Sandbox Code Playgroud)