Everything is correct here, you use single quotes in your call to open . Single quotes do not interpolate variables into a quoted string. Double quotes do.
my $foo = 'cat'; print 'Why does the dog chase the $foo?';
So far so good. But others have neglected to give you some important tips on open .
The open function has evolved over the years, as has the way Perl works with file descriptors. In the old days, open was always called with the mode and file name combined in the second argument. The first argument has always been a global file descriptor.
Experience has shown that this was a bad idea. Combining mode and file name into one argument created security issues. Using global variables, well, uses global variables.
Since Perl 5.6.0, you can use the 3-dimensional open form, which is much more secure, and you can save your file descriptor in lexically scanned parentheses.
open my $fh, '>', $file or die "Can't open $file - $!\n"; print $fh "Goes into the file\n";
There are many nice things about lexical file descriptors, but one excellent property is that they automatically close when their refcount drops to 0 and they are destroyed. There is no need to explicitly close them.
Something else worth noting is that most Perl communities feel that strict and warnings pragmas are recommended. Using them helps to catch many errors at an early stage of the development process and can be a huge time saver.
use strict; use warnings; for my $base ( 10_001..10_003 ) { my $file = "$base.txt"; print "file: $file\n"; open my $fh,'>', $file or die "Can't open the output file: $!";
I simplified your code too. I used the range operator to generate your base numbers for file names. Since we are working with numbers, not strings, I was able to use _ as a thousands separator to improve readability without affecting the final result. Finally, I used the idiomatic perl for loop instead of the C style you had.
I hope you find this helpful.