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

前端 未结 6 576
南笙
南笙 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:59

    Yes, it's possible: use Devel::FindRef.

    use strict;
    use warnings;
    use Data::Dumper;
    use Devel::FindRef;
    
    sub ref_again {
       my $str = @_ ? shift : $_;
       my ($addr) = map hex, ($str =~ /\((.+?)\)/);
       Devel::FindRef::ptr2ref $addr;
    }
    
    my $ref = [1, 2, 3];
    my $str = "$ref";
    my $ref_again = ref_again($str);
    
    print Dumper($ref_again);
    

提交回复
热议问题