Perl $$var — two dollar signs as a sigil?

后端 未结 2 1048
慢半拍i
慢半拍i 2021-01-01 10:35

This seems like something I should be able to easily Google, Bing, or DDG, but I\'m coming up completely empty on everything.

Basically, I\'m tasked with (in part) r

相关标签:
2条回答
  • 2021-01-01 11:09

    This is an alternative form of dereferencing. Could (and honestly probably should) be written as:

    $LOC->{'DESCRIPTION'};
    

    You'll sometimes see the same with other all basic data types:

    my $str = 'a string';
    my %hash = (a => 1, b => 2);
    my @array = (1..4);
    
    my $strref = \$str;
    my $hashref = \%hash;
    my $arrayref = \@array;
    
    print "String value is $$strref\n";
    print "Hash values are either $$hashref{a} or $hashref->{b}\n";
    print "Array values are accessed by either $$arrayref[0] or $arrayref->[2]\n";
    
    0 讨论(0)
  • 2021-01-01 11:14

    Dereferencing is treating a scalar as the 'address' at which you can locate another variable (scalar, array, or hash) and its value.

    In this case, $LOC{DESCRIPTION} is being interpreted as the address of another scalar, which is read into $comment.

    In Perl,

    my $str = 'some val';
    my $ref = \$str;
    my $val = ${$ref};
    my $same_val = $$ref;
    
    0 讨论(0)
提交回复
热议问题