Greeting fellow monks,

I feel rather embarrased asking this question, but here goes anyway. (It's an optimization plus question.) I've recently written a little thingy that essentially lets the novice/newbie change strings within several files in a directory tree. It's written so that it'll run on both UNIX (via command line) and Win32 (via icon double click) systems.

It works, mostly. Biggest problem is that I tried to use the thing on UNIX to remove those pesky "\r"s that Windows puts in text files. Well, it ended up dumping all "\n"s as well. :(

My question/plea for assistance is two fold and thus: Can someone help me figure out why I can't substitute "\n" for "\r\n"? More importantly, and the bigger issue, there are several parts of this code that are really newbie and inefficient (both in run-speed and # of lines). Can anyone suggest how I might reduce some of the lines here? Thanks.

#!/usr/bin/perl use strict; use Cwd; use File::Find; my $changefrom; while ($changefrom eq '') { print "Change (Pattern): "; $changefrom = <>; chomp($changefrom); } print "To... (Pattern): "; my $changeto = <>; chomp($changeto); print "Match files [.]: "; my $match_files = <>; chomp($match_files); $match_files = '.' if ($match_files eq ''); my $cwd = cwd(); print "Directory [$cwd]: "; my $startdir = <>; chomp($startdir); $startdir = $cwd if ($startdir eq ''); print "Case Sens (y/n) [n]: "; my $case = <>; chomp($case); my $change_files_count = 0; my $change_times_count = 0; print "Starting search & changes...\n"; &find(\&wanted, $cwd); if ($change_files_count == 0) { print "Done! Didn't find any files to change.\n"; } else { print "Done! Made $change_times_count changes ", "in $change_files_count files.\n"; } if (($^O =~ /MS/i) && ($^O =~ /Win/i)) { print "\nPress return to exit. "; my $ending = <>; } exit; sub wanted { if ((-f) && ($_ =~ /$match_files/)) { open(DEFILE, $_) || print "Can't open file: $!\n"; my @file_contents = <DEFILE>; close(DEFILE); my $happens; if (($case eq "y") || ($case eq "Y")) { $happens = grep(/$changefrom/, @file_contents); } else { $happens = grep(/$changefrom/i, @file_contents); } if ($happens > 0) { print "Changing \"$File::Find::name\" file...\n"; my $file_contents = join('', @file_contents); if (($case eq "y") || ($case eq "Y")) { $file_contents =~ s/$changefrom/$changeto/g; } else { $file_contents =~ s/$changefrom/$changeto/gi; } open(DAFILE, "> $_") || print "Can't write to file: $!\n"; print DAFILE $file_contents; close(DAFILE); $change_times_count += $happens; $change_files_count++; } } }

Thanks in advance. :) Keep in mind that I need this to run on both UNIX and Win32, and it needs to be very user friendly. As in, the most basic novice could figure it out.

-Gryphon.


In reply to Change utility; code optimization by gryphon

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.