How can I get Perl to open/make a .txt file using $input for the file name?

tim85a

New member
I want to get perl to make the file using $input as the name so if 'name' was saved as $input the file would be called 'name.txt' I've got it like this at the mo:

open FILE, ">'$input'.txt" or die $!;

but its not working :( anyone got any ideas?
 
In Perl, the command line arguments are stored in the @ARGV array. $#ARGV can get the number of arguments.

#!/usr/bin/perl
if ($#ARGV == 0) {
die("Not enough arguments!");
}

$input = shift @ARGV;
open FILE, ">$input.txt" or die("Can't open input file: $!");
 
In Perl, the command line arguments are stored in the @ARGV array. $#ARGV can get the number of arguments.

#!/usr/bin/perl
if ($#ARGV == 0) {
die("Not enough arguments!");
}

$input = shift @ARGV;
open FILE, ">$input.txt" or die("Can't open input file: $!");
 
Back
Top