in reply to Re: slow startup for some common modules? (autodie, Pod::Usage, Getopt::Long))
in thread slow startup for some common modules? (autodie, Pod::Usage, Getopt::Long))

By "postponing until needed", do you mean something like eval q{use Pod::Usage}, whenever the help message is needed ?

Thanks for the pointer to Getopt::Std, I'll consider it the next time around. I've ended up coding a simple arg-parsing loop in the meanwhile (the repo is ssh_agent_share; quite trivial, but it seems I end up learning something new every time I reach back to perl).

Replies are listed 'Best First'.
Re^3: slow startup for some common modules? (autodie, Pod::Usage, Getopt::Long))
by Marshall (Canon) on Dec 29, 2022 at 05:44 UTC
    use Pod::Usage; # is the same as BEGIN { require Pod::Usage; Pod::Usage->import(); }
    BEGIN blocks execute at compile time, not run time.

    Assuming that often you run without ever using the Pod functions, and you want to fire that module up only if you will be needing it, you could have a runtime flag like this:

    if ($needPod) { require Pod::Usage; Pod::Usage->import(); }
    I leave it you to decide how this applies to your code. You have to do the require and import before using any functionality of Pod::Usage.

    Usually GetOPt::Std is enough for me. I almost never use autodie and absolutely never use it in end user code.

      Ah. Then why not use Pod::Usage if $needPod;
        > use Pod::Usage if $needPod;

        Timing! You can't make a compile-time use (the BEGIN-part) depend on run-time conditions.

        FWIW: you could use the if pragma, but only if the condition is available at compile-time

          Because use takes effect at compile time, it doesn't respect the ordinary flow control of the code being compiled. In particular, putting a use inside the false branch of a conditional doesn't prevent it from being processed. If a module or pragma only needs to be loaded conditionally, this can be done using the if pragma:

          use if $] < 5.008, "utf8"; use if WANT_WARNINGS, warnings => qw(all);

        but as you see the conditions here are (compile time) constants

        Cheers Rolf
        (addicted to the 𐍀𐌴𐍂𐌻 Programming Language :)
        Wikisyntax for the Monastery

Re^3: slow startup for some common modules? (autodie, Pod::Usage, Getopt::Long))
by NERDVANA (Priest) on Dec 31, 2022 at 07:34 UTC
    I use this recipe in most scripts:
    use Getopt::Long; sub pod2usage { require Pod::Usage; goto \&Pod::Usage::pod2usage; } GetOptions( ... ... ) or pod2usage(2);
    I don't mind paying for Getopt::Long because I like its features, but there's no reason to load Pod::Usage unless the user screws up the commandline.

    On that note, someone ought to contribute a patch to Pod::Usage that avoids loading anything until it gets called. Then I could skip that bit of ugly boilerplate in my code.

Re^3: slow startup for some common modules? (autodie, Pod::Usage, Getopt::Long))
by Anonymous Monk on Dec 28, 2022 at 18:40 UTC

    The eval q{use Pod::Usage}; will work, though those who are allergic to stringy evals will probably prefer something more like

    require Pod::Usage;
    Pod::Usage::pod2usage( ... );