为什么下面的代码显示错误?
Why the following code shows an error?
我想从命令行传递参数,所以我尝试了以下代码但它抛出了错误?
use strict;
use warnings;
use Getopt::Long qw(GetOptions);
use Getopt::Std;
print "raw data:@ARGV\n";
my $site=getopts('bangalore','norwood','limerick');
if($site)
{
print "success";
}
else
{
die "error";
}
print "final data:@ARGV \n";
#!/usr/bin/perl -w
# a perl getopts example
# alvin alexander, http://www.devdaily.com
use strict;
use Getopt::Std;
# declare the perl command line flags/options we want to allow
my %options=();
getopts("hj:ln:s:", \%options);
# test for the existence of the options on the command line.
# in a normal program you'd do more than just print these.
print "-h $options{h}\n" if defined $options{h};
print "-j $options{j}\n" if defined $options{j};
print "-l $options{l}\n" if defined $options{l};
print "-n $options{n}\n" if defined $options{n};
print "-s $options{s}\n" if defined $options{s};
# other things found on the command line
print "Other things found on the command line:\n" if $ARGV[0];
foreach (@ARGV)
{
print "$_\n";
}
您的代码不正确。请先阅读文档:http://perldoc.perl.org/Getopt/Long.html
下面是对您试图实现的目标的猜测。
use strict;
use warnings;
use Getopt::Long qw(GetOptions);
my $city;
print "raw data:@ARGV\n";
GetOptions ("city=s" => $city) or die("Error in command line arguments\n");
my $site = $city;
if($site){
print "success: City is $city\n";
}
print "Final data:@ARGV \n";
输出:
chankeypathak@Whosebug:~/Desktop$ perl test.pl -city=bangalore
raw data:-city=bangalore
success: City is bangalore
Final data:
传递错误参数时的输出:
chankeypathak@Whosebug:~/Desktop$ perl test.pl -blah=blah
raw data:-blah=blah
Unknown option: blah
Error in command line arguments
我想从命令行传递参数,所以我尝试了以下代码但它抛出了错误?
use strict;
use warnings;
use Getopt::Long qw(GetOptions);
use Getopt::Std;
print "raw data:@ARGV\n";
my $site=getopts('bangalore','norwood','limerick');
if($site)
{
print "success";
}
else
{
die "error";
}
print "final data:@ARGV \n";
#!/usr/bin/perl -w
# a perl getopts example
# alvin alexander, http://www.devdaily.com
use strict;
use Getopt::Std;
# declare the perl command line flags/options we want to allow
my %options=();
getopts("hj:ln:s:", \%options);
# test for the existence of the options on the command line.
# in a normal program you'd do more than just print these.
print "-h $options{h}\n" if defined $options{h};
print "-j $options{j}\n" if defined $options{j};
print "-l $options{l}\n" if defined $options{l};
print "-n $options{n}\n" if defined $options{n};
print "-s $options{s}\n" if defined $options{s};
# other things found on the command line
print "Other things found on the command line:\n" if $ARGV[0];
foreach (@ARGV)
{
print "$_\n";
}
您的代码不正确。请先阅读文档:http://perldoc.perl.org/Getopt/Long.html
下面是对您试图实现的目标的猜测。
use strict;
use warnings;
use Getopt::Long qw(GetOptions);
my $city;
print "raw data:@ARGV\n";
GetOptions ("city=s" => $city) or die("Error in command line arguments\n");
my $site = $city;
if($site){
print "success: City is $city\n";
}
print "Final data:@ARGV \n";
输出:
chankeypathak@Whosebug:~/Desktop$ perl test.pl -city=bangalore
raw data:-city=bangalore
success: City is bangalore
Final data:
传递错误参数时的输出:
chankeypathak@Whosebug:~/Desktop$ perl test.pl -blah=blah
raw data:-blah=blah
Unknown option: blah
Error in command line arguments