Open a file for reading and process it line by line:

use strict; use warnings;

open(my $fh, '<', 'data.txt') or die "Cannot open data.txt: $!";
while (my $line = <$fh>) {
    chomp $line;          # remove trailing newline
    next if $line =~ /^#/;  # skip comment lines
    print "Line: $line\n";
}
close $fh;

Write to a file with the > mode (overwrites) or >> (appends):

open(my $out, '>', 'output.txt') or die "Cannot write: $!";
print $out "First line\n";
print $out "Second line\n";
close $out;

# Append to a log file
open(my $log, '>>', 'app.log') or die "Cannot open log: $!";
printf $log "[%s] %s\n", scalar localtime, "Application started";
close $log;

Slurp an entire file into a variable or an array of lines:

# Slurp into a scalar
my $content = do { local $/; open my $f, '<', 'file.txt'; <$f> };

# Read all lines into an array
open my $fh, '<', 'file.txt' or die $!;
my @lines = <$fh>;
chomp @lines;
close $fh;