quick help message; or delayed 'use <pkg>'

Discussion in 'Perl Misc' started by badarisj@gmail.com, Mar 10, 2006.

  1. Guest

    folks,

    in our perl programs we have bunch of 'use <pkg>' that load bunch of
    module
    to perform the actual function of the program.

    But when the user specifies
    a simple '-help', it is annoying for the users to have perl program
    load everything
    and then spit out the help message. so couple of questions:

    - is there anything like 'delayed use' ?
    - may be i could place the 'use <pkg>' statements
    of the modules not related displaying help message inside an 'eval'
    so that perl would
    load those modules only when help is not specified.
    - probably we could go back to use of 'require <file>' ; but that
    would have caveat that
    we may miss the non-presence of a certain module loaded only under
    certain circumstance.

    just curious as to if others faced similar problem and how they
    addressed it.

    thanks,
    -badari
     
    , Mar 10, 2006
    #1
    1. Advertising

  2. Paul Lalli Guest

    wrote:
    > in our perl programs we have bunch of 'use <pkg>' that load bunch of
    > module
    > to perform the actual function of the program.
    >
    > But when the user specifies
    > a simple '-help', it is annoying for the users to have perl program
    > load everything
    > and then spit out the help message. so couple of questions:
    >
    > - is there anything like 'delayed use' ?


    Yes.

    use Foo;

    is exactly identical to

    BEGIN {
    require Foo;
    Foo->import;
    }

    If you don't want your module loading to happen immeidately, write
    those two lines yourself, not inside a BEGIN{} block, whenever you want
    them to happen.

    That being said, however, I would suggest taking the exact opposite
    approach. If you have code you wanted executed before the module
    loading, put this code inside a BEGIN{} block before the use Foo;
    statements:

    BEGIN {
    if ($ARGV[0] eq '--help'){
    print "To use this program. . . . \n";
    }
    }
    use Foo;
    use Bar;
    #etc


    Paul Lalli
     
    Paul Lalli, Mar 10, 2006
    #2
    1. Advertising

  3. Paul Lalli wrote:

    > use Foo;
    >
    > is exactly identical to
    >
    > BEGIN {
    > require Foo;
    > Foo->import;
    > }
    >
    > If you don't want your module loading to happen immeidately, write
    > those two lines yourself, not inside a BEGIN{} block, whenever you want
    > them to happen.


    Good advice.

    > That being said, however, I would suggest taking the exact opposite
    > approach. If you have code you wanted executed before the module
    > loading, put this code inside a BEGIN{} block before the use Foo;
    > statements:
    >
    > BEGIN {
    > if ($ARGV[0] eq '--help'){
    > print "To use this program. . . . \n";
    > }
    > }


    I think this is bad advice. This is mostly based on a gut feeling and
    not so easy to justify to anyone who can't see "it's just wrong".

    Except in "perl -ne" the BEGIN{} block is there for stuff that really
    needs to be done at compile time even when not executing the script.

    If you really do want to abuse BEGIN{} this way you should at least
    check $^C.
     
    Brian McCauley, Mar 11, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jeffy
    Replies:
    2
    Views:
    2,679
    Thomas Weidenfeller
    Sep 10, 2003
  2. mrstephengross
    Replies:
    0
    Views:
    347
    mrstephengross
    Mar 10, 2006
  3. ryles
    Replies:
    3
    Views:
    537
    Piet van Oostrum
    Jul 26, 2009
  4. matt
    Replies:
    1
    Views:
    401
  5. matt
    Replies:
    8
    Views:
    224
    Mauricio Fernández
    Jun 19, 2004
Loading...

Share This Page