I am trying to write a code which asks for a file (if the first time, an invalid filename is given, it asks for file 5 times until exhausting), then it checks if the file is in fasta format.
how to code that? I have the following code so far.
#!/usr/bin/perl -w
#A program that asks for a file, opens it if file exists and check
#if the file is in FASTA format
use strict;
#get data from a file
my @file = openfile();
#open file
#subroutines
sub openfile {
my $filename;
my $x;
my $datafile;
my $file;
for ($x = 0; $x<5; $x++) {
print "\n\nPlease enter file name: ";
chomp ($filename = <STDIN>);
if (-e $filename) {
print "File found!\n\n";
exit;
} else {
if ($x<4) {
print "Invalid file name!\n\n";
} else {
print "Five tries were unsuccessful! Please check and try again!\n\n";
}
}
}
return;
}
Thanks!!! I will try it out!