in reply to How would I write this multiple substitution script?
Also, readdir returns both plain files and directories, and you don't check for that. The file list may (depending on the platform) also include "." and ".."! Check with -f to see if it's indeed a file before you try to open it — again, don't forget about the directory.
It might be a little easier to use glob to get the file list, for which the file names will include the directory name. Plus, you can provide a pattern for the file name. Also, glob will not return results for "." or "..", whatever the pattern. (Though, on Unix it might if the pattern for the basename starts with "."; I'm not sure.)
As an optimization for writing code: you could pull out the actual file processing (the sequence of s/// statements) into separate subs, and pass a sub reference to a sub which does the boring stuff, which can be common for all directories and all files. Something like:
process($dir1, sub { s/blue/red/; s/yellow/green/; }); sub process { my $dir = shift; my $sub = shift; ... # pretty much like your own code, until we have to process the + file contents: $sub->(); # note that the file contents is expected to be in $_ ... # rest of the wrapper code }
|
|---|