symbol table experiment - unknown output

Experimenting with this.  Here's a short program shows defined vars in package ZZZ.
I understand all the output expect the very last line.

why the last two lines:
   Here's the typeglob name "*ZZZ::second"
   $typeglob is defined


 Here's the output, (program is below)
====================================== output
=== I see something ===
Here's the typeglob name "*ZZZ::BEGIN"
=== I see something ===
Here's the typeglob name "*ZZZ::a"
%a is defined
=== I see something ===
Here's the typeglob name "*ZZZ::b"
@b is defined
=== I see something ===
Here's the typeglob name "*ZZZ::first"
$first is defined
=== I see something ===
Here's the typeglob name "*ZZZ::second"
$second is defined
=== I see something ===
Here's the typeglob name "*ZZZ::second"
$typeglob is defined

===================================== program
package ZZZ;

use strict;
  my $pA = "private";
  my $pB = "private2";

no strict;
  %a = qw(a A b B c C d D);
  @b = qw(u v w x y z);
  $first = "111";
  $second = "222";

use strict;

# See any symbols?
foreach my $symname (sort keys %ZZZ::) {
  print qq/=== I see something ===\n/;
  *typeglob = $ZZZ::{$symname};
  my $gname = $ZZZ::{$symname};

  print qq/Here's the typeglob name "$gname"\n/;

  print "\$$symname is defined\n" if defined $ZZZ::typeglob;
  print "\@$symname is defined\n" if defined @ZZZ::typeglob;
  print "\%$symname is defined\n" if defined %ZZZ::typeglob;
}
LVL 1
hank1Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

hank1Author Commented:
I lied, I don't know what BEGIN is either, but I can see it coming up in the next few pages.  Thanks
0
clockwatcherCommented:
BEGIN is a default code block (perldoc perlmod).  If you add a subroutine to ZZZ, you'll see it gets picked up as well.

package ZZZ;

use strict;
  my $pA = "private";
  my $pB = "private2";

no strict;
  %a = qw(a A b B c C d D);
  @b = qw(u v w x y z);
  $first = "111";
  $second = "222";

sub SampleCodeBlock
{
  return;
}

use strict;

# See any symbols?
foreach my $symname (sort keys %ZZZ::) {
  print qq/=== I see something ===\n/;
  *typeglob = $ZZZ::{$symname};
  my $gname = $ZZZ::{$symname};

  print qq/Here's the typeglob name "$gname"\n/;

  print "\$$symname is defined\n" if defined $ZZZ::typeglob;
  print "\@$symname is defined\n" if defined @ZZZ::typeglob;
  print "\%$symname is defined\n" if defined %ZZZ::typeglob;
}
0
clockwatcherCommented:
The "use" statement creates an implicit BEGIN block in ZZZ.

package zzz;
$a = "hello";

package yyy;
use strict;
$yyy::a = "hello";

package main;
print "package zzz:\n";
foreach (keys %zzz::)
{
    print "sym: $_\n";
}

print "\n\npackage yyy:\n";
foreach (keys %yyy::)
{
    print "sym: $_\n";
}
0
clockwatcherCommented:
I'm not sure I really understand where you're confused in the first one.  Your line:

  *typeglob = $zzz:{$symname}

is creating a typeglob variable called "typeglob" in your namespace.   It's an alias to another variable in your program and you're setting what it's an alias to in the loop.  Alphabetically, it's last, when the loop finally gets around to "typeglob" it's set to *second.  Technically, it's set to itself (*typeglob = *typeglob) which was last set to *second.

Does this make it more clear?

package zzz;
$a = "hello";
%b = { hello, there };

my @keys = sort(keys(%zzz::));

foreach (@keys)
{
  *g = $zzz::{$_};  
  print "$_\n";
  print "\$$_ is defined\n" if defined $zzz::g;
  print "\@$_ is defined\n" if defined @zzz::g;
  print "\%$_ is defined\n" if defined %zzz::g;

}

# the following weren't defined prior to the loop but typeglobs have been set up for them in the symbol namespace
# same thing is happening to g

$u = 3;
$v = 2;
$w = 1;
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
clockwatcherCommented:
Here's the output of the above with some explanations:

a
$a is defined      # *g = *a  ($a is defined)
b
%b is defined     # *g = *b (%b is defined)
g
%g is defined     # *g = *g   -->  this isn't changing its value it's still an alias for *b (%b is defined therefore %g is defined)
u                       # *g = *u   --> nothing's been defined for the typeglob u yet (and so on)
v
w
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Perl

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.