Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

how to write script usage sub routine for the perl file?

by perlanswers (Initiate)
on Apr 03, 2017 at 05:02 UTC ( #1186769=perlquestion: print w/replies, xml ) Need Help??

perlanswers has asked for the wisdom of the Perl Monks concerning the following question:

How can i write the usage function to run the file in perl.
./html.pl -root="/home/data/multiple/" -out="/home/data/output" -file= +"/home/data/file.txt"

I have the above perl file.I have to run the same file in the terminal as i mentioned above.How can i include usage of the script inside the perl file.
code which i have tried
use strict; use warnings; use Getopt::Long qw(GetOptions); sub usage { if (@_) { my ($msg) = @_; chomp($msg); print(STDERR "$msg\n"); } GetOptions( 'root=s' => \my $dir, 'out=s' => \my $out, 'file=s' => \my $file, ) or usage();

Replies are listed 'Best First'.
Re: how to write script usage sub routine for the perl file? the dynamic duo with Pod::Usage
by Discipulus (Abbot) on Apr 03, 2017 at 07:54 UTC
    Hello perlanswers and welcome to the monastery and to wonderful world of Perl!

    There are many way to do it but I found the following particulary useful and versatile: The Dynamic Duo --or-- Holy Getopt::Long, Pod::UsageMan!

    Infact Pod::Usage can semplfy a lot such part of a program: it can shows sections of your choice of the POD embed in the program (I'm used to put the POD after the __DATA__ token), it has the ability to modify the exit value with the useful NOEXIT feature.

    The module can semplify a lot the help and manual commandline options; let's see it's recommendend usage:

    use strict; use Pod::Usage; use Getopt::Long; ## Parse options my %opt; GetOptions(\%opt, "help|?", "man", "flag1") || pod2usage(2); pod2usage(1) if ($opt{help}); pod2usage(-exitval => 0, -verbose => 2) if ($opt{man}); ## Check for too many filenames pod2usage("$0: Too many files given.\n") if (@ARGV > 1);

    L*

    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
Re: how to write script usage sub routine for the perl file?
by huck (Parson) on Apr 03, 2017 at 06:35 UTC

      Looks like the same person.

Re: how to write script usage sub routine for the perl file?
by Anonymous Monk on Apr 03, 2017 at 05:52 UTC

    From looking at the code, the usage function expects an argument. But, none is given.

    ... or usage();
    A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (6)
As of 2021-01-27 12:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?