tags:

views:

132

answers:

2

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 file - $!\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?

+10  A: 

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)
Marcelo Cantos
+8  A: 

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.

David Dorward
or use: open( my $fh, '<', "$file_name")
Eric Eijkelenboom
Yes, but why would you want to do that? The quotes in `"$file_name"` are useless. Just plain `$file_name` is exactly equivalent and cleaner, to boot.
Dave Sherohman