sf500 has asked for the wisdom of the Perl Monks concerning the following question:

Hello there, Is there the posibility of changing the bellow snippet into a subroutine so every time I invoke into all *.rc files (changing the versions). Here how it is:
.............. chomp(my $rc_dir_file1 = "C:\\$dev_proj"); chomp(my $rc_file1 = "C:\\$dev_proj\\file1.rc"); chomp(my $rc_file2 = "C:\\$dev_proj\\file2.rc"); chomp(my $olddot_ver = $v_rc_file1); my $pendingnewdot = (chop($v_rc_file1)+1) if ($vss_usr_auto =~ /^up$/i +); my $pendingnewdot = (chop($v_rc_file1)-1) if ($vss_usr_auto =~ /^down$ +/i); chomp(my $dot_ver = ($v_rc_file1 . $pendingnewdot)); print "\nUpdating: file1.dll, File: "; system('ss.exe Checkout "$rc_file1" -cAutomated_Version_UPDATE_System +-F- -I-Y -R'); system('ss.exe Checkout "$rc_file2" -cAutomated_Version_UPDATE_System +-F- -I-Y -R'); #FILE VERSION my $screw = $olddot_ver; $olddot_ver =~ s/\./\,/g; my $oldcomma_ver = $olddot_ver; chomp(my $newdot_ver = $dot_ver); $newdot_ver =~ s/\./\,/g; chomp(my $newcomma_ver = $newdot_ver); my $find = (' FILEVERSION ' . "$olddot_ver"); my $replace = (' FILEVERSION ' . "$newcomma_ver"); my $find1 = ('VALUE "FileVersion", ' . "\"" . "$screw" . "\""); my $replace1 = ('VALUE "FileVersion", ' . "\"" . "$dot_ver" . "\"" +); #FILE VERSION Subtitution find(sub{return unless(/\.$doctype_rc$/i);local @ARGV = $_; local $^I += '.bac';while(<>){if(s/$find/$replace/ig) {print;} else {print;}}}," +$rc_dir_file"); find(sub{return unless(/\.$doctype_rc$/i);local @ARGV = $_; local $^I += '.bac';while(<>){if(s/$find1/$replace1/ig) {print;}else{print;}}}," +$rc_dir_file"); print "$file.$type:"; system('ss.exe Checkin "$rc_file1" -cAutomated_Version_UPDATE_System - +I-Y -R'); system('ss.exe Checkin "$rc_file2" -cAutomated_Version_UPDATE_System - +I-Y -R'); sub d { my $file = $File::Find::name; $file =~ s,/,\\,g; return unless -f $file; return unless $file =~ /$file_pattern/; open F, $file || print "couldn't open $file\n" && return; while (<F>) { if (my ($found) = m/($search_pattern)/o) { print "ERRORs found in: $file\n\n"; last; } } close F; } .........
Thank you for all your help.

Replies are listed 'Best First'.
Re: Find Replace into a subroutine
by shmem (Chancellor) on Sep 02, 2008 at 13:59 UTC
    Here how it is: ...........

    ....... and it is wrong from the beginning:

    chomp(my $rc_1 = '$rc_file1');

    Read chomp to learn what it does. The above line just stuffs the literal $rc_file1 into the variable $rc_1; chomp does nothing here. As it doesn't anywhere else in your code. Read up on chop, also.

    Your code is written in a way it is nearly impossible to read and get what it is intended for; variables with unknown content appear out of the blue; cloning of variables is done without ever using the resulting variable (e.g. $rc_1); the formatting is poor.

    So, before even thinking in refactoring, clean up that mess first.

Re: Find Replace into a subroutine
by lidden (Curate) on Sep 02, 2008 at 23:05 UTC
    Could you explain what it is you are trying to do, because that code dose not make any sense to me.

    What are those system calls to ss.exe for? There seems to be two different ss.exe on windows are the correct one in your path?
    SmoothSurfer (ss.exe) is a popup advertisement blocker.
    and
    Visual SourceSafe (VSS) is a client/server application which acts as a storage system for files.

    You obviously wants the second one but you are trying to checkin/checkout a file named "$rc_file1" instead of whatever is in $rc_file1.

Re: Find Replace into a subroutine
by puudeli (Pilgrim) on Sep 02, 2008 at 13:17 UTC

    Of course it can be be re-factored.

    Have you even tried to re-factor the code? If you haven't why should someone else do it for you?

    --
    seek $her, $from, $everywhere if exists $true{love};