Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re^3: readdir return nul string

by Aim9b (Monk)
on Sep 26, 2007 at 18:50 UTC ( [id://641215]=note: print w/replies, xml ) Need Help??


in reply to Re^2: readdir return nul string
in thread readdir return nul string

Fletch, thanks for the tips. Can you explain a little further, the whys & wherefores? The examples I used for the open were files, not dirs. Is that the difference. Files use || & dirs use 'or', or is it something else?
If I'm building a list of files to open, I wouldn't think I would EVER want the newline there. I used the chomp over the chop for just that reason, so I wouldn't lose a significant character of the name.

Thanks.

Replies are listed 'Best First'.
Re^4: readdir return nul string
by Fletch (Bishop) on Sep 26, 2007 at 19:32 UTC

    It's a matter of precedence: || binds "tighter" than or so you've in effect written opendir( FOO, ( "." || die "..." ) ). The die is silently optimized away since it can never be reached (since the constant "." will never be false). See perlsyn for more details.

    As for newlines, while it's not common there's nothing (at least on POSIX systems; I'm sure someone will chime in about Wintendo land . . .) that prevents you from having a newline character in a filename. You can make one yourself with perl -e 'open( my $f, ">", "foo\n" )' (and then when you do an ls you'll see something like foo?, or foo\n if your ls supports the -b flag).

    At any rate, my point was that in the majority of cases you're not going to have a newline on the end of a filename so calling chomp is wasted effort. In the rare case where some joker did go and put a newline (or some other wonky character) into a filename if you muck with it to remove newlines or what not you no longer have a the real filename (the strings "foo" and "foo\n" are two different things because whitespace is significant in filenames; attempting to open the later by giving it the former isn't going to work).

    (And to save everyone who blithely tries my open example above frantic googling or indiscriminate rm invocations, perl -e 'unlink( "foo\n" ) will clean things up :)

    Update: Minor tweaks to wording of 2nd para.

      Fletch, thank you so much for the great explanation and for taking the time to elaborate. I'm still learning perl and this is just plain good info. I don't think the wasted time via the chomp will be an issue because this is a simple system and all the file names are program generated, and in a fixed format, but it's still good to know. Also, I'll fix the 'or' issue right away. Thanks again.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (1)
As of 2024-04-25 03:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found