On Tue, 2 May 2017 17:02:40 +0200
Gabor Szabo <szab...@gmail.com> wrote:
> Is there some way in Perl 6 to tell if a file was executed directly or
> loaded into memory as a module?

One way that seems to work: define a ``sub MAIN``; it will be invoked
when you execute the file as a program, but won't be touched if you
load it as a module.

Example: in file ``/tmp/x/Foo.pm6``::

  class Foo {
    has $.value;

  sub MAIN($value) {
    say Foo.new(:$value).value;


  $ perl6 -I /tmp/x -e 'use Foo;say Foo.new(:value(5)).value'

  $ perl6 /tmp/x/Foo.pm6
    /tmp/x/Foo.pm6 <value>

  $ perl6 /tmp/x/Foo.pm6 12

        Dakkar - <Mobilis in mobile>
        GPG public key fingerprint = A071 E618 DD2C 5901 9574
                                     6FE2 40EA 9883 7519 3F88
                            key id = 0x75193F88

Leela: You buy one pound of underwear and you're on their list forever.

Reply via email to