use strict; use warnings; # here the code where the $hashref is populated (or maybe it is actually part of a hash of hashes or array of hashes, we don't know enough details) open my $FH_OUTFILE, ">", "output.txt" of die "could not open the output file $!"; my $price = $hashref->{price}; my $item_name = $hashref->{title}; if ($price >= 55 and $item_name !~ /electric/) { print $FH_OUTFILE "free_batteries"; } print $FH_OUTFILE "\n"; # perhaps some code to close $FH_OUTFILE (if you're done at this point)