nysus has asked for the wisdom of the Perl Monks concerning the following question:

Was looking at the App::Cmd documentation. Has this in synopsis:

package YourApp; use App::Cmd::Setup -app;

I've never seen a dash used when importing a module. use and export module documentation didn't shed any light on it for me. So what does this do?

$PM = "Perl Monk's";
$MCF = "Most Clueless Friar Abbot Bishop Pontiff Deacon Curate Priest";
$nysus = $PM . ' ' . $MCF;
Click here if you love Perl Monks

Replies are listed 'Best First'.
Re: What does "-" in a use statement do?
by Corion (Patriarch) on Jan 27, 2018 at 13:42 UTC

    The leading "-" does what it does everywhere else in Perl too, so looking at use only tells you where to look next, because what you see is the use Module LIST form. So you will next be looking at how Perl parses a list, and elements of a list.

    See perlop on symbolic unary operators, which explains that -bareword is the same as "-bareword". So your expression above would be identical to:

    use App::Cmd::Setup "-app";

      OK, thanks. But now my question is, how is it possible for App::Cmd::Setup to export something called -app? I didn't think identifiers could start with a dash.

      $PM = "Perl Monk's";
      $MCF = "Most Clueless Friar Abbot Bishop Pontiff Deacon Curate Priest";
      $nysus = $PM . ' ' . $MCF;
      Click here if you love Perl Monks

        Nowhere in use does it say that the LIST should be a list of identifiers.

        If you read the documentation of App::Cmd::Setup, I presume that there it will also not talk about a list of identifiers that get exported, especially no identifier named -app.

        If you further read use (and the associated require) you will find that one of the things that use does is call a subroutine import if it exists. And that subroutine is called with all the parameters from the LIST part of the use statement.

        What that import subroutine does depends on the module. If the module uses the import subroutine from Exporter like the following:

        use Exporter 'import';

        ... then it will get the behaviour you seem to expect, of exporting subroutine names and variable names on demand.

        But most likely, App::Cmd::Setup does something different.

Re: What does "-" in a use statement do?
by Dallaylaen (Chaplain) on Jan 29, 2018 at 09:47 UTC

    Hello nysus,

    use Foo -bar; is essentially
    BEGIN { require Foo; Foo->import("-bar"); };
    You can define your own import sub, just like follows:
    package Foo; use strict; use warnings; sub import { warn "@_"; }; 1;
    Furthermore, you can use Exporter:
    package Foo; use strict; use warnings; use parent qw(Exporter); our @EXPORT_OK = qw(quux); sub import { warn "@_"; if (grep { /-bar/ } @_) { __PACKAGE__->export_to_level(1, undef, "quux"); }; }; sub quux { return 42; }; 1;

    And this will suddenly export quux to the calling package, despite having "-bar" in the use statement.

    No magic there, just a lot, A LOT of silent conventions.