The way your code is written, the "interactive" flag is only checked when the "force" flag has been set, which seems odd at first glance -- is this what you intended?
If so, then specifying "-i" and "-q" together could get a user into trouble -- he'd be asked to confirm a rename operation without being shown what's going to happen...... if ($force == 1) { if ($interactive == 1) { ...
Also, I wonder why you print an empty line on every iteration even when a rename is not called for (that last "else" block). Maybe the code you want for the loop over files is something more like this:
I haven't tested it entirely, but I think it preserves the intended(?) logic of the original.foreach $old ( grep { -f $_ } @filez ) { $new = $old; next unless ( $new =~ s/$search/$replace/ ); # s/// returns false if there's no match if (( $force and $interactive ) or not $quiet ) { print "$old -> $new"; if ( $interactive ) { print " y/n? "; $yn = <STDIN> # no need for chomp here next unless ( $yn =~ /^y/i ); } else { print "\n" } } rename $old, $new if ( $force ); }
You may want to think more about what makes sense as a set of command line options, e.g. "--verbose" instead of "-q", "-n" (no-op) instead of "--force"; and maybe you want to add to pfaut's suggested ARGV handling, to check for (and die on) incompatible combinations of options -- and make sure you have a "$Usage" string that you print out when essential args are missing or unusable args are given.
For the heck of it, you might want to check out the "shloop" (shell loop) utility that I have linked from my home node; it supports the use of perl regexes for file renaming, as well as for other command-line operations (copy, delete, symlink, whatever). BTW, that reminds me -- you may need to use quotemeta on $search (or \Q$search\E in the substitution), in case the command line arg contains regex meta-characters (like "+", "$", etc).
In reply to Re: Hey everybody
by graff
in thread Problem with command-line option parsing
by fightingbishop22
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |