Reading another package symbol table in Perl

I am trying to read a global character from another package. I have a package name as a string. I use qualify_to_reffrom Symbolmodule

    my $ref  = qualify_to_ref ( 'myarray', 'Mypackage' ) ;
    my @array =  @$ref ;

gives me I Not an ARRAY reference at ......guess the wrong dereferencing format is wrong.

Here is a complete sample program.

    use strict;
    use Symbol ;

    package Mypackage ;
    our @myarray = qw/a b/ ;

    package main ;

    my $ref  = qualify_to_ref ( 'myarray', 'Mypackage' ) ;
    my @array =  @$ref ;
+3
source share
4 answers

The function qualify_to_refreturns a reference to the type glob, which you can disable, for example:

my @array =  @{*$ref};

The glob dereferencing syntax is documented here .

+4
source

, , perldoc perlmod " ":

package Mypackage;
use strict;
use warnings;
our @myarray = qw/a b/;

package main;

our @array;
*array = \@Mypackage::myarray;
print "array from Mypackage is @array\n";

, , . , Mypackage Exporter.

+4

, FM , glob -:

my $array =  *{$ref}{ARRAY};

, IO, . Perl.

+1

: @$$ ref @$ref

0

Source: https://habr.com/ru/post/1741600/


All Articles