Reputation: 59
I am working on a project for school, and this class is very non-descriptive. I don't understand what is going on and why I cannot get a file to display.
#!/usr/bin/perl
use 5.010;
use strict;
use warnings;
print "\n+--------------------------+";
print "\n| The File Search & |";
print "\n| Display Tool |";
print "\n+--------------------------+";
print "\nPlease enter the file you would like to read (with full path): \n";
my $FILE = <STDIN>;
chomp $FILE;
sleep 1;
open (FILE, "$FILE");
print " Here is the secret information you seek supreme leader: \n";
print "==============================================================";
while ($FILE) {
chomp $FILE;
open $FILE;
}
I'm not entirely sure what I am doing wrong. I have tried all kinds of different combinations but it only ever leads to errors.
So I made some revisions to the code per everyone's suggestions (see below). After running the script it returns the following error
readline() on unopened filehandle at test.pl line 30.
In my code below, I think I opened the filehandle but this error is making me think I messed something up with it somewhere near the open command.
#!/usr/bin/perl
use 5.010;
use strict;
use warnings;
print "\n+--------------------------+";
print "\n| The File Search & |"; #Not so fancy banner!
print "\n| Display Tool |";
print "\n+--------------------------+";
print "\n Welcome to the Tool. It is still being tested!!\n";
print "\n Please enter the file you would like to read: ";
my $filename = <STDIN>; #define the variable for the filename
if ( not -f $filename ) #This checks the validity of the file
{
print "Filename does not exist\n";
exit;
}
sleep 1;
print " Here is the secret information you seek supreme leader: \n";
print "==============================================================\n";
open my $filehandle, "<", $filename; #or die $!
while ( <$filename> ) { #While loop to read each line of the file
chomp;
print "$_\n";
}
I commented out the or die portion of the open string because it was just killing the script and I wanted to see what was wrong. I am a little confused about the "<", that is in the open statement. @Borodin
Upvotes: 2
Views: 622
Reputation: 126722
You are just getting your variables mixed up. You are using $FILE
and FILE
, and (invisibly) $_
. $FILE
starts off as the name of the file, but you also read from it, chomp
it and open
it which is wrong
You should open
file names, read from file handles, and print
what you have read (or any other value). And it is best to use lower case for all local variables and reserve capitals for globals
Your code should look like this. I have used the lexical file handle $fh
instead of the old-fashioned global one. Putting variables in quotes like "$FILE"
is wrong: at best it makes no difference, but it may break your program. Also, <$fh>
in a while
loop like that reads into the default variable $_
, which is also the default parameter for chomp
. Think of it as the pronoun it in English
open my $fh, '<', $file_name;
while ( <$fh> ) {
chomp;
print "$_\n";
}
Upvotes: 3
Reputation: 53478
The core problem here is that you're re-opening, not actually reading.
To open a file:
open ( my $filehandle, "<", $filename ) or die $!;
The die
is quite important, because it'll tell you if there's a problem opening the file. $!
is the 'status code' of the open operation, so it'll tell you if the file couldn't be found.
But all this does is 'set' $filehandle
to give you an access point.
To get the contents of the file, you need to read it, and in perl
this is done with <>
.
So:
my $line = <$filehandle>
Will read a single line.
If you want to read more than a single line, then you do it in a while loop.
while ( my $line = <$filehandle> ) {
print $line;
}
You might also find it useful to test whether a file exists before doing this:
if ( not -f $filename ) {
print "$filename does not exist\n";
exit;
}
Upvotes: 2
Reputation: 530853
Two issues: one, you read from FILE
the same way you read from STDIN
, with the <>
operator. Two, assuming you want to display the contents of $FILE
, your while loop should look like
while (<FILE>) {
print;
}
This uses the implicit variable $_
to store the line read from FILE
each time through the loop, which is the same variable print
uses with no other argument. More explicitly, you might write
while (my $line = <FILE>) {
print "$line";
}
chomp
isn't needed, since print
won't print an additional newline unless you tell it to. You could also write
while (<FILE>) {
chomp;
print "$_\n";
}
or
while (my $line = <FILE>) {
chomp $line;
print "$line\n"
}
Upvotes: 1