Aliases of type glob

Ok, so it's easy to create an array reference ...

my @a; my $b=\@a; #can now reference the same list of scalars from either @$b or @a 

But how can I do this in reverse order? For example:

 my $a=[1..4]; my @b; #some magic happens here and now @b is an alias for @$a @b=(6..10); print "@$a\n"; #should print "6 7 8 9 10" 

I guess this will happen through typeglobs, but it just eludes me. Ideas?

It would also be nice to do the same for hashes as well as arrays.

EDIT: this seems to work, but it clones a bit, as it just copies the elements of the anon array into an โ€œaliasโ€ and then points to the array again:

 my @ b=@ $a; $a=\@b; 

Any better ideas?

+1
source share
4 answers

Two ways:

  • Globe Smoothing

    Perl calls "type glob" or "glob" for short, the structure of the data records in the symbol table. In this data structure, you can specify a link to the name of this link.

     *B = $A; # Sets the array slot of "B", @B. say for @B; 

    Note that it is usually required that Perl forbid us to use package variables (using use strict; ). You can get around this using

    • say for $this::package::B;
    • our @B; say for @B;

  • Data :: Alias

     alias my @B = @$A; say for @B; 
+5
source

All variables in perl programs are stored in namespaces. There are two types of namespaces:

  • Character tables. This is a global hash in which variables are stored.
  • Lexical areas. This is an anonymous temporary memory that is stored, not a PARTIAL symbol table, but it is attached to the block of your program. They store variables that we can only see in this program block.

Typeglobs are used to define records (variable, array, etc.) of symbol tables, but not for lexical sights. So, when you use this piece of code:

 my @b; *b = $a; 

you will receive it:

 Name "main::b" used only once: 

This tells us that the main :: b entry is not defined by us in the symbol tables, but we can do this using the " ours " modifier. Therefore, when you write like this:

 our @b; *b = $a; 

We can get a useful result for us because * b is stored in Symbol tables, and we can use the * typeglob operator.

+4
source

I think I understand ...

 my $a=[1..4]; our @b; *b=$a; print "@b\n"; @b=(6..10); print "@$a\n"; 

prints:

 1 2 3 4 6 7 8 9 10 

"ours" is still a little mysterious for me ... I guess I have some readings ...

+3
source

$ a and @a are not the same thing. $ a, since you assign it first, this is a reference to an anonymous array. It has nothing to do with @a, which is an array (not a link).

$ b = \ @ a # $ b contains a link to @a, but not to $ a.

$ a, @ a,% a are all different variables. So if you have

my @a = (1,2,3,4);

And then you declare

my $ a;

$ a does not contain a link to @a;

Variables are stored in a character table by type; scalar, hash, array, etc. That way you can have $ a, @ a,% a, & a ... and don't conflict with each other.

The fact is that

 #some magic happens here and now @b is an alias for @$a 

not happening. It still indicates the area of โ€‹โ€‹memory in which @a is stored is different from where $ a is stored.

0
source

All Articles