Getopt::Long and <> problem

D

Daniel Berger

Hi all,

Perl 5.8.3
Solaris 9

I'm trying to use the <> operator in Getopt::Long to call a sub when
an unknown option occurs. However, it doesn't seem to work. I read
the docs, found a few online examples, but nothing that would indicate
why this would fail. It's probably something obvious but I don't see
it.

Help appreciated.

Regards,

Dan

# getopttest.pl
use strict;
use Getopt::Long;

$Getopt::Long::debug = 9;
#Getopt::Long::config qw(permute); # tried both
$Getopt::Long::eek:rder = $PERMUTE;

my($qt_dir, $CC);

# never gets called afaict
sub foo{
my $arg = shift;
print "WTF?-> $arg\n";
}

# Why isn't <> working?
GetOptions(
"--with-qt=s" => \$qt_dir,
"--with-gcc=s" => \$CC,
"<>" => \&foo
);

$CC ||= $ENV{CXX};

print "QT dir: $qt_dir\n";
print "CC: $CC\n";

# Output
perl getopttest.pl --foo
Getopt::Long 2.34 ($Revision: 2.68 $) called from package "main".
ARGV: (--foo)
autoabbrev=1,bundling=0,getopt_compat=1,gnu_compat=0,order=1,
ignorecase=1,requested_version=0,passthrough=0,genprefix="(--|-|\+)".
=> link "with-qt" to SCALAR(0x12cd14)
=> link "with-gcc" to SCALAR(0x12ccf0)
=> $opctl{with-gcc} = ARRAY(0x12ccb4) ["s","with-gcc",O,$,"<undef>"]
$opctl{with-qt} = ARRAY(0x1ca584) ["s","with-qt",O,$,"<undef>"]
=> arg "--foo"
=> find "--foo"
=> split "--"+"foo"
=> 0 hits () with "foo" out of 2
Unknown option: foo
QT dir:
CC: g++ -Wall -m64 -mcpu=ultrasparc -fno-strict-aliasing
 
B

Bob Walton

Daniel said:
Hi all,

Perl 5.8.3
Solaris 9

I'm trying to use the <> operator in Getopt::Long to call a sub when
an unknown option occurs. However, it doesn't seem to work. I read


I think you misinterpreted the Getopt::Long docs. They say:

"A special option 'name' <> can be used to designate a subroutine to
handle non-option arguments."
-------^^^^^^^^^^^^^^^^^^^^

Something like --foo looks like an option argument. Something like foo
does not look like an option argument. Try your code with:

perl getopttest.pl foo

and see what you get.


....
 
K

ko

Daniel said:
Hi all,

Perl 5.8.3
Solaris 9

I'm trying to use the <> operator in Getopt::Long to call a sub when
an unknown option occurs. However, it doesn't seem to work. I read
the docs, found a few online examples, but nothing that would indicate
why this would fail. It's probably something obvious but I don't see
it.

Help appreciated.

Regards,

Dan

# getopttest.pl
use strict;
use Getopt::Long;

$Getopt::Long::debug = 9;
#Getopt::Long::config qw(permute); # tried both
---------------------------------------^^^^^^^^^^
Probably won't make a difference. 'permute' is usually enabled by
default. You want the 'pass_through' configuration option:

#!/usr/bin/perl
# test.pl
use strict;
use warnings;
use Getopt::Long;
Getopt::Long::config qw[pass_through];
my($qt_dir, $CC);

sub foo {
print $_[0] =~ /^--/
? "unknown option: $_[0]\n"
: "unknown value: $_[0]\n";
}

GetOptions(
"--with-qt=s" => \$qt_dir,
"--with-gcc=s" => \$CC,
'<>' => \&foo
);
__END__

$ test.pl arg1 --foo foo --foobar arg2 --with-qt arg3
unknown value: arg1
unknown option: --foo
unknown value: foo
unknown option: --foobar
unknown value: arg2

You might also want to read about the 'require_order' option - see the
documentation.

[snip]

HTH - keith
 
D

Daniel Berger

ko said:
Daniel said:
Hi all,

Perl 5.8.3
Solaris 9

I'm trying to use the <> operator in Getopt::Long to call a sub when
an unknown option occurs. However, it doesn't seem to work. I read
the docs, found a few online examples, but nothing that would indicate
why this would fail. It's probably something obvious but I don't see
it.

Help appreciated.

Regards,

Dan

# getopttest.pl
use strict;
use Getopt::Long;

$Getopt::Long::debug = 9;
#Getopt::Long::config qw(permute); # tried both
---------------------------------------^^^^^^^^^^
Probably won't make a difference. 'permute' is usually enabled by
default. You want the 'pass_through' configuration option:

#!/usr/bin/perl
# test.pl
use strict;
use warnings;
use Getopt::Long;
Getopt::Long::config qw[pass_through];
my($qt_dir, $CC);

sub foo {
print $_[0] =~ /^--/
? "unknown option: $_[0]\n"
: "unknown value: $_[0]\n";
}

GetOptions(
"--with-qt=s" => \$qt_dir,
"--with-gcc=s" => \$CC,
'<>' => \&foo
);
__END__

HTH - keith

That does the trick. Thanks much!

Regards,

Dan
 

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
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top