Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

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

by hippo (Bishop)
on Dec 28, 2022 at 10:15 UTC ( #11149153=note: print w/replies, xml ) Need Help??

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

why are these so slow to import?

Well, if you think about it when you use Pod::Usage it has to parse the source of your code, extract the POD and process the POD to present usage info - that will all take time. You can save this by postponing the load until/unless it is actually needed (most runs will never need it, of course). The other option is to use pod2usage out-of-band in advance and just copy the output of that into your code instead statically.

I find that Getopt::Std is much faster than Getopt::Long. Its reduced functionality may well be worth the trade-off for you.


  • Comment on Re: slow startup for some common modules? (autodie, Pod::Usage, Getopt::Long))

Replies are listed 'Best First'.
Re^2: slow startup for some common modules? (autodie, Pod::Usage, Getopt::Long))
by almr (Sexton) on Dec 28, 2022 at 17:50 UTC

    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).

      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;
      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.

      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( ... );

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11149153]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (2)
As of 2023-03-21 04:17 GMT
Find Nodes?
    Voting Booth?
    Which type of climate do you prefer to live in?

    Results (59 votes). Check out past polls.