[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

[MacPerl] find replace



I'm totally unschooled in programming, but playing with Perl anyway. I need
to go to a site, look for specific links and update them. I found a script
called chrp but I don't have a clue as how to configure it for my needs.
The script isn't commented in any way & the manual assumes I know what I'm
doing. I am probably missing some FAQ or module or something extremely
obvious, only it doesn't seem obvious to me. Any help or direction greatly
appreciated.

#Config
#unshift (@INC, "/home/hakan/httpd/libwww"); require "www.pl";

$TmpF="/tmp/$$.tmp";
#End of Config

$ac=0; $cmdopt=""; $msgs=1;
while($ARGV[$ac] =~ /^\-(\w+)$/){
    $i=$1;
    if ($i =~ /h/) {print "Usage: chrp [-hieq] <SerachStr> <ReplaceStr>
[<File1> ... <FileN>]\n";exit;}
    if ($i =~ /i/) {$cmdopt="e$cmdopt";}
    if ($i =~ /e/) {$cmdopt="i$cmdopt";}
    if ($i =~ /q/) {$msgs=0;}
    $ac++;
}

$ChFrom=$ARGV[$ac]; $ac++;
$ChTo=$ARGV[$ac]; $ac++;
if ($msgs) {print STDERR "Changing $ChFrom to $ChTo\n";}

if (!$ARGV[$ac]){
    $con="";
    while(<STDIN>) {$con="$con$_";}
    $con=&dochrp($con);
    print "$con";
}

while($ARGV[$ac]){
    $f1=$ARGV[$ac];
    $ac++;
    if ($msgs) {print STDERR "$f1...";}
    if (!(-e $f1)) {if ($msgs) {print STDERR "Not Found\n";}} else {
	$con="";
	open (Fil, "<$f1");
	while (<Fil>) {$con="$con$_";}
	close (Fil);

        $con=&dochrp($con);

	open (Fil, ">$f1");
	print Fil "$con";
	close (Fil);

	if ($msgs) {print STDERR "OK\n";}
    }
}

exit;

sub dochrp {
    local($cont)=@_;
    $cmd="\$cont =~ s/$ChFrom/$ChTo/g$cmdopt";
    eval($cmd);
    $cont;
}

http://meckes.com



***** Want to unsubscribe from this list?
***** Send mail with body "unsubscribe" to mac-perl-request@iis.ee.ethz.ch