开发者

Why can't Perl's open find my file?

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜