Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

getopt::std help

by convenientstore (Pilgrim)
on Jan 07, 2008 at 02:28 UTC ( [id://660731]=perlquestion: print w/replies, xml ) Need Help??

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

not sure why below is not working?
am I calling it the wrong way?
I tried ./programname -h and thinking it was gonna print out the usage and exit
#!/usr/bin/perl -w use strict; use Getopt::Std; sub usage { print <<NOW; usage : $0 [-h] -l<something> -t<somethig> -f<file> syntax: -h prints this help text and exists -l sometihng -t someting -f something NOW exit 0; } my $o={} ; getopt('hltf', $o); #usage() if $$o{h}; if ($$o{h}) { usage(); }

Replies are listed 'Best First'.
Re: getopt::std help
by davidrw (Prior) on Jan 07, 2008 at 02:45 UTC
    Add this to see what's going on:
    use Data::Dumper; print Dumper $o;
    And you'll see that the key 'h' _is_ being created, and the value is undef, which is why your if statements don't pick it up, since it's a false value. This will do what you want:
    usage() if exists $o->{h};
      doh..
      Thank you.
      One more thing, will using getopt automatically reject the flags that are not explicitly specified (getopt('hltf', $o);), meaning if I call a program w/ ./program -x ,
      Will getopt automatically reject and abort the program or do I need to tell the program that?

        Try running ./program -x and see what happens. Sometimes the best way to learn is to experiment;)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (4)
As of 2024-04-19 01:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found