CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
Is it possible to use '?' as an option to &Getopts()?

 



cjsmall
New User


Jul 17, 2015, 1:27 AM

Post #1 of 5 (4454 views)
Is it possible to use '?' as an option to &Getopts()? Can't Post

&Getopts('?') does not complain about placing a '?' in the options list, but perl will not accept $opt_? as valid syntax. Is there any trick that can be used to determine whether -? was set on the command line?

Just as a point of reference, in a ksh script, getopts will set ${OPTARG} to "?" when it encounters the -? switch. I am trying to reproduce this same behavior in perl.

Thanks.


Laurent_R
Veteran / Moderator

Jul 17, 2015, 3:21 AM

Post #2 of 5 (4448 views)
Re: [cjsmall] Is it possible to use '?' as an option to &Getopts()? [In reply to] Can't Post

AFAICT, there isn't any reason to end up with a

Code
$opt_?

when using the Getopt module.

Pleased take a look at the following documentation: http://search.cpan.org/~rjbs/perl-5.22.0/lib/Getopt/Std.pm.

If that does not help, please show the code your are using.


BillKSmith
Veteran

Jul 17, 2015, 6:08 AM

Post #3 of 5 (4441 views)
Re: [cjsmall] Is it possible to use '?' as an option to &Getopts()? [In reply to] Can't Post

Use of the option hash solves the syntax problem.

I have found the following behavior by experiment when I specified '?' as a Boolean. It does not appear to be documented.

If the switch -? does not appear on the command line, the key $opts{'?'} does not exist.
If it does appear, the value $opts{'?'} is not defined.

In general, I do not recommend exploiting undocumented 'features' of a module. (Changes to the module could cause your code to fail.) Given that caveat, the following code will meet your requirement for now.


Code
use strict; 
use warnings;
use Getopt::Std;
use Data::Dumper;
my %opts;
getopt('?:', \%opts) or die" Unable to fetch arguments\n";
$opts{'?'} //= '?' if exists $opts{'?'};
print Dumper(\%opts);


I do not know what ksh does if -? does not appear on the command line. In perl, you can use the test:

Code
if(!exists $opts{'?'}){ 
# '-?' does not appear on command line.
}

Good Luck,
Bill


FishMonger
Veteran / Moderator

Jul 17, 2015, 6:19 AM

Post #4 of 5 (4441 views)
Re: [cjsmall] Is it possible to use '?' as an option to &Getopts()? [In reply to] Can't Post

Use a lexical hash instead of the global $opt_ vars.

Code
my %opts; 
getopt('?', \%opts);


Personally, I think the Getopt::Long module coupled with Pod::Usage would be a better choice than Getopt::Std.

Code
use Getopt::Long; 
use Pod::Usage;

my $man = 0;
my $help = 0;

GetOptions('help|?' => \$help, man => \$man) or pod2usage(2);
pod2usage(1) if $help;
pod2usage(-exitval => 0, -verbose => 2) if $man;

http://search.cpan.org/~jv/Getopt-Long-2.47/lib/Getopt/Long.pm#Documentation_and_help_texts


(This post was edited by FishMonger on Jul 17, 2015, 6:20 AM)


cjsmall
New User


Jul 20, 2015, 12:50 PM

Post #5 of 5 (4301 views)
Re: [cjsmall] Is it possible to use '?' as an option to &Getopts()? [In reply to] Can't Post

Thanks to everyone for the responses. It turns out that my O'Reilly Perl 5 book does not document a number of the features of Getopts used in these examples, including the hashing option. I really appreciate the pointers -- and the lesson is: refer to the online documentation in the future! :-)

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives