Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

spot the opendir bug in my code

by mandog (Curate)
on Nov 20, 2001 at 21:44 UTC ( [id://126561]=perlmeditation: print w/replies, xml ) Need Help??

update:Changed title at suggestion of beatnik

I re-encountered this the other day The following code pukes why?:

01: #!/usr/local/bin/perl -wT 03: open(FH,'data.txt') 04: or die "couldn't open file $!\n"; 06: my $data_dir=<FH>; 07: chomp($data_dir); 08: close(FH) 09: or die "couldn't close file $!"; 10: opendir(DH,$data_dir) 11: or die "couldn't open #$data_dir# $!\n"; 12: my @files=readdir(DH) 13: or die "couldn't read dir\n"; 14: print join("\n",@files);
Select the hints to view

HINT (contents of data.txt)
The data.txt contained a single line:
../
HINT (exact error message)
# No such file or directory
HINT (How the code was created)
All files were created in win2K notepad and ftp'd up to a LINUX box with DreamWeaver's check-in mechanism.
HINT (error message after removing a line)
After removing line 07 the error message became:
couldn't open #../ # No such file or directory
HINT (How the problem will be avoided in the future)
The following line was inserted between lines 07 and 08
$data_dir=~s/\r//g;
EXPLAINATION
Microsoft ends lines with a \r\n a carrage return and newline or \r\n. UNIX ends them with a \n

When I read a line of text from my file, I accedently included a carrage return which became part of the directory name I was trying to open.

../\r is not a directory that exists on my LINUX system.

Replies are listed 'Best First'.
Re: spot the misnamed opendir bug
by Beatnik (Parson) on Nov 20, 2001 at 21:59 UTC
    It's not an opendir bug, merely yar regula' ole' line end chars. If you'd uploaded with something other than Dreamweaver and in ASCII mode, chances are you'd have less problems. Much of those platform thingies are described in perlport.
    And yes, I suggest naming it: a bug in my code when I was using opendir since that could've happened to any I/O function, not opendir specifically.


    Greetz
    Beatnik
    ... Quidquid perl dictum sit, altum viditur.
      Um, it wouldn't have happened with open():
      open F, "Quen-Eng.rtf\r" and print -s F"; # works for me

      _____________________________________________________
      Jeff[japhy]Pinyan: Perl, regex, and perl hacker.
      s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlmeditation [id://126561]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (3)
As of 2024-04-24 22:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found