bangor has asked for the wisdom of the Perl Monks concerning the following question:

I'm useless at Windows and failing at passing in a file path to a script from the command line:
perl test.pl --file='test.txt' ... Could not open 'test.txt': No such file or directory at test.pl
Note the quotes around test.txt. I tried with the full path
perl test.pl --file='C:\scripts\test.txt' ... Could not open 'C:\scripts\test.txt': Invalid argument at test.pl
Script and file are in the same directory. Any help appreciated

Replies are listed 'Best First'.
Re: How to pass Windows filepaths on command line
by BrowserUk (Patriarch) on Aug 28, 2014 at 17:57 UTC

    Use double quotes (or no quotes if the path doesn't contain spaces) instead of single:perl test.pl --file="test.txt"


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: How to pass Windows filepaths on command line
by davido (Cardinal) on Aug 28, 2014 at 17:55 UTC

    Without code I can only guess what you are doing with @ARGV. I suspect you're using Getopt::Long, which is fine. If there's some other method of processing @ARGV that you're using we should probably know, because that may be relevant.

    That said, why don't you try perl test.pl --file="test.txt", and perl test.pl --file="C:\scripts\test.txt"? The shell will remove the double-quotes anyway, so unless there are special characters you want to preserve, the quoting isn't doing anything for you to begin with.

    Note the following:

    perl -e "print qq{<<$_>>\n} for @ARGV" file=test\test\test __OUTPUT__ <<file=test\test\test>>

    In other words, even without quotes, the backslashes are preserved.

    Now try the same one-liner with double-quotes around the path. The output will demonstrate that the shell is stripping them away. Single quotes don't have that syntactic meaning under Windows.


    Dave

Re: How to pass Windows filepaths on command line
by toolic (Bishop) on Aug 28, 2014 at 17:51 UTC
      I'm using Getopt::Lucid. Everything works fine on Unix.
      my $in_file = $opt->get_file; open(my $fh, "<:encoding(utf8)", $in_file) or die "Could not open $in_file: $!";
        Your error message has single quotes, but your code does not. Try without the single quotes:
        perl test.pl --file=test.txt
Re: How to pass Windows filepaths on command line
by bangor (Monk) on Aug 28, 2014 at 18:04 UTC
    Thanks everyone for the fast replies - removing the quotes worked!
      Beware spaces in your path. Quotes will be required.
Re: How to pass Windows filepaths on command line
by sandy105 (Scribe) on Aug 29, 2014 at 06:18 UTC

    usually i run the programs like perl test.pl "test.txt" if in same folder or full path if not ..