I am new to Perl coding. I am facing a problem while executing a small script I have: open is not able to find the file which I am giving as an argument.
The file is available:
ls -l DLmissing_months.sql
-rwxr-xr-x 1 tlmwrk61 aimsys 2842 May 16 09:44 DLmissing_months.sql
My Perl script:
#!/usr/local/bin/perl
use strict;
use warnings;
my $this_line = "";
my $do_next = 0;
my $file_name = $ARGV[0];
open( my $fh, '<', '$file_name')
or die "Error opening fil开发者_StackOverflowe - $!\n";
close($fh);
executing the Perl script :
> new.pl DLmissing_months.sql
Error opening file - No such file or directory
What is the problem with my Perl script?
Single quotes don't interpolate. Instead of opening DLmissing_months.sql
, you are trying to open a file called $file_name
. Use double quotes to make things interpolate. In this case, you can just use the variable on its own:
open( my $fh, '<', $file_name)
You've put $file_name
inside single quotes. Single quotes are non-interpolating, so you are trying to read a file called "$file_name", not a file called "whatever is stored in the $file_name scalar".
Remove the quotes.
精彩评论