How does Perl read in files, how does it tell it to advance to the next line in the text file, and how does it make it read all lines in the .txt file until, for example, it reaches item “banana”?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Basically, there are two ways of reading files:
For both ways you need to create a FILEHANDLE using the “open” command, like so:
Then you can either slurp the file by putting it into an array:
or read the file one by one using a while loop
Afterwards, don’t forget to close the file.
That’s just the basics.. there’s a lot to do with files, especially in Exception handling (what to do when the file does not exist, is not readable, is being written to), so you’ll have to read up or ask away 🙂