WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

Perl · Basic · question 9 of 100

How do you read and write data from/to a file in Perl?

📕 Buy this interview preparation book: 100 Perl questions & answers — PDF + EPUB for $5

In Perl, you can read and write data from/to a file using filehandles. A filehandle is a reference to an opened file, and it is usually represented by a variable name that starts with a dollar sign followed by a capital letter.

To open a file for reading or writing, you can use the ‘open()‘ function. It takes two arguments: the first argument is the filehandle, and the second argument is the name of the file. For example, to open a file named "input.txt" for reading, you can use the following code:

my $fh;
open($fh, '<', 'input.txt') or die "Cannot open file input.txt: $!";

In this example, the first argument is ‘$fh‘, which is the filehandle for the opened file. The second argument is the file mode, which is ‘’<’‘ for reading. The third argument is the name of the file we want to open.

If the file cannot be opened, the ‘open()‘ function will return ‘undef‘, and the ‘die()‘ function will print an error message and terminate the program.

To read data from the file, you can use various functions such as ‘read()‘, ‘sysread()‘, ‘getc()‘, ‘readline()‘, ‘<FILEHANDLE>‘, etc. Here’s an example that reads the contents of the file line by line and prints them to the screen:

while (my $line = <$fh>) {
    print $line;
}

In this example, the ‘<$fh>‘ syntax is equivalent to the ‘readline()‘ function. It reads one line from the filehandle and returns it as a string. The ‘while‘ loop reads all the lines in the file until the end of file (EOF) is reached.

To write data to a file, you can use various functions such as ‘print()‘, ‘printf()‘, ‘syswrite()‘, etc. Here’s an example that writes some text to a file named "output.txt":

my $out_fh;
open($out_fh, '>', 'output.txt') or die "Cannot create file output.txt: $!";
print $out_fh "Hello world!n";
close $out_fh;

In this example, the second argument to ‘open()‘ is ‘’>’‘, which means the file is opened for writing. The ‘print()‘ function writes the string "Hello world!
n" to the file. The ‘close()‘ function is called to close the filehandle and save the changes to the file.

Note that you should always check whether the file was opened successfully, whether the read/write operations were successful, and whether the file was closed correctly. Also, you should always use the appropriate file modes (‘<’‘, ‘’>’‘, ‘’>>’‘, etc.) depending on whether you want to read, write, or append to the file.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Perl interview — then scores it.
📞 Practice Perl — free 15 min
📕 Buy this interview preparation book: 100 Perl questions & answers — PDF + EPUB for $5

All 100 Perl questions · All topics