http://www.perlmonks.org?node_id=302145


in reply to rename 0.2 - an improved version of the script which comes with Perl

That is sweet -- much more flexible/usable (in terms of renaming files) than the home-grown script that I wrote for myself years ago to do sort of the same thing. (But then, my script is actually meant to do anything you want with files or any list, not just renaming.)

Just one squib about your choice of semantics for the "-z" option: downcasing names is already pretty easy (in fact, I assume you can specify it separately, while still using "-z"). But something that's not so easy, and that comes up all too often, is whole bunches of file names that happen to include characters like  [<>|!=\&\$\#\(\)\[\]\{\}\\] (probably others as well), which tend to have special meanings in shell commands. When the unwitting shell user happens to do a quick "copy/paste" of such a name onto a command line, all hell breaks loose.

(I've seen file names like this created by "wget -r ..." as well as by "innovative" windows users putting files with "helpful" names onto volumes that are used by both samba and nfs.)

So, since there's already a way to downcase, but removing shell metacharacters can be an important issue in "sanitizing", how about the thrice-used "-z" handling the latter instead? (Note that a space really just another shell metacharacter -- so it makes sense to handle other such characters with the same option.)

(BTW, does "thrice used" mean "-zzz" or "-z -z -z"? I guess with the longer args it's an easier guess: "--sanitize --sanitize --sanitize", but that's a lot to put on the command line...)

  • Comment on Re: rename - an improved version of the script which comes with Perl
  • Download Code

Replies are listed 'Best First'.
Re^2: rename - an improved version of the script which comes with Perl
by Aristotle (Chancellor) on Oct 26, 2003 at 13:12 UTC

    You're right about the shell metacharacters - and their substitution should really happen along with the control characters. I don't know why I thought [:cntrl:] would include them, it logically doesn't. To me the space is more than just another metacharacter - it breaks more things than the others. (Trivially fixed, contrived example: for F in * ; do ls $F ; done)

    You can bundle short options, as you can see from the source - Getopt::Long::Configure('bundling', 'no_ignore_case');. But you can't specify downcasing independently - that's why I included it by having a third -z. Besides, I kinda liked the -zzz visual.. maybe I should add a -Z option as well? ;^) Now that I think about it though, maybe sanitizing names should indeed not be a simple option, not a mode.

    I also discovered another independent derivative of Larry's rename script on CPAN just after I posted this one, with rather different goals (and pretty messy innards).

    So there's plenty of things to move around and more yet to add. Expect a New And Improved version in the near future. :) (For now, this version scratches me quite excellently, and I have other things to do, so the pressure is low.)

    Makeshifts last the longest.