pfaut gave a nice treatment for the first half, so let me take a stab at the second:

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 ($force == 1) { if ($interactive == 1) { ...
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...

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:

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 ); }
I haven't tested it entirely, but I think it preserves the intended(?) logic of the original.

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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.