本文介绍了使Perl球解密(*)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题中,发帖人询问如何在一行中执行以下操作:

In this question the poster asked how to do the following in one line:

sub my_sub {
    my $ref_array = shift;
    my @array = @$ref_array;
}

根据我对Perl基本魔术的了解,我可以通过简单地使用类似的东西来避免这种情况:

which with my knowledge of the basic Perl magic I would avoid by simply using something like:

sub my_sub {
    my $ref_array = shift;
    for (@$ref_array) {
      #do somthing with $_ here
    };

    #use $ref_array->[$element] here
}

但是在此答案 SO的一位本地僧侣tchrist建议:

However in this answer one of SO's local monks tchrist suggested:

sub my_sub {
  local *array = shift();
  #use @array here
}

我问

尽管他确实提供了不错的参考,但有人建议我将其作为新帖子问.无论如何,这里去了吗?有人可以解释一下分配给什么的东西以及为什么创建@array而不是%array或$ array吗?谢谢.

I was suggested to ask it as a new post, though he did give nice references. Anyway, here goes? Can someone please explain what gets assigned to what and how come @array gets created rather than perhaps %array or $array? Thanks.

推荐答案

分配给全局对象

*glob = VALUE

包含一些魔法,具体取决于VALUE的类型(即返回值,例如Scalar::Util::reftype(VALUE)).如果VALUE是对标量,数组,哈希或子例程的引用,则该符号表中的条目将被覆盖.

contains some magic that depends on the type of VALUE (i.e., return value of, say, Scalar::Util::reftype(VALUE)). If VALUE is a reference to a scalar, array, hash, or subroutine, then only that entry in the symbol table will be overwritten.

这个习语

local *array = shift();
#use @array here

当子例程的第一个参数是数组引用时,

会按照记录的方式工作.如果第一个参数是标量引用,则只有$array而不是@array会受到赋值的影响.

works as documented when the first argument to the subroutine is an array reference. If the first argument was instead, say, a scalar reference, then only $array and not @array would be affected by the assignment.

一些演示脚本,以了解发生了什么事情

A little demo script to see what is going on:

no strict;

sub F {
  local *array = shift;

  print "\@array = @array\n";
  print "\$array = $array\n";
  print "\%array = ",%array,"\n";
  print "------------------\n";
}

$array = "original scalar";
%array = ("original" => "hash");
@array = ("orignal","array");

$foo = "foo";
@foo = ("foo","bar");
%foo = ("FOO" => "foo");

F ["new","array"];        # array reference
F \"new scalar";          # scalar reference
F {"new" => "hash"};      # hash reference
F *foo;                   # typeglob
F 'foo';                  # not a reference, but name of assigned variable
F 'something else';       # not a reference
F ();                     # undef


输出:


Output:


@array = new array
$array = original scalar
%array = originalhash
------------------
@array = orignal array
$array = new scalar
%array = originalhash
------------------
@array = orignal array
$array = original scalar
%array = newhash
------------------
@array = foo bar
$array = foo
%array = FOOfoo
------------------
@array = foo bar
$array = foo
%array = FOOfoo
------------------
@array =
$array =
%array =
------------------
@array = orignal array
$array = original scalar
%array = originalhash
------------------

位于 perlmod perldata .早在引用成为Perl的一部分之前,这种惯用法就有助于将数组和哈希传递给子例程.

Additional doc at perlmod and perldata. Back in the days before references were a part of Perl, this idiom was helpful for passing arrays and hashes into subroutines.

这篇关于使Perl球解密(*)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 04:41