How does Perl handle file input/output operations?

Perl provides a variety of built-in functions and operators for handling file input/output operations. The two most commonly used functions are "open" and "print".

The "open" function is used to open a file for reading or writing. The first argument to the "open" function is the name of the file, and the second argument is the mode in which the file should be opened. For example, "open(my $fh, "<", "file.txt")" opens the file "file.txt" for reading, while "open(my $fh, ">", "file.txt")" opens the file "file.txt" for writing.

Once a file is open, the "print" function can be used to write data to the file. For example:

open(my $fh, ">", "file.txt"); print $fh "Hello World!\n"; close $fh;

This will write the string "Hello World!" to the file "file.txt". To read from a file you can use the "<" operator on the filehandle. For example:
open(my $fh, "<", "file.txt"); my $line = <$fh>; print "$line\n"; close $fh;

This will read the first line of the file "file.txt" and print it.

Additionally, Perl also provides functions like readline(), read(), sysread() and getc() which are useful in different scenarios like reading a line at a time, reading a specific number of bytes or reading a single character respectively.

Perl also provides the -e operator which can be used to check if a file exists and the -s operator which can be used to check the size of a file.

In summary, Perl provides a variety of built-in functions and operators for handling file input/output operations, including open, print, "<" operator and many more. These functions and operators make it easy to read from and write to files in a variety of ways.