Best way for -h option for script to run perldoc on itself?

Discussion in 'Perl Misc' started by Ed Hennig, Feb 7, 2006.

  1. Ed Hennig

    Ed Hennig Guest

    What's the best or correct way to get a Perl program to handle the -h
    option (through getopts) by outputting its own perldoc documentation
    to the terminal?

    exec("perldoc " . $0) if ( $option{h} );

    ?

    --
    Thanks,
    Ed
    Ed Hennig, Feb 7, 2006
    #1
    1. Advertising

  2. Ed Hennig

    Paul Lalli Guest

    Ed Hennig wrote:
    > What's the best or correct way to get a Perl program to handle the -h
    > option (through getopts) by outputting its own perldoc documentation
    > to the terminal?
    >
    > exec("perldoc " . $0) if ( $option{h} );
    >
    > ?


    Check out the Pod::Usage module at
    http://search.cpan.org/~marekr/Pod-Parser-1.34/lib/Pod/Usage.pm

    An example from that manpage:
    use Pod::Usage;
    use Getopt::Long;

    ## Parse options
    GetOptions("help", "man", "flag1") || pod2usage(2);
    pod2usage(1) if ($opt_help);
    pod2usage(-verbose => 2) if ($opt_man);

    ## Check for too many filenames
    pod2usage("$0: Too many files given.\n") if (@ARGV > 1);

    Hope this helps,
    Paul Lalli
    Paul Lalli, Feb 7, 2006
    #2
    1. Advertising

  3. Ed Hennig

    Adam Funk Guest

    On 2006-02-07, Ed Hennig <> wrote:
    > What's the best or correct way to get a Perl program to handle the -h
    > option (through getopts) by outputting its own perldoc documentation
    > to the terminal?
    >
    > exec("perldoc " . $0) if ( $option{h} );


    That's roughly how I've been doing it, although I can't remember where
    I learned it. The following is an excerpt from one of my programs.

    use Getopt::Std ;
    my %option;

    getopts("hsb:t:p:p:fdDlwT:a", \%option) ;

    if ( $option{h} ) {
    exec("perldoc " . $0) ; # exec terminates this script
    }
    Adam Funk, Feb 7, 2006
    #3
  4. Ed Hennig

    Donald King Guest

    Ed Hennig wrote:
    > What's the best or correct way to get a Perl program to handle the -h
    > option (through getopts) by outputting its own perldoc documentation
    > to the terminal?
    >
    > exec("perldoc " . $0) if ( $option{h} );
    >
    > ?
    >


    FWIW, if you do stick with exec-ing perldoc instead of Pod::Usage, you
    should probably use the list form of exec:

    exec("perldoc", $0) if $option{h};

    This works even if the user has renamed the script to a path containing
    shell meta characters (especially spaces).

    --
    Donald King, a.k.a. Chronos Tachyon
    http://chronos-tachyon.net/
    Donald King, Feb 10, 2006
    #4
  5. Ed Hennig

    Ed Hennig Guest

    > FWIW, if you do stick with exec-ing perldoc instead of Pod::Usage, you
    > should probably use the list form of exec:
    >
    > exec("perldoc", $0) if $option{h};
    >
    > This works even if the user has renamed the script to a path containing
    > shell meta characters (especially spaces).


    Oh, good point!

    Thanks.
    Ed Hennig, Feb 10, 2006
    #5
  6. Ed Hennig

    Adam Funk Guest

    On 2006-02-10, Donald King <> wrote:

    (1)
    >> exec("perldoc " . $0) if ( $option{h} );


    > FWIW, if you do stick with exec-ing perldoc instead of Pod::Usage, you
    > should probably use the list form of exec:
    >

    (2)
    > exec("perldoc", $0) if $option{h};
    >
    > This works even if the user has renamed the script to a path containing
    > shell meta characters (especially spaces).


    Now that you mention it, I think I've read somewhere that (2) should
    also be slightly more efficient because the command is executed and
    the arguments are passed directly to it, without an intermediary
    shell, whereas in (1) Perl executes a shell and passes the single
    string to it to parse and process. Is this correct?
    Adam Funk, Feb 14, 2006
    #6
  7. Adam Funk <> wrote in
    news::

    > On 2006-02-10, Donald King <> wrote:
    >
    > (1)
    >>> exec("perldoc " . $0) if ( $option{h} );

    >
    >> FWIW, if you do stick with exec-ing perldoc instead of Pod::Usage,
    >> you should probably use the list form of exec:
    >>

    > (2)
    >> exec("perldoc", $0) if $option{h};
    >>
    >> This works even if the user has renamed the script to a path
    >> containing shell meta characters (especially spaces).

    >
    > Now that you mention it, I think I've read somewhere that (2) should
    > also be slightly more efficient because the command is executed and
    > the arguments are passed directly to it, without an intermediary
    > shell, whereas in (1) Perl executes a shell and passes the single
    > string to it to parse and process. Is this correct?


    perldoc -f exec

    perldoc -f system


    --
    A. Sinan Unur <>
    (reverse each component and remove .invalid for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
    A. Sinan Unur, Feb 14, 2006
    #7
  8. Ed Hennig

    Donald King Guest

    Adam Funk wrote:
    > On 2006-02-10, Donald King <> wrote:
    >
    > (1)
    >
    >>>exec("perldoc " . $0) if ( $option{h} );

    >
    >
    >>FWIW, if you do stick with exec-ing perldoc instead of Pod::Usage, you
    >>should probably use the list form of exec:
    >>

    >
    > (2)
    >
    >>exec("perldoc", $0) if $option{h};
    >>
    >>This works even if the user has renamed the script to a path containing
    >>shell meta characters (especially spaces).

    >
    >
    > Now that you mention it, I think I've read somewhere that (2) should
    > also be slightly more efficient because the command is executed and
    > the arguments are passed directly to it, without an intermediary
    > shell, whereas in (1) Perl executes a shell and passes the single
    > string to it to parse and process. Is this correct?


    True, although the performance difference is unlikely to matter on a
    modern system.

    --
    Donald King, a.k.a. Chronos Tachyon
    http://chronos-tachyon.net/
    Donald King, Feb 14, 2006
    #8
  9. Ed Hennig

    Uri Guttman Guest

    >>>>> "EH" == Ed Hennig <> writes:

    EH> What's the best or correct way to get a Perl program to handle the -h
    EH> option (through getopts) by outputting its own perldoc documentation
    EH> to the terminal?

    EH> exec("perldoc " . $0) if ( $option{h} );

    there are modules that can do that for you. search cpan for pod stuff.

    uri

    --
    Uri Guttman ------ -------- http://www.stemsystems.com
    --Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
    Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
    Uri Guttman, Feb 14, 2006
    #9
  10. Ed Hennig

    Ed Hennig Guest

    Uri Guttman <> wrote:

    > >>>>> "EH" == Ed Hennig <> writes:

    >
    > EH> What's the best or correct way to get a Perl program to handle the -h
    > EH> option (through getopts) by outputting its own perldoc documentation
    > EH> to the terminal?
    >
    > EH> exec("perldoc " . $0) if ( $option{h} );
    >
    > there are modules that can do that for you. search cpan for pod stuff.


    If

    exec("perldoc", $0) if ($option{h});

    works fine, why make things more complicated than necessary?

    What are the advantages of using the CPAN POD modules when all I want
    is to show something that looks like a man page?
    Ed Hennig, Feb 16, 2006
    #10
  11. Ed Hennig

    Uri Guttman Guest

    >>>>> "EH" == Ed Hennig <> writes:

    EH> Uri Guttman <> wrote:
    >> >>>>> "EH" == Ed Hennig <> writes:

    >>

    EH> What's the best or correct way to get a Perl program to handle the -h
    EH> option (through getopts) by outputting its own perldoc documentation
    EH> to the terminal?
    >>

    EH> exec("perldoc " . $0) if ( $option{h} );
    >>
    >> there are modules that can do that for you. search cpan for pod stuff.


    EH> If

    EH> exec("perldoc", $0) if ($option{h});

    EH> works fine, why make things more complicated than necessary?

    EH> What are the advantages of using the CPAN POD modules when all I want
    EH> is to show something that looks like a man page?

    a short list:

    $0 isn't alway correct as it can be modified on some OS's

    it forks off a process (actually execs) which is slower. but
    speed isn't usually an issue here.

    perldoc may not be in your path or even installed in
    bin. modules are more likely to be where you put them.

    the pod modules that do this type of thing can do more than just
    run perldoc.

    some pod munging modules can even generate the arg parsing stuff
    for you from the pod. damian conway wrote such a module.

    i am sure i could find other reasons. anyhow you do what you want. just
    check out cpan for them. you might be surprised and end up using one of
    them.

    uri

    --
    Uri Guttman ------ -------- http://www.stemsystems.com
    --Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
    Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
    Uri Guttman, Feb 16, 2006
    #11
  12. Ed Hennig

    Ed Hennig Guest

    Uri Guttman <> wrote:

    > EH> What are the advantages of using the CPAN POD modules when all I want
    > EH> is to show something that looks like a man page?
    >
    > a short list:
    >
    > $0 isn't alway correct as it can be modified on some OS's
    >
    > it forks off a process (actually execs) which is slower. but
    > speed isn't usually an issue here.
    >
    > perldoc may not be in your path or even installed in
    > bin. modules are more likely to be where you put them.
    >
    > the pod modules that do this type of thing can do more than just
    > run perldoc.
    >
    > some pod munging modules can even generate the arg parsing stuff
    > for you from the pod. damian conway wrote such a module.


    Interesting points. I only work on *nix-like systems where $0 works
    and perldoc is in $PATH, so I've never noticed any of those issues.

    > i am sure i could find other reasons. anyhow you do what you want. just
    > check out cpan for them. you might be surprised and end up using one of
    > them.


    I'll take a look. Thanks for the response!
    Ed Hennig, Feb 16, 2006
    #12
  13. Ed Hennig <> wrote:
    > Uri Guttman <> wrote:
    >> >>>>> "EH" == Ed Hennig <> writes:

    >>
    >> EH> What's the best or correct way to get a Perl program to handle the -h
    >> EH> option (through getopts) by outputting its own perldoc documentation
    >> EH> to the terminal?
    >>
    >> EH> exec("perldoc " . $0) if ( $option{h} );
    >>
    >> there are modules that can do that for you. search cpan for pod stuff.

    >
    > If
    >
    > exec("perldoc", $0) if ($option{h});
    >
    > works fine,



    Because you have a program named "perldoc" in your path.


    > why make things more complicated than necessary?



    So they will work when there is no program named "perldoc" in the path.


    > What are the advantages of using the CPAN POD modules when all I want
    > is to show something that looks like a man page?



    No dependencies on external programs.

    Works when the path gets changed.


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Feb 16, 2006
    #13
    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. P McGinness
    Replies:
    0
    Views:
    286
    P McGinness
    Jan 14, 2004
  2. Ollie
    Replies:
    0
    Views:
    359
    Ollie
    Oct 2, 2003
  3. zerg
    Replies:
    125
    Views:
    2,554
  4. equation .
    Replies:
    27
    Views:
    1,358
    Joshua Maurice
    Jan 11, 2011
  5. nan li

    help! ( run perldoc as root )

    nan li, Mar 22, 2006, in forum: Perl Misc
    Replies:
    5
    Views:
    123
    Guest
    Mar 29, 2006
Loading...

Share This Page