有人可以帮我一个简单的perl脚本

时间:2016-10-12 04:17:09

标签: perl

我有一个教授给出的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;

1 个答案:

答案 0 :(得分:1)

脚本无法打开file_in。 $file_in是应该传递给脚本的变量。注意my $file_in = $ARGV[0];

$ARGV[0]是您必须传递给脚本的第一个命令行参数。

How do you use command line parameters

如果您已在该目录中创建了一个文件并将其命名为&#39; file_in&#39;,则运行perl_part3.pl file_in