user1987607 user1987607 - 2 months ago 9x
Perl Question

perl getopt::Long path to file

I'm trying to open an input file as an argument using the Getopt::Long module

This is the beginning of my script

#! /usr/bin/perl -s
use strict;
use warnings;

use Data::Dumper;
local $Data::Dumper::Useqq = 1;
use Getopt::Long qw(GetOptions);;

my $input='';

GetOptions('input|in=s' => \$input);

open(my $table1,'<', $input) or die "$! - [$input]"; #input file

And this is how I launch the script

$ -in /path/to/file.txt

I get as output:

No such file or directory - [] at line 13.

Line 13 is the line with

Is there a mistake in the script?


You are using Perl's built-in option parsing with the -s in your shebang line. In a command like -in /path/to/file.txt that makes perl set the variable $in to 1 and remove the corresponding entry from @ARGV before Getopt::Long ever sees it

Just remove -s from the shebang line and everything it will work for you