if ( scalar @matches ) { make_path($parsed_dir); open my $out_fh, '>', "$parsed_dir/${basename}.$wanted.$rnumber.txt" or die $!; print $out_fh $_ for @matches; print $out_fh "\n"; close $out_fh; }