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


The follwing code works until I add 'use CGI'. Now I am getting the error:

No such file or directory at...
#Open the directory to list what's inside opendir(DIR, "c:\$directory") || die "Cannot open $directory $!"; foreach $name (sort readdir(DIR)) { #Prints names sorted print "$name\n"; } closedir(DIR);

Any help would be appreciated.

L.

Replies are listed 'Best First'.
Re: WinNT Opendir with CGI
by dws (Chancellor) on Jan 31, 2001 at 21:30 UTC
    Tilt that backslash over.
    opendir(DIR, "c:/$directory") ... ^
    As is, you're quoting '$'.

    For extra credit, explain what, if anything, this has to do with CGI.pm

Re: WinNT Opendir with CGI
by the_slycer (Chaplain) on Jan 31, 2001 at 21:29 UTC
    Hmm, this does not work for me WITHOUT cgi either. Looks like the problem is the "c:\$directory", I believe the single backslash is causing your problem. Try "c:/$directory" instead and you should be OK. (This made the script work for me)
Re: WinNT Opendir with CGI
by chromatic (Archbishop) on Feb 02, 2001 at 02:41 UTC
    If CGI.pm calls binmode on STDIN on your platform (and I will suspect that it does), Perl won't internally translate the \r\n sequence into \n. That's the point of binmode.

    In that case, chomp will remove the \n, but there will still be a trailing \r at the end of the filename.

    Your error message ought to print "Cannot open $directory" and then the contents of $!. Since you're only getting $!, if you've reported the entire error message, it indicates that Perl printed the first part of the error, hit the \r, moved back to the first column, and printed the second half:

    my ($one, $two) = qw( one two ); print "$one\r$two\n";
    The simple fix is to strip out any carriage returns with a transliteration: $directory =~ tr/\r//d;

    As a side note, I usually debug variables by printing them like print "Directory: ($directory)\n";. That helps make whitespace apparent.

      You may want to try this instead of chomp:

      $var=~tr/\n\r//d; $var=~s/^\s+//;

      --
      $Stalag99{"URL"}="http://stalag99.keenspace.com";

Re: WinNT Opendir with CGI
by loosid (Initiate) on Jan 31, 2001 at 21:49 UTC

    This is the entire prog so far. It doesn't have anything to do with CGI yet.. But even with the change I am still getting the same error.
    use CGI qw(:standard); #use Win32::ODBC; use strict; use win32; my ($directory); my ($name); print "enter in directory \n"; chomp ($directory = <STDIN>); print "directory is $directory\n"; #Open the directory to list what's inside opendir(DIR, "c:/$directory") || die "Cannot open $directory $!"; foreach $name (sort readdir(DIR)) { #Prints names sorted print "$name\n"; } closedir(DIR); print "$directory is the directory";

    again, thanks for any help.

    L
Re: WinNT Opendir with CGI
by the_slycer (Chaplain) on Jan 31, 2001 at 23:18 UTC
    This is very strange behaviour.. I've tried the same on my Win 2000 machine and I am facing the exact same results. However, on my Linux install I have no issues.

    I decided to take a look at CGI.pm on my W2k machine. It appears as though it has some problems with either CGI::Defaultclass or with setting binmode for main::STDIN. I uncommented 3 lines in the module (do a search for "if ($needs_binmode)") and the script works fine.

    Some monk with more experience than I can tell us why this is happening :-)

    Should this node maybe be moved to SOPW?
Re: WinNT Opendir with CGI
by dws (Chancellor) on Jan 31, 2001 at 21:32 UTC
    (delete)
Re: WinNT Opendir with CGI
by dws (Chancellor) on Jan 31, 2001 at 21:31 UTC
    (delete)