我有教授给出的perl脚本,该脚本很容易运行,但是我的机器出现错误,我不理解。因为该文件所在的目录中还有另一个名为file_in的文件,所以我创建了该文件,因为我认为这是运行该脚本所需的唯一操作。但这在第33行给了我一个错误。请帮助我。谢谢,
#!/usr/bin/perl
# the strict package forces you to declare each variable you use beforehand
use strict;
# a variable in strict mode is declared using my
# the $ symbol means it is a single-valued variable
# the @ symbol means it is an array
# each declaration/instruction is closed with a ; sign
my @par_list = (1,2,3,4,5,6,7,8,9,10);
# creating a variable for the current value of the parameter
my $value;
# get and store the size of the array
my $nbr_of_values = $#par_list;
# now, we read in a variable that will be the filename of the template input file
# $ARGV are the input arguments, 0 means it is the first one (perl starts counting at 0, not 1)
my $file_in = $ARGV[0];
# start of the loop
for( my $i=0; $i<= $nbr_of_values; $i++){
$value = $par_list[$i];
print "This is the current parameter value: $value \n";
# now we create a new string variable that will later be the filename of the new input deck
# the . symbol is the concatenation operator between strings
my $new_input_filename = $file_in."_".$value;
print " The new filename is $new_input_filename \n";
# open the template file and store its filehandle (fh_in)
open my $fh_in, '<', $file_in or die "Can't open output $file_in !";
# open the new file (it currently does not exist and is thus empty) and store its filehandle (fh_out)
open my $fh_out, '>', $new_input_filename or die "Can't open output $new_input_filename !";
while (<$fh_in>) {
# this is for you to see on the console, we read line-by-line, while there is something
# the line read is stored in a special PERL variable $_
print "I have read $_";
# now we actually print that line intot he new file
print $fh_out $_;
}
close $fh_in;
close fh_out;
}
print " I am done with this !!! \n";
exit 111;
该脚本不会打开file_in。$file_in
是应该传递给脚本的变量。注意my $file_in = $ARGV[0];
。
$ARGV[0]
是您必须传递给脚本的第一个命令行参数。
如果您在该目录中创建了一个文件并将其命名为“ file_in”,请运行 perl_part3.pl file_in
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句