Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: convert whitespaces to underscores

by mattriff (Chaplain)
on May 26, 2002 at 03:09 UTC ( [id://169329]=note: print w/replies, xml ) Need Help??


in reply to convert whitespaces to underscores

A few thoughts:

I'm pretty sure that chdir("-") will only work under certain UNIX shells. Actually, in limited testing, it doesn't seem to work for me even when the Perl script is run from a shell (tcsh) that does support that.

UPDATE: In retrospect, as merlyn has pointed out, this would in fact never work. My pre-post testing actually bore that out. No more late (for me) night posting...

You can omit the quotes around lone variable names. That is, "$file" and $file are the same in all of your cases.

As an aside, if you use the Cwd module and its getcwd() function, your code will be portable to non-UNIX systems (that probably don't have a pwd command to shell out to). You don't really need to use either method, though. Since you are presumably in the directory you want to open, you can use: opendir(DIR,'.').

I say presumably because the code really should be checking to see if the chdir() you did succeeded, or you could end up with unexpected results. Likewise, you might consider using the -e test to see if a file named $file already exists before you rename(), so you don't overwrite something accidentally.

- Matt Riffle

Replies are listed 'Best First'.
•Re: Re: convert whitespaces to underscores
by merlyn (Sage) on May 26, 2002 at 22:15 UTC
    chdir "-"; won't work anywhere. All I can say is "untested code". It doesn't matter what your shell is. Perl is handing that to chdir(2) which is then looking (regardless of shell) for a subdirectory called dash underneath the current directory.

    That's why you should always always always check the return status of chdir -- to prevent such embarassment.

    -- Randal L. Schwartz, Perl hacker

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (2)
As of 2024-04-24 17:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found