Why can't Perl open my file?

I am new to Perl coding. I ran into a problem while running a small script with me: open cannot find the file that I give as an argument.

File 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 file - $!\n";
close($fh);

Perl script execution:

> new.pl DLmissing_months.sql
Error opening file - No such file or directory

What is the problem with my Perl script?

+3
source share
2 answers

Single quotes are not interpolated. Instead of opening, DLmissing_months.sqlyou are trying to open a file with a name $file_name. Use double quotes to interpolate things. In this case, you can simply use the variable yourself:

open( my $fh, '<', $file_name)
+11
source

$file_name . , "$ file_name", ", $file_name".

.

+8

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


All Articles