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

E

Ed Hennig

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} );

?
 
P

Paul Lalli

Ed said:
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
 
A

Adam Funk

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
}
 
D

Donald King

Ed said:
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).
 
E

Ed Hennig

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

Adam Funk

(1)
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?
 
A

A. Sinan Unur

(1)


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
 
D

Donald King

Adam said:
(1)



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

Uri Guttman

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
 
E

Ed Hennig

Uri Guttman said:
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?
 
U

Uri Guttman

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> 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
 
E

Ed Hennig

Uri Guttman said:
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!
 
T

Tad McClellan

Ed Hennig said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,183
Messages
2,570,968
Members
47,517
Latest member
TashaLzw39

Latest Threads

Top