Ad space (banner)
Lesson 15 / 20

File input and output, the basics

This lesson covers the basic idea of reading and writing files in Perl.

The open function opens a file handle and close closes it. You can specify read mode (<), write mode (>), or append mode (>>).

Since actual file operations are not possible in the browser, this lesson only walks through the structure of the code. Perl convention is to write the error handling alongside, as in open(my $fh, '<', 'data.txt') or die "cannot open: $!";.

A common early stumble is leaving out error handling like or die. File operations can always fail, so it is worth making a habit of writing the failure path at the same time.

In professional work, Perl is still used widely for batch jobs that read log files, aggregate them, and generate reports — and file handling is at the centre of that.

๐Ÿ“– Reference code
โœ๏ธ Your code
Type your code, then press "Run"

๐Ÿงช This site can't compile or run Perl directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ€” nothing is sent anywhere).

Ad space (banner)
Ad space (in-article)