1. Perl
  2. Module creation

Understand typeglobs

The typeglob represents an entry in the symbol table.

 # * Main::num $main::{num} has almost the same meaning.
 # * num You can omit the package name.

On the symbol table, $main::{num} is assigned the string'* main::num', which is exactly what it is (called the symbol table entry, maybe). .. However, it's not a string, it's a typeglob.

If you omit the package name, it becomes an entry in the symbol table of the current current package.

Typeglobs and variable

my $num_from_typedglob = ${* main::num}; # Same as $num
my @num_from_typedglob = @{* main::num}; # Same as @num
my %num_from_typedglob = %{* main::num}; # Same as%num
my $ret_from_typedglob = & {* main::num}; # Same as & num (also same as num ())

A typeglob is a kind of hash, a scalar variable, array, < You can access a href="/blog/20161013147635.html">hash, subroutine, and more. It looks exactly like a dereference, but it's not a dereference.

Image of type glob

| - -- -- -- -- |
| * typedglob |-|
| - -- -- -- --- | |
                 | | - -- -- -- -- --- |
                 | - -- > | ${* typedglob} |
                 | | - -- -- -- -- --- |
                 | - -- > | @{* typedglob} |
                 | | - -- -- -- -- --- |
                 | - -- > |%{* typedglob} |
                 | | - -- -- -- -- --- |
                 | - -- > | & {* typedglob} |
                        | - -- -- -- -- --- |

Example

A example to understand typeglobs and symbol tables.

use strict;
use warnings;

# Type glob
# * typedglob;
# Represents an entry in the symbol table.


# Variable with the same name but different only in funny characters ($, @,%)
our $num = 11;
our @num = (12, 13);
our%num = (key => 14);

# Subroutine with the same name
sub num {
  return 15;
}

require Data::Dumper;

print "1-1: Access variable from the symbol table.\n";
# Enclosed in ${}, a scalar entry
my $num_from_symbol = ${$main::{num}};
# Array entries, enclosed in @{}
my @num_from_symbol = @{$main::{num}};
# Hash entries, enclosed in %{}
my %num_from_symbol = %{$main::{num}};

print Data::Dumper->Dump([$num_from_symbol], ['* num_from_symbol']);
print Data::Dumper->Dump([\@num_from_symbol], ['* num_from_symbol']);
print Data::Dumper->Dump([\%num_from_symbol], ['* num_from_symbol']);
print "\n";

print "1-2: Call a subroutine from the symbol table.\n";
my $ret_from_symbol = & {$main::{num}};
print "\$ret_from_symbol = $ret_from_symbol\n\n";

print "2-1: Access the variable from the typeglob.\n";
my $num_from_typedglob = ${* main::num}; Enclosed in # ${} is a scalar entry
my @num_from_typedglob = @{* main::num}; If you enclose it with # @{}, the array entry will be
my %num_from_typedglob = %{* main::num}; # %{} is a hash entry

print Data::Dumper->Dump([$num_from_typedglob], ['* num_from_typedglob']);
print Data::Dumper->Dump([\@num_from_typedglob], ['* num_from_typedglob']);
print Data::Dumper->Dump([\%num_from_typedglob], ['* num_from_typedglob']);
print "\n";

print "2-2: Call a subroutine from a typeglob.\n";
my $ret_from_typedglob = & {* main::num};
print "\$ret_from_typedglob = $ret_from_typedglob\n\n";

Related Informatrion