in reply to creating a text file at run time and writing to it.

Here's how I would re-write that code:

use strict; use warnings; open my $name_fh, '<', 'name.txt' or die "Couldn't open name.txt.\n$!"; my $y = 0; while ( <$name_fh> ) { chomp; next unless $_; # Skip empty filenames. my $name = $_ . '.txt'; open my $out_fh, '>', $name or die "Couldn't open $name for output:\n$!"; print $out_fh "Hello\n"; $y++; close $out_fh or die "Couldn't close output file $name.\n$!"; } close $name_fh; # Don't need to check for errors closing input files. print "Created $y files.\n";

I've used lexical filehandles, avoided creating a list of filenames by simply dealing with the names one by one, checked for errors as needed. Note: I didn't validate the input. In other words, if your input file has garbage for filenames, you're going to get garbage filenames, or possibly illegal filenames. The only validation I've done is to skip empty filenames. Oh, and I also used strict and warnings as a best practice.


Dave