用 Getopt::Std 替换 getopts.pl

用 Getopt::Std 替换 getopts.pl

我正在使用 Cygwin 版本 2.10.0 和 perl 5.26.0。我需要更改getopts.pl为,Getopt::Std但是线路该怎么办&Getopts('F:f:');呢?

#! /usr/bin/perl
require "getopts.pl" ;

# Perl script to take particle data and
# plot using (in this case) GMT to 
# produce a postscript file of specified size.
# Assumption is that this is a frame for a movie
# and hence that time information is meaningful

&Getopts('F:f:');
# Options:   -f: Filename for input data
# Options:   -F: Filename (root) for output data

# default values for parameters if not specified
if($opt_F eq "") {
  $opt_F = "ascii-conversion";
}

# Read the particle file ...   Assume 2D !! 
open(PAR,"< $opt_f") || die "File not found $opt_f\n";
open(OUT,"> $opt_F") || die "File not found $opt_F\n";
# open(OUT,">$name") || die "Cannot open file $name : $!\n";

答案1

Getopt::Std: 你应该可以直接更换

require "getopts.pl";
&Getopts('F:f:');

use Getopt::Std;
getopts('F:f:');

如果您还使用use warnings;and use strict;(通常建议这样做),则需要事先使用 声明变量our ($opt_F, $opt_f);。或者,您可以使用哈希:

getopts('F:f:', \my %opts);
$opts{f} # instead of $opt_f
$opts{F} # instead of $opt_F

相关内容