Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Best way for -h option for script to run perldoc on itself?

Reply
Thread Tools

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

 
 
Ed Hennig
Guest
Posts: n/a
 
      02-07-2006
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


 
Reply With Quote
 
 
 
 
Paul Lalli
Guest
Posts: n/a
 
      02-07-2006
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-P...b/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

 
Reply With Quote
 
 
 
 
Adam Funk
Guest
Posts: n/a
 
      02-07-2006
On 2006-02-07, Ed Hennig <(E-Mail Removed)> 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:fdDlwT:a", \%option) ;

if ( $option{h} ) {
exec("perldoc " . $0) ; # exec terminates this script
}
 
Reply With Quote
 
Donald King
Guest
Posts: n/a
 
      02-10-2006
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/
 
Reply With Quote
 
Ed Hennig
Guest
Posts: n/a
 
      02-10-2006
> 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.


 
Reply With Quote
 
Adam Funk
Guest
Posts: n/a
 
      02-14-2006
On 2006-02-10, Donald King <(E-Mail Removed)> 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?
 
Reply With Quote
 
A. Sinan Unur
Guest
Posts: n/a
 
      02-14-2006
Adam Funk <(E-Mail Removed)> wrote in
news:(E-Mail Removed):

> On 2006-02-10, Donald King <(E-Mail Removed)> 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 <(E-Mail Removed)>
(reverse each component and remove .invalid for email address)

comp.lang.perl.misc guidelines on the WWW:
http://mail.augustmail.com/~tadmc/cl...uidelines.html
 
Reply With Quote
 
Donald King
Guest
Posts: n/a
 
      02-14-2006
Adam Funk wrote:
> On 2006-02-10, Donald King <(E-Mail Removed)> 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/
 
Reply With Quote
 
Uri Guttman
Guest
Posts: n/a
 
      02-14-2006
>>>>> "EH" == Ed Hennig <(E-Mail Removed)> 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.velocityreviews.com/forums/(E-Mail Removed) -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
 
Reply With Quote
 
Ed Hennig
Guest
Posts: n/a
 
      02-16-2006
Uri Guttman <(E-Mail Removed)> wrote:

> >>>>> "EH" == Ed Hennig <(E-Mail Removed)> 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?


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
DHCP relay agent versus Option 3; Routers Option lcorrigan Cisco 2 09-27-2006 05:18 PM
no 'option' in aspx file means 'option'="false"? Cas ASP .Net 5 08-28-2006 10:36 AM
help! ( run perldoc as root ) nan li Perl Misc 5 03-29-2006 06:07 PM
g++ -pg option and -shared option Julien ROUZIERES C++ 1 12-21-2004 02:30 PM
Best way to integrate custom script with .NET's validator script P McGinness ASP .Net 0 01-14-2004 11:49 AM



Advertisments