Assumindo que seu shell é bash, isso pode ser um one-liner:
perl -i.bak -pe '
/\begin\{verbatim\}/../\end\{verbatim\}/ or s/->/\$\to\$/g
' {Cardiology,Pathophysiology,"Patology and Biopsy",Physiology,Propedeutics,Radiology,Rheumatology,Surgery}/*.tex
Note que {...}
é um quantificador regex, então as chaves precisam ser escapadas.
Eu escreveria seu código como:
my @directories=(
"Cardiology", "Pathophysiology", "Patology and Biopsy", "Physiology",
"Propedeutics", "Radiology", "Rheumatology", "Surgery"
);
chdir $path or die "cannot chdir '$path'";
foreach my $dir (@directories) {
opendir my $DIR, $dir or die "cannot opendir '$dir'";
while (my $file = readdir($DIR)) {
my $filepath = "$dir/$file";
next unless -f $filepath and $filepath =~ /\.tex$/;
open my $f_in, "<", $filepath
or die "cannot open '$filepath' for reading";
open my $f_out, ">", "$filepath.new"
or die "cannot open '$filepath.new' for writing";
while (<$fh>) {
if (not /\begin\{verbatim\}/ .. /\end\{verbatim\}/) {
s/->/\$\to\$/g;
}
print $f_out;
}
close $f_in or die "cannot close '$filepath'";
close $f_out or die "cannot close '$filepath.new'";
rename $filepath, "$filepath.bak"
or die "cannot rename '$filepath' to '$filepath.bak'";
rename "$filepath.new", $filepath
or die "cannot rename '$filepath.new' to '$filepath'";
}
closedir $DIR or die "cannot closedir '$dir'";
}
Eu continuaria a fazer mais OO:
use autodie qw(:io);
use Path::Class;
foreach my $dir (
"Cardiology", "Pathophysiology", "Patology and Biopsy", "Physiology",
"Propedeutics", "Radiology", "Rheumatology", "Surgery"
)
{
my $directory = dir($path, $dir);
while (my $file = $directory->next) {
next unless -f $file and $file =~ /\.tex$/;
my $f_out = file("$file.new")->open('w');
for ($file->slurp) {
/\begin\{verbatim\}/ .. /\end\{verbatim\}/ or s/->/\$\to\$/g;
$f_out->print;
}
$f_out->close;
rename "$file", "$file.bak";
rename "$file.new", "$file";
}
}