How can I convert the stringified version of array reference to actual array reference in Perl?

前端 未结 6 577
南笙
南笙 2020-11-29 11:11

Is there any way to get Perl to convert the stringified version e.g (ARRAY(0x8152c28)) of an array reference to the actual array reference?

For example



        
6条回答
  •  忘掉有多难
    2020-11-29 11:53

    The stringified version contains the memory address of the array object, so yes, you can recover it. This code works for me, anyway (Cygwin, perl 5.8):

    use Inline C;
    @a = (1,2,3,8,12,17);
    $a = \@a . "";
    print "Stringified array ref is $a\n";
    ($addr) = $a =~ /0x(\w+)/;
    $addr = hex($addr);
    $c = recover_arrayref($addr);
    @c = @$c;
    print join ":", @c;
    __END__
    __C__
    AV* recover_arrayref(int av_address) { return (AV*) av_address; }
    

    .

    $ perl ref-to-av.pl
    Stringified array ref is ARRAY(0x67ead8)
    1:2:3:8:12:17
    

提交回复
热议问题