Simple opendir(), directory does not exist problem

T

theo22

Newbie question:
Windows 2000, Perl v5.8.0 built for MSWin32-x86-multi-thread

All I want to do is pass an argument to my script on the command line.
The argument is the directory that gets passed to the opendir()
function. When I run the script , I always get an error:
Uncaught exception from user code:
Directory D:\My Documents\ScriptTesting does not exist: No
directory at D:\My Documents\ScriptTesting\GetDirList3.pl line 12.

How can it say the directory does not exist if the file that it is
referring to with the error, is in that directory? If I hard code the
directory in the opendir() function, it works fine.

Here is my code. You will notice it takes a directory and then spits
it out to a .txt file. It works great except I can't pass the
directory I want it to list through the command line:
******************************************
#!/usr/bin/perl -w

use strict;
use diagnostics;

#Tried a variable name for ARGV[0] and chomp as alternative, but it
made no difference so I commented it all out.
#my $DirPath=$ARGV[0];
#chomp($DirPath);

#Create directory handle so we can get a list of all the files in the
directory.
#opendir(MyDir, '$DirPath') || die "Directory $DirPath does not exist:
$!";
opendir(MyDir, '$ARGV[0]') || die "Directory $ARGV[0] does not exist:
$!";

#Create filehandle to dump list of directory files into a .txt file
for reading.
open(MyFile, ">My_Files.txt") || die "Cannot open My_Files.txt: $!";

#Gets the list of only .txt files
my @Files = grep (/\.txt$/, readdir MyDir);

#Print each file in the directory to a .txt file. Prepend filenames
with thier full path.
foreach my $Files (@Files){
$Files = "$ARGV[0]\\$Files";
print MyFile "$Files\n";
}

print "My_Files.txt file created with list of all .txt files!\n";

#Close directory and file handles.
closedir(MyDir);
close(MyFile);
****************************************

Please Help. Here is the command that I used:
C:>GetDirList3.pl "D:\My Documents\ScriptTesting"

What am I doing wrong here?

Thx!!
 
W

Walter Roberson

:Windows 2000, Perl v5.8.0 built for MSWin32-x86-multi-thread

:The argument is the directory that gets passed to the opendir()
:function. When I run the script , I always get an error:
:Uncaught exception from user code:
: Directory D:\My Documents\ScriptTesting does not exist: No
:directory at D:\My Documents\ScriptTesting\GetDirList3.pl line 12.

:eek:pendir(MyDir, '$ARGV[0]') || die "Directory $ARGV[0] does not exist:
:$!";

Single-quotes don't interpolate, but double-quotes do. So you
are trying to open a directory literally named $ARGV[0] and
then complaining with a message that uses the -contents- of $ARGV[0]

I suggest you just remove the single-quotes.

If you choose to double-quote instead, then keep in mind that
inside double-quotes, the backslashes in the string you provide
are going to be interpreted as escape sequences. To use double-quotes
you would have to quote-meta or use "\Q$ARGV[0]\E"
 
A

A. Sinan Unur

(e-mail address removed) (theo22) wrote in @posting.google.com:

Please Help. Here is the command that I used:
C:>GetDirList3.pl "D:\My Documents\ScriptTesting"

What am I doing wrong here?

I did not look at the rest of your script, but you might want to try the
following alternatives:

C:>GetDirList3.pl "D:/My Documents/ScriptTesting"

or

C:>GetDirList3.pl "D:\\My Documents\\ScriptTesting"

Sinan.
 
B

Ben Morrow

opendir(MyDir, '$ARGV[0]') || die "Directory $ARGV[0] does not exist:
$!";

This attempts to open a directory called $ARGV[0]. The double-quotes
used for the die message interpolate the variable, those single quotes
don't. You don't need to quote it at all; also, it is more usual to
use uppercase for IO handles:

opendir my $DIR, $ARGV[0] or die "Cannot open $ARGV[0]: $!";

Ben
 
B

Ben Morrow

If you choose to double-quote instead, then keep in mind that
inside double-quotes, the backslashes in the string you provide
are going to be interpreted as escape sequences. To use double-quotes
you would have to quote-meta or use "\Q$ARGV[0]\E"

Bzzzt! Wrong.

A double quoted string != a regex.

Ben
 
T

Tore Aursand

#opendir(MyDir, '$DirPath') || die "Directory $DirPath does not exist:
$!";
opendir(MyDir, '$ARGV[0]') || die "Directory $ARGV[0] does not exist:
$!";

You need double quotes when dealing with variables, or - in this case - no
quotes at all;

opendir(MYDIR, $DirPath) or die "$!\n";
opendir(MYDIR, $ARGV[0]) or die "$!\n";

Try this script to see the difference;

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

my $variable = 'test';
print '$variable' . "\n";
print "$variable" . "\n";
print $variable . "\n";
 
U

Uri Guttman

BM> [email protected] (Walter Roberson) said:
If you choose to double-quote instead, then keep in mind that
inside double-quotes, the backslashes in the string you provide
are going to be interpreted as escape sequences. To use double-quotes
you would have to quote-meta or use "\Q$ARGV[0]\E"

BM> Bzzzt! Wrong.

BM> A double quoted string != a regex.

bzzzt! escape sequences exist in double quotish strings too. what do you
call \n, \r, \t, etc?

uri
 
B

Ben Morrow

Uri Guttman said:
BM> [email protected] (Walter Roberson) said:
If you choose to double-quote instead, then keep in mind that
inside double-quotes, the backslashes in the string you provide
are going to be interpreted as escape sequences. To use double-quotes
you would have to quote-meta or use "\Q$ARGV[0]\E"

BM> Bzzzt! Wrong.

BM> A double quoted string != a regex.

bzzzt! escape sequences exist in double quotish strings too. what do you
call \n, \r, \t, etc?

Yes, of course. However,

my $x = '\n\r\t';
my $y = "$x";

will not (as Walter suggested) give a string with any control
characters in it. \Q$var\E *is* useful in regexen, as those escape
sequences which are regexish rather than double-quoteish will be
interpreted even if they came in from a variable.

Ben
 
U

Uri Guttman

BM> [email protected] (Walter Roberson) said:
If you choose to double-quote instead, then keep in mind that
inside double-quotes, the backslashes in the string you provide
are going to be interpreted as escape sequences. To use double-quotes
you would have to quote-meta or use "\Q$ARGV[0]\E"

BM> my $x = '\n\r\t';
BM> my $y = "$x";

BM> will not (as Walter suggested) give a string with any control
BM> characters in it. \Q$var\E *is* useful in regexen, as those escape
BM> sequences which are regexish rather than double-quoteish will be
BM> interpreted even if they came in from a variable.

i agree walter said the wrong thing there. data is never interpolated,
only literal \ and $ and such.

uri
 
T

theo22

Thank you all very much!!

It was the single quote around my first use of ARGV[0] that screwed it
up. When I removed it, it worked fine. I didn't notice that I didn't
have any quotes around the second use of ARGV[0], which is why it was
reporting the correct directory in the error message and confused me
even more.

Should be:
opendir(MyDir, $ARGV[0]) || die "Directory $ARGV[0] does not exist:
$!";

NOT:
opendir(MyDir, '$ARGV[0]') || die "Directory $ARGV[0] does not exist:
$!";

By the way, this command line entry worked with this solution:
C:>GetDirList3.pl "D:\My Documents\ScriptTesting"


Ben: I also changed my IO handles to all uppercase. Thank you!

Tore: I ran your little test. It is somewhat more clear, however I
don't see a difference between the double quotes and no quotes. The
output is the same. Also, what is the period used for in that
statement? I tried to take it out and the script of course failed. I
guess it is the concatanation operator and it is combining the two
arguments to the print statement? Hmmm... I've never seen that.
Nice.
 
T

Tore Aursand

Tore: I ran your little test. It is somewhat more clear, however I
don't see a difference between the double quotes and no quotes. The
output is the same.

There is not difference, except that I guess Perl will _try_ to
interpolate the value stored inside the double quotes. The lesson learned
should be: Use double quotes when you _need_ to interpolate something. If
you don't need to, don't use double quotes. If you're dealing with a
variable, don't use _any_ quotes. IMO.
Also, what is the period used for in that statement? I tried to take it
out and the script of course failed. I guess it is the concatanation
operator and it is combining the two arguments to the print statement?

You're right - it's the concatanation (?) operator. I almost constantly
use it because of personal style;

print "$variable\n";
print $variable . "\n";

The two lines above does excactly the same, but because my editor
highlights everything inside double and single quotes, I prefer to keep
variable names outside of quotes when possible.
 
T

Tad McClellan

theo22 said:


Please form proper followups.

If you have something to say about Tore's post, then say it in
a followup to Tore's post.

I ran your little test. It is somewhat more clear, however I
don't see a difference between the double quotes and no quotes.


I don't see any code at all, so it is unclear what are referring to.

The
output is the same.


The output was the same this time, it could be different other times,
as the Perl FAQ points out (assuming that the "quotes" you are
referring to are like in the FAQ):

What's wrong with always quoting "$vars"?


Also, what is the period used for in that
statement?


What period?

What statement?
 
A

Anno Siegel

[...]
You're right - it's the concatanation (?) operator. I almost constantly
use it because of personal style;

print "$variable\n";
print $variable . "\n";

The two lines above does excactly the same, but because my editor
highlights everything inside double and single quotes, I prefer to keep
variable names outside of quotes when possible.

This is one thing that bothers me about so-called syntax hilighters.
They all have limitations and get some things wrong. In the long run,
there will be a tendency to adapt your style to suit your syntax
hilighter's quirks. That is not a solid basis for a decision about
programming style.

Anno
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top