in reply to Reading from a file
1. You don't check if the open() worked, and you're not passing it a full pathname to the file, are you sure it's found the file? Try using:
- the code will end (die) if the file cant be found or opened, including the '$!' will give you the system error message, such as 'cant find file' etc.open(file1, "sample.txt") || die "Can't open sample.txt ($!)";
2. You're using file1, whether the open actually worked or not. To get Perl to tell you if the file handle is actually readable, try putting 'use warnings;' at the top of your code.
Do you realise that you are trying to print the first line of the file, then assign the second line to $line1, and then print that? Each line is read only once from the file handle, the next access will get the following line..
C.
|
|---|