Example 8: File input

Overview

In this example, you will learn how to read to a file

Example Code

Output

print the lines in "filename.txt" to the screen

 

Decode

>>Line 3: open(MYINPUTFILE, "<filename.txt");

opens the file called "filename.txt". Make sure that you have this file saved in the same folder as your perl file before you run it.

>>Line 6: while(<MYINPUTFILE>)

This command will read the file line by line until end of line

>>Line 9: $line = $_;

store $_ value because subsequent operations may change it.

>>Line 12: chomp($line);

strip the trailing newline from the line.

 

Exercise Eight

>> Read the file you created in Exercise Six and print out the content in that file