简体   繁体   English

在perl中使用split和指向结构的指针

[英]Using split in perl with pointer to a structure

Given the following piece of code: 给出以下代码:

($a, $b, $c, $d ) = split(' ',$trav->{'Lptr'})

Lptr is a pointer to a structure. Lptr是指向结构的指针。 The structure has 6 elements of which 3 are also pointer to structures. 该结构具有6个元素,其中3个也指向结构。

I am unable to understand the usability of split here. 我无法在此处了解拆分的可用性。 What would be the output of this code, what is assigned to a,b,c & d ? 该代码的输出是什么,分配给a,b,c&d的是什么?

If $trav->{Lptr} is indeed a reference, then that reference will be stringified and the resulting string (eg, "HASH(0x9084818)") will be stored in $a . 如果$trav->{Lptr}确实是一个引用,则该引用将被字符串化,结果字符串(例如,“ HASH(0x9084818)”)将存储在$a The other three variables will remain undef. 其他三个变量将保持undef。 The split will effectively do nothing because the stringification of a reference will not contain any spaces for it to split on. split实际上不会做任何事情,因为引用的字符串化不会包含任何要拆分的空格。

This is easily determined by testing on the command line: 通过在命令行上进行测试可以轻松确定这一点:

$ perl -w -E '($a, $b, $c, $d) = split(" ", {}); say "a => $a, b => $b, c => $c, d => $d";'
Use of uninitialized value $b in concatenation (.) or string at -e line 1.
Use of uninitialized value $c in concatenation (.) or string at -e line 1.
Use of uninitialized value $d in concatenation (.) or string at -e line 1.
a => HASH(0x9ae2818), b => , c => , d => 

The only situation I could think of in which code like this would be some kind of useful would be if $trav->{Lptr} was an object with overloaded stringification like this: 我可以想到的唯一情况是,如果$trav->{Lptr}是一个带有重载字符串化的对象,例如:

#!/usr/bin/env perl
package Foo;
use Moo;
use overload '""' => \&to_string;
use feature 'say';

# prepare attributes
has name    => (is => 'ro');
has numbers => (is => 'rw', isa => sub { die unless ref($_[0]) eq 'ARRAY' });

# stringification method
sub to_string {
    my $self = shift;
    return  'name:'     . $self->name . ' '
        .   'numbers:'  . join '_' => @{$self->numbers};
}

# create an object
my $zaphod = Foo->new(name => 'Zaphod', numbers => [17, 42]);

#---------------------------------------------------------------------------

# split stringification
my ($name, $numbers) = split / / => $zaphod;
say $name;
say $numbers;

Output: 输出:

name:Zaphod
numbers:17_42

... for strange values of 'useful'. ...对于“有用”的奇怪值。 ;) ;)

Test it and see: 测试一下,看看:

    perl -e '$emote={"one"=>":)","two"=>":]"};
    $remote=["remote", "pointer"]; 
    $fish=["hake","cod","whiting"];
    $trav{Lptr}=[$remote,$emote,$fish,"and a scalar"];
    use Data::Dumper;
    print Dumper\%trav;
    ($a,$b,$c,$d)=split(/\s/,$trav{Lptr});
    print "a is: $a\nb is: $b\nc is: $c\nd is: $d\n"'
    $VAR1 = {
          'Lptr' => [
                      [
                        'remote',
                        'pointer'
                      ],    
                      {
                        'one' => ':)',
                        'two' => ':]'
                      },
                      [
                        'hake',
                        'cod',
                        'whiting'
                      ],
                      'and a scalar'
                    ]
        };
    a is: ARRAY(0x9a083d0)
    b is:
    c is:
    d is:

If this code is functioning, you are misrepresenting/misunderstanding it 如果此代码有效,则表示您误解/误解了

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM