# Load old list.
my @sets = do {
open(my $fh, '<', $file)
or die("Unable to open set list \"$file\": $!\n");
local $/ = '
';
<$fh>
};
# Add new item to the top.
unshift @sets, "a new set - line 7
";
# Keep the top 5.
if (@sets > 5) {
@sets = @sets[0..4];
}
# Save new list.
{
open(my $fh, '>', $file)
or die("Unable to create set list \"$file\": $!\n");
print $fh @sets;
}