get list of routines

get a list of all subroutines in a module (whatever a "module" is in the given language)

Download reflection.zip

Synopsis:

reflection.pl


reflection.pl

Synopsis
#WARNING UNTESTED

## this package is for testing...
package UnitTests;
my $var1 = 0;
sub test1($)
  {
  $var1 = 1;
  }
sub test2($)
  {
  $var1 = 1;
  }

#-------------------------------------------
package main;

my %tables = ();
my @todo = ();

#-------------------------------------------
sub addNewSymbolTable(@)
{
  my ($packageName, $refToSymbolTable) = @_;

  return if defined $tables{$packageName} ;

  $tables{$packageName} = $refToSymbolTable;
  push @todo, $packageName;
}

#-------------------------------------------
sub getSymbolTable($)
  {
  my ($packageName) = @_;
  return $tables{$packageName};
  }

#-------------------------------------------
sub isMoreTodo()
{
  return scalar @todo;
}

#-------------------------------------------
sub nextSymbolTable()
{
  return pop @todo;
}

#-------------------------------------------
sub forAllSymbolsIn($)
  {
  my ($packageName) = @_;
  my $refToSymbolTable = getSymbolTable($packageName);
  foreach my $symbolName (sort keys %{$refToSymbolTable})
    {
    ## get the symbol table entry (a typeglob)
    local *sym = $$refToSymbolTable{$symbolName};

    print "subroutine $packageName$symbolName()\n" if defined &sym;

    next if $symbolName !~ /::$/ ;         ## package names end with '::'
    next if defined $tables{$symbolName} ; ## skip is we've already searched the package.

    ## it's a package, so the symbolName is a package name, which names a symbol table (a hash)
    #print "package    $symbolName\n";
    addNewSymbolTable($symbolName, \%sym);
    }
  }

#-------------------------------------------
sub searchForAllSubroutinesIn(@)
{
  addNewSymbolTable(@_);
  while (isMoreTodo())
    {
    forAllSymbolsIn( nextSymbolTable() );
    }
}

##start from main and dump all packages and symbol tables in main
searchForAllSubroutinesIn("main::", \%main::);






Contact me about content on this page using john_web-at-arrizza-dot-com
For Web Master or site problems contact: webadmin-at-arrizza-dot-com
Copyright John Arrizza (c) 2001,2002,2003,2004,2005,2006,2007