J. Doe J. Doe - 1 month ago 6
Perl Question

Can someone help me with a simple perl script

I have a perl script that professor gave which is suppose to be easy to run, but my machine is giving error, which i don't understand.
since the directory this file is in has another file named file_in which i created because i think that's the only thing i need to do in order to run this script. But it's giving me an error at line 33. Please help me .
Thank you,

#!/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;

Answer

The script won't open file_in. $file_in is a variable that should be passed to the script. Note my $file_in = $ARGV[0];.

$ARGV[0] is the first command line argument that you have to pass to the script.

How do you use command line parameters

If you have created a file in that directory and called it 'file_in', then run perl_part3.pl file_in