How to create a new file using a variable value as a name in Perl?

For instance:

$variable = "10000"; for($i=0; $i<3;$i++) { $variable++; $file = $variable."."."txt"; open output,'>$file' or die "Can't open the output file!"; } 

This does not work. Please suggest a new way.

+4
source share
6 answers

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?'; # prints: Why does the dog chase the $foo? print "Why does the dog chase the $foo?"; # prints: Why does the dog chase the cat? 

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: $!"; # Do stuff with handle. } 

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.

+17
source

use double quotes: "> $ file". single quotes will not interpolate your variable.

 $variable = "10000"; for($i=0; $i<3;$i++) { $variable++; $file = $variable."."."txt"; print "file: $file\n"; open $output,">$file" or die "Can't open the output file!"; close($output); } 
+5
source

The problem is that you are using single quotes for the second open argument, and single-quoted strings do not interpolate the variables specified in them . Perl interpreted your code as if you wanted to open a file that really had a dollar sign for the first character of its name. (Check your drive, you will see an empty file called $ file.)

You can avoid the problem by using the three-parameter version of open :

 open output, '>', $file 

Then, the file name argument cannot accidentally interfere with the open-mode argument, and there is no unnecessary interpolation or concatenation variable.

+2
source

Use the file descriptor:

 my $file = "whatevernameyouwant"; open (MYFILE, ">>$file"); print MYFILE "Bob\n"; close (MYFILE); 

print '$ file' gives $ file, while print "$ file" gives anynameyouant.

0
source

You have almost everything right, but there are a few problems.

1 - You need to use double quotes around the file you open. open output, "> $ file" or die [...] 2 - Minor errors, you do not close files afterwards.

I would rewrite your code like this:

 #!/usr/bin/perl $variable = "1000"; for($i=0; $i<3;$i++) { $variable++; $file = $variable."."."txt"; open output,">$file" or die "Can't open the output file!"; } 
0
source
 $variable = "10000"; for($i=0; $i<3;$i++) { $variable++; $file = $variable . 'txt'; open output,'>$file' or die "Can't open the output file!"; } 

it works 1.txt 2.txt etc.

0
source

Source: https://habr.com/ru/post/1304336/


All Articles