Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Setting up helper objects in App::Cmd

by McA (Curate)
on Sep 25, 2012 at 10:06 UTC ( #995520=perlquestion: print w/ replies, xml ) Need Help??
McA has asked for the wisdom of the Perl Monks concerning the following question:

Hi all!

Another question to App::Cmd:

Where is the right place to set up helper objects that are accessible to all commands like loggers or database handles?

Best regards
McA

Comment on Setting up helper objects in App::Cmd
Download Code
Re: Setting up helper objects in App::Cmd
by tobyink (Abbot) on Sep 25, 2012 at 10:21 UTC

    I'd generally name my modules things like:

    • MyApp::Command::Foo
    • MyApp::Command::Bar
    • MyApp::Lib::Foobar
    • MyApp::Lib::Database

    And instead of loading library modules at compile time with use MyApp::Lib::Database, defer loading to run-time (with require MyApp::Lib::Database) inside your command's execute method. This means that you can avoid loading up database connections, etc when the user is just running, say, myapp help. Load them only once they're needed.

    PS: a release using this technique is P5U.

    perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'

      Hi Tobyink!

      Once again thank you for your reply. A ++ for that.

      When I need to do some module initialization for EVERY command. Is there a place/method to do so? Especially when I do initialization work based on the global options. I don't like the idea of doing the same thing, like

      require SomeLib; SomeLib->init();
      in every command's execute method.

      Best regards
      McA

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://995520]
Approved by chacham
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (13)
As of 2014-10-25 14:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (145 votes), past polls