Without being able to read dynamic inputs from the user, or to provide the processing results as output either to the screen or to a file, then why… what’s all this for?
Reading from Standard Input
We have already seen how to use the <stdin> operator to prompt the user for a line of input. The entered line can be assigned to a variable, and in most cases has its trailing newline character removed using the chomp function.
Writing to Standard Output
So far, we have used the print function to write output to the standard output (screen). The print function doesn’t insert a newline at the end of the printed output, so you have to explicitly include it if the output in hand should be separated from the next one by a newline.
Using the say Function
The say function is newly introduced in Perl version 5.10. It works exactly like the print function, with one interesting difference that it implicitly appends a newline.
Print the letters from A to Z in a separate line.
When executed, the above script will print a line containing letters from A to Z
Notice the use of the statement:
As the say function is introduced in Perl version 5.10, the use statement causes Perl to use (import) the features of version 5.10. If you forget to use it, the Perl interpreter will fire the following error:
Undefined subroutine &main::say called at ./use_say.pl line 3.
For a Perl program to perform any I/O operation, a special channel is defined and open for that purpose between the program and the other party (could be standard input, standard output, file, external command, etc.). Such channel is called A File Handle. A file handle could be …
– Wait. What do you say? We have been doing input and output operations to standard input and standard output (respectively) without even caring about those you call file handles!!
Mmm, actually, you did! You have even used and specified one of those file handles in some of your scripts. Don’t be astonished; Perl actually has a set of special file handles that are ready for use: STDIN, STDOUT, STDERR, …
– Wait, is that the stdin we used in ?
Exactly. Additionally, when you write something to the standard output using either print or say, the Perl automatically (behind the scene) uses the stdout file handle for you.
So, the statement:
print ("Hello, I am a Perl Programmer\n");
print STDOUT ("Hello, I am a Perl Programmer\n");
Have you got the idea? Great! Let’s now build on it.
Reading from and Writing to files
The ability to read from and write to files is of great importance. If you are a UNIX or Linux guy, one of the basic facts that you should be aware of is that “Everything in UNIX is a file”. Anything and everything. So, knowing how to work with files is vital for your success as a Perl developer. In this section, we are going to tackle this topic. So, here we go.
The open Function
We have already agreed that when you need to perform any I/O operation, we need to open a file handle for it. Fortunately, Perl was nice enough to do that for us in cases of reading from STDIN, and writing to STDOUT and STDERR. But, when it comes to files, you should manage to open a handle for each file you need to input or output to. So, say Hi to the open function.
open (FILEHANDLE, MODE, FILENAME);
FILEHANDLE is the file handle for this I/O operation. It could be any valid Perl identifier; preferably in uppercase.
MODE is the opening mode of the file. In other words, it is the purpose for which the file opened. Four modes are available:
- Reading <
- Writing >
- Appending >>
- Both reading and writing +< or +>
FILENAME is a relative or full path of the file to open.
We need to read the contents of the /etc/passwd file line by line, and store each line as an element of the array @passwd
Consider the following script:
@passwd = ();
open (PASSWDFILE, '<', "/etc/passwd");
push (@passwd, $_);
Let’s execute it and see what we get:
Now, let’s explain things:
- The statement @passwd = (); initializes the empty array @passwd.
- The statement:
open (PASSWDFILE, ‘<‘, “/etc/passwd”);
opens a file handle PASSWDFILE to the file /etc/passwd in reading mode.
- The expression:
inside the while loop heading uses the operator <> to read one line from the open connection (file handle) PASSWDFILE (which is a connection to the file /etc/passwd). The line read is stored in the default variable $_
- The push function appends the default variable $_ as a new element in the @passwd array.
- When the end of file is reached andthe while loop condition is not met anymore, the loop will exit, and the array contents are printed to the screen.
- The <stdin> operator is used for reading a line of input from standard input.
- stdin, stdout, and stderr are three file handles that Perl opens for you automatically.
- A file handle is a connection between the Perl program and whatever party the program needs to read or write to.
- The say function works exactly like the print function, with one difference that it implicitly appends a newline at the end.
- The open function opens a file handle to a file for either reading, writing, appending, or both reading and writing.
The next article will present more examples for writing to a file, appending to a file, and opening a file for both reading and writing operations. See you.