Skip to content

Instantly share code, notes, and snippets.

@Xliff
Last active March 19, 2023 12:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Xliff/d846eb39d472bcbd703be2a02d7e4357 to your computer and use it in GitHub Desktop.
Save Xliff/d846eb39d472bcbd703be2a02d7e4357 to your computer and use it in GitHub Desktop.
Accessing a CompUnits Symbol Table

So I needed to access the symbol table of one of my modules, recently. After a few hours on the web (and after wracking my brain for a bit) I came up with the following:

sub getSymbolTable (\T, $cu-name) is export {
  return Nil unless $cu-name;

  my $nodes = $cu-name.split('::').Array;
  my $fn    = $nodes.shift;
  my $st    = T.WHO{$fn};

  return Nil if $st.WHO === Nil;
  for $nodes[] {
    $st = $st.WHO{$_};
    return Nil if $st.WHO === Nil;
  }
  $st.WHO;
}

So if you have a package called A::B::C, then getSymbolTable(MY, "A::B::C") will extract its Stash for you.

Enjoy!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment