perl - %$var 是否取消引用 Perl 哈希?

标签 perl reference

我正在向一个子程序发送一个哈希值,然后用 my($arg_ref) = @_; 获取它

但究竟是什么%$arg_ref ?是 %$取消引用哈希?

最佳答案

$arg_ref是标量,因为它使用 $印记。据推测,它持有一个哈希引用。是的,%$arg_ref散列引用的尊重。另一种写法是%{$arg_ref} .这使得代码的意图更加清晰,尽管更加冗长。

引自 perldata(1) :

 Scalar values are always named with '$', even when referring
 to a scalar that is part of an array or a hash.  The '$'
 symbol works semantically like the English word "the" in
 that it indicates a single value is expected.

     $days               # the simple scalar value "days"
     $days[28]           # the 29th element of array @days
     $days{'Feb'}        # the 'Feb' value from hash %days
     $#days              # the last index of array @days

所以你的例子是:
     %$arg_ref           # hash dereferenced from the value "arg_ref"
my($arg_ref) = @_;获取函数参数堆栈中的第一项并将其放入名为 $arg_ref 的局部变量中.调用者负责传递散列引用。一种更规范的写法是:
my $arg_ref = shift;

要创建散列引用,您可以从散列开始:
some_sub(\%hash);

或者您可以使用匿名哈希引用创建它:
some_sub({pi => 3.14, C => 4}); # Pi is a gross approximation.

关于perl - %$var 是否取消引用 Perl 哈希?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/562798/

相关文章:

jquery - 将 jQuery ajax() 与 Perl 一起使用会导致成功时闪烁

perl - 无法使用导出 PERL5LIB 更新@INC

c++ - 关于使用返回的引用

Scala akka 输入 : how to get ActorRef to Actor from it's instance and send message itself?

Perl:命令行覆盖配置文件设置?

perl - 如何从使用 su -c 在管道中执行的命令中获取 Perl 中的 STDERR

algorithm - rand 在 perl 中的工作

c++ - 无法将此指针从 const Class<T> 转换为 Class<T>&

c++ - 通过模板化类中的成员函数返回不可修改的引用

rust - 为什么不能在同一结构中存储值和对该值的引用?