brian d foy
brian d foy

Reputation: 132896

Is there a way to get a list of all known types in a Perl 6 program?

Is there a way to get a list of all known types (builtin, defined, loaded, whatever) a Perl 6 program knows about? I don't have a particular task in mind, and this is a bit different than figuring out if a type I already know about has been defined.

Upvotes: 13

Views: 324

Answers (1)

smls
smls

Reputation: 5791

This should do the trick:

.say for (|CORE::, |UNIT::, |OUTERS::, |MY::)
    .grep({ .key eq .value.^name })
    .map(*.key)
    .unique
;

Explanation:

Perl 6 provides Pseudo-packages that allow indirect look-up of symbols that are declared/visible in different scopes. They can be accessed and iterated like Hashes.

  • All built-in symbols should be in CORE::.
  • Finding all those declared in (or imported into) the current lexical scope or one of its parent scopes, is more tricky.
    Based on its description in the documentation, I would have thought LEXICAL:: would contain them all, but based on some experimentation that doesn't seems to be case and it looks like UNIT::, OUTERS::, and MY:: need to be searched to catch 'em all.

The kind of symbols defined in those pseudo-packages include:

  • types (packages, modules, classes, roles, native types, enum types, subset types)
  • functions (subroutines, terms and operators)
  • enum values
  • variables & constants

To get only the types, I grepped the ones where the symbol's declared name equals the name of its object type.

If you wanted only classes, you could add the following step:

    .grep({ .value.HOW.^name eq 'Perl6::Metamodel::ClassHOW' })

Upvotes: 14

Related Questions