• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 193
  • Last Modified:

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;
}
0
hank1
Asked:
hank1
  • 4
1 Solution
 
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
 
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

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now