Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I inspect a Perl 6 module?

Tags:

module

raku

In another question (How can I declare and use a Perl 6 module in the same file as the program?), I had code like this:

module Foo {
    sub foo ( Int:D $number ) is export {
        say "In Foo";
        }
    }

foo( 137 );

I wanted to inspect the Foo module to see if it's defined and what might be in it to do a bit of debugging. Since Foo is a module and not a class, do the meta-methods make sense?

Also, I thought there used to be a way to get a list of methods in a class. I'd like to get a list of subroutines in a module. That would be one way to test that I'd defined the right stuff and Perl 6 knows about them. In my Perl 5 stuff, I often test that I've defined a subroutine because I had a period where I'd choose a name in the module and a slightly different name in the tests (like last night, I guess, with valid_value and is_value_value). If I could test that foo is defined, I could do a bit of debugging here.

like image 737
brian d foy Avatar asked Jan 09 '16 01:01

brian d foy


1 Answers

You can get at a packages' symbol table by adding a trailing :: to its name. This will work for modules and classes both, but in case of classes will not include any methods, as those are associated with the type object and not the package itself.

The symbol table is of type Stash, which is associative (ie supports hash-like operations):

module Foo {
    sub foo is export { ... }
    sub bar is export(:bar) { ... }
    our sub baz { ... }
}

# inspect the symbol table
say Foo::.WHAT;       #=> (Stash)
say Foo::.keys;       #=> (EXPORT &baz)
say Foo::<&baz>.WHAT; #=> (Sub)

# figure out what's being exported
say Foo::EXPORT::.keys;          #=> (bar DEFAULT ALL)
say Foo::EXPORT::bar::.keys;     #=> (&bar)
say Foo::EXPORT::DEFAULT::.keys; #=> (&foo)
say Foo::EXPORT::ALL::.keys;     #=> (&bar &foo)
like image 145
Christoph Avatar answered Sep 18 '22 06:09

Christoph