Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to modify raku code "say $?PACKAGE;" to print the fully qualified package name

    say $?PACKAGE;
    class Foo {
        say $?PACKAGE;
        class Bar {
            say $?PACKAGE;
            my package P {say $?PACKAGE;}
        }
    }

This code prints:

(GLOBAL)
(Foo)
(Bar)
(P)

Wanted:

(GLOBAL)
(GLOBAL::Foo)
(GLOBAL::Foo::Bar)
(GLOBAL::Foo::Bar::P)
like image 338
rahogaboom Avatar asked Mar 02 '21 17:03

rahogaboom


People also ask

What is a raku module?

In Raku module can also refer to a type of package declared with the module keyword (see Module Packages and the examples below) but here we mostly mean "module" as a set of source files in a namespace. zef is the application used for installing modules in Raku.

How to distribute a raku module to CPAN?

(See use lib pragma, -I command line switch, or RAKULIB env variable, to include a path to your module source while developing it, so you don't have to install it at all). Uploading a module to CPAN is the preferred way of distributing Raku modules. A prerequisite for this is a PAUSE user account.

How do I get Started with Raku?

The only requirement is that you know how to be nice to all kinds of people (and butterflies). Go to #raku (irc.libera.chat) and someone will be glad to help you get started . Jump in! Rakudo is a compiler for Raku code. Install it and you're all set to run Raku programs! More…

What does depends mean in raku?

It is used to describe the module in the Raku ecosystem. The depends, build-depends, and test-depends sections include different modules that are used in those phases of the of installation. All are optional, but they must obviously contain the modules that are going to be needed in those particular phases.


2 Answers

For the benefit of others who may have the same question, I'll expand on the correct answer you already got in a comment from @ugexe:

You can get print the full package path by printing the debug representation of $*PACKAGE with the .raku method or (on Rakudo only), with the dd routine.

Thus, your code could be modified to have the output shown below:

dd $?PACKAGE;                         # OUTPUT: «GLOBAL»
class Foo {
    dd $?PACKAGE;                     # OUTPUT: «Foo»
    class Bar {
        dd $?PACKAGE;                 # OUTPUT: «Foo::Bar»
        my package P {say $?PACKAGE;} # OUTPUT: «Foo::Bar::P»
    }
}

Note that GLOBAL is implicit and thus isn't printed unless it is the only item in the path.

like image 74
codesections Avatar answered Oct 19 '22 04:10

codesections


When you call say you get .gist of the argument.
The .gist is intended to give a human just enough information to figure out what is going on.

If you actually want the name of the package, ask for it.
Or more specifically, ask the metamodel to give it to you with .^name.

say $?PACKAGE.^name;       # GLOBAL
class Foo {
  say $?PACKAGE.^name;     # Foo
  class Bar {
    say $?PACKAGE.^name;   # Foo::Bar
    my package P {
      say $?PACKAGE.^name; # Foo::Bar::P
    }
  }
}
like image 35
Brad Gilbert Avatar answered Oct 19 '22 04:10

Brad Gilbert