揭秘 Perl glob (*) [英] Demystifying the Perl glob (*)

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

问题描述

这个问题中,海报询问了如何在一行中执行以下操作:

sub my_sub {我的 $ref_array = shift;我的@array = @$ref_array;}

根据我对基本 Perl 魔法的了解,我可以通过简单地使用以下内容来避免:

sub my_sub {我的 $ref_array = shift;对于(@$ref_array){#在这里用 $_ 做点什么};#在此处使用 $ref_array->[$element]}

但是在 这个答案中,SO 的一位当地僧侣 tchrist 建议:

sub my_sub {本地 *array = shift();#在这里使用@array}

当我问

<块引用>

在尝试学习中级 Perl魔法,我能问一下,你是什么在这里设置什么?你是将@array 的引用设置为已经传入的arrayref?如何你知道你创建了@array 和不是 %array 或 $array?我可以在哪里了解有关此 * 运算符的更多信息(perlop?).谢谢!

有人建议我将其作为新帖子提问,尽管他确实提供了很好的参考.无论如何,来了?有人可以解释什么被分配给什么以及如何创建@array 而不是 %array 或 $array?谢谢.

解决方案

分配给 glob

*glob = VALUE

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

这个成语

local *array = shift();#在这里使用@array

当子例程的第一个参数是数组引用时,按文档中的说明工作.如果第一个参数是标量引用,那么只有 $array 而不是 @array 会受到赋值的影响.

一个小演示脚本,看看发生了什么:

无严格;子F{本地*数组=移位;打印 "@array = @array
";打印 "$array = $array
";打印 "\%array = ",%array,"
";打印 "-------------------
";}$array = "原始标量";%array = ("原始" => "哈希");@array = ("原始","数组");$foo = "foo";@foo = ("foo","bar");%foo = ("FOO" => "foo");F ["新建","数组"];# 数组引用F "新标量";# 标量参考F {新" =>哈希"};# 哈希引用F *富;# typeglobF'富';# 不是引用,而是分配变量的名称F '别的东西';# 不是参考F ();# 未定义

<小时>

输出:

<前>@array = 新数组$array = 原始标量%array = 原始哈希------------------@array = 原始数组$array = 新标量%array = 原始哈希------------------@array = 原始数组$array = 原始标量%array = 新哈希------------------@array = foo 栏$数组 = foo%array = FOOfoo------------------@array = foo 栏$数组 = foo%array = FOOfoo------------------@数组 =$数组 =%数组 =------------------@array = 原始数组$array = 原始标量%array = 原始哈希------------------

附加文档位于 perlmodperldata.早在引用成为 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;
}

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
}

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

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

When I asked

In trying to learn the mid-level Perl magic, can I ask, what is it that you are setting to what here? Are you setting a reference to @array to the arrayref that has been passed in? How do you know that you create @array and not %array or $array? Where can I learn more about this * operator (perlop?). Thanks!

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.

解决方案

Assignment to a glob

*glob = 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.

This idiom

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

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
";
  print "$array = $array
";
  print "\%array = ",%array,"
";
  print "------------------
";
}

$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
------------------

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 glob (*)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆