gpt4 book ai didi

perl 子程序参数列表 - "pass by alias"?

转载 作者:行者123 更新时间:2023-12-04 14:46:34 28 4
gpt4 key购买 nike

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

my $line;
$rc = getline($line); # read next line and store in $line

我一直都明白 Perl 参数是按值传递的,所以每当我需要传入一个大型结构,或者传入一个要更新的变量时,我都会传递一个引用。

但是,阅读 perldoc 中的细则后,我了解到 @_ 由参数列表中提到的变量的别名组成。读取下一位数据后, getline() 返回 $_[0] = $数据; , 存储 $数据直接进入 $行 .

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

最佳答案

你可以,它不是很漂亮:

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

关于perl 子程序参数列表 - "pass by alias"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16783628/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com