rahogaboom
rahogaboom

Reputation: 81

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)

Upvotes: 6

Views: 100

Answers (2)

Brad Gilbert
Brad Gilbert

Reputation: 34130

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
    }
  }
}

Upvotes: 4

codesections
codesections

Reputation: 9600

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.

Upvotes: 5

Related Questions