A little bit of perl

"Perl" stands for practical extraction and report language . You will find many books on the language in the bookstore, for it is very popular in applications to the internet. We use it here as an editing tool. All of the scripts we use here either build a Maple worksheet, or a LaTeX file or massage a set of existing LaTeX files, that is, make substitutions, deletions and additions to the files. These are tasks that could be done by hand with a good text editor, but the time required to complete the task would be large.

You don't need to know any perl to run the scripts, as we have seen, but if you want to be able to modify a script or write your own, you need to know a little bit, and a good way to learn a little bit is to look at examples. Here are a couple of useful ones.

Global substitutions in multiple files

Suppose you have several files that contain the same mispelled word. This can be done file by file using a text editor. A good editor, like vi or pfe, or word97, will allow you to make global substitutions in a file. For example, in vi (which is on the chisel disk) the following sequence in a TeXShell would change each occurence of thru to through in the file bill.tex.

vi bill.tex

:1,$s/thru/through/g

:wq

On the other hand, here is a short perl script (called glosub.pl) which can make the same substitution, for example, in all tex files in the current directory by executing the sequence

perl -S  glosub  thru  through  *.tex

Just copy the script into an editor and save the file as glosub.pl in X:\chisel\bin, where X is the letter of the drive where you installed chisel.

glosub.pl

#!perl -i.bak
$opt = $ARGV[0];
if ($opt =~ /\-h/) {
print "Usage: perl -S glosub old new files \n";
print " This script replaces all occurences of old by new in files.\n";
exit;
}
$old = $ARGV[0]; shift;
$new = $ARGV[0]; shift;
$/ = "";
while(<>){
s/$old/$new/g;
print;
}
print "done";
exit;

You can learn a little bit of perl just by reading through this script, and testing it out on some test files in a temporary directory. A lot you can pick up from context, but there are many things can only be learned by reading and/or seeing them demonstrated. For example, the top line activates the perl option -i, which says to modify the files in place, but make back up files first with names like filename.bak where filename is one of the files being processed. Similarly, the line $/ = ""; says to process the files a paragraph (separated by blank lines) at a time.

I mention here that there is a lot of documentation that comes with the perl5 on the chisel disk. After you have installed it, look in X:\Perl\doc, where X is the drive letter where you install Chisel. Also, there are books on perl. One good one is Learning Perl on Win32 Systems , by Randal Swartz, Erik Olson, and Tom Christiansen, published by O'Reilly and Associates, Inc.

Recolorizing the eps files that are exported from Maple

When you export a Maple worksheet to Latex, the plots that are in the worksheet are exported as monochrome plots. However, they can be recolorized by observing which shade of grey each of the sixteen named colors in Maple converts to and make the appropriate inverse substitution in the eps file. Here is a perl script clreps.pl that performs the substitution. Note that it also changes the thickness of lines in the plot and removes the border around the plot.

clreps.pl

#!perl -i.bak
#
$opt = $ARGV[0];
$rel5 = "no"; $i=0;
$comout = "off";
if ($opt =~ /\-h/) {
print "Usage: perl -S clreps files to colorize black and white\n";
print " eps files generated when exporting Maple worksheets\n";
print " to LaTeX. Also thickens up lines in line drawing\n";
print " and removes the border around the plot.\n";
exit;
}
@x =(0, .11, .179922, .234157, .291451, .3, .30549, .37451, .41, .42698,
.55498, .59, .592431, .593843, .7, .701882, .752941, .774196, .82949,.832784, .844706, .89, 1);
@r = (0, 0, .13725, .30980392, .55686, 1, .64706, .80000000, 1, .55686275, .8000, 0,
.623529, 1, 0, .43922, .75294, .91764706, .55686, .84705882, .67843137, 1, 1 );
@g =(0, 0, .13725, .18431373, .13725, 0, .16471, .19607843, 0, .41960784, .4980, 1,
.623529, .498, 1, .85882, .75294, .67843137, .13725, .84705882, .91764706, 1, 1 );
@b = ( 0, 1, .55686, .30980392, .41961, 0, .16471, .19607843, 1, .13725490, .1961, 0,
.372549, 0, 1, .57647, .75294, .91764706, .41961, .74901961, .91764706, 0, 1 );
#
while(<>){
if (/drawborder true def/){s/drawborder true def/drawborder false def/;
$rel5 ="yes";}
s/thin 3 def/thin 13 def/;
s/medium 7 def/medium 17 def/;
s/thick 16 def/thick 26 def/;
if (/([0-9\.]+ G)/) {
$t1 = $1;
$t = $t1;
$t =~ s/ G//;
$i=1;
while ($x[$i] < $t && $i < 23) {$i =$i+1;};
$red = $r[$i] - ($x[$i]-$t)*($r[$i]-$r[$i-1])/($x[$i]-$x[$i-1]);
$grn = $g[$i] - ($x[$i]-$t)*($g[$i]-$g[$i-1])/($x[$i]-$x[$i-1]);
$blu = $b[$i] - ($x[$i]-$t)*($b[$i]-$b[$i-1])/($x[$i]-$x[$i-1]);
s/$t1/$red $grn $blu C/;
}
# This takes off the border for release 4 eps files
if (/The following draws a box/ && $rel5 eq "no") {$comout = "on";}
if ($comout eq "on") {$i=$i+1;s/./\%/; if ($i==8) { $comout = "off";}}
print;
}
exit;

We can test this by export this worksheet to latex and then in a TeXshell, issue the command

perl -S clreps lit*.eps

> plot(sin(1/x)*x,x=1/(14*Pi)..1/Pi,
numpoints=100,thickness=3,color=magenta);

[Maple Plot]

table of contents