Laboratory Sessions on Perl¶
Tu Sep 1 and Thu Sep 3, 2020¶
- If you get stuck, ask for help.
- Feel free to talk to each other and help each other as needed.
- Use your imagination to extend the assignments and experiment.
Intro to scripting and \(\LaTeX\)¶
In this lab we will take a first look at automating tasks using scripting languages. Here we will focus on Perl and how it can be used to compile different pieces of information into a small report. To write the report we will use \(\LaTeX\).
Start by adding a directory called
Labs/lab1
to your repository, this is where you will work on this assignment. This lab work will not be graded but I will check that you did it.Pull the latest changes from the class repo. If you have not yet cloned it, do so by
git clone https://your_username@bitbucket.org/motamed/hpsc2020.git
.Copy the contents of
hpsc2020/Labs/lab1
into the directory you just created.Start with opening the file
lab1.tex
using, for example, emacs$ emacs lab1.tex &
. Inside the file you can see different sections, subsections, and some basic commands for equations and figures. To compile the file execute$ pdflatex lab1.tex
in the terminal window. After having executed this command you should have generated a file calledlab1.pdf
, do$ ls -ltr
to check that it is there. Open the pdf file (e.g. by$gs lab1.pdf &
or by$evince lab1.pdf &
or by$open -a preview lab1.pdf
) and try compare the output with thelab1.tex
file.Note If you get an error message after running pdflatex saying that the file lab1.eps does not exist, you will first need to run the Matlab code lab1.m to generate lab1.eps and then run pdflatex.
The two obvious pieces of data in this document are the figure and the table, and we will get to these in a second. Before discussing how we can generate the table and figure automatically we first turn to a very common task in computing.
Automating a parameter study¶
One of the most common tasks in computational science is to perform experiments for a range of parameters, say temperaures in a chemical reaction model or number of realizations in a study containing a random variable. Typically this process contains all or some of the steps:
- Change a data file or a source file.
- Compile the program and run it.
- Post process the output. Perhaps you are computing a length \(L\) but what you are really interested in reporting is the area \(L^2\).
- Compile the processed results into tables and/or figures.
If you perform these tasks by point-and-click, this will typically be a painful and slow process. In addition it will be very error prone as it is easy to forget one of the steps, especially if you are interrupted during the process. It will be much more reliable (and faster) if you let the computer do these tasks for you.
We focus on the two first tasks and use lab1.txt
document as an example of a source code that we would like to change and eventually compile and look at (the pdf version of it).
- Open the file
lab1.tex.Template
(which should look identical tolab1.tex
). Your first task will be to replace the text stringsAUTHOR1
andAUTHOR2
with something more appropriate (For example your name). A possible way to do this is by using Perl. The perl scriptlab1.perl
is set up to replace the first author. Execute$ perl lab1.perl
and take a look at the output on the screen and inside the generated filelab1_auto.tex
. Add the replacement of the second author to the perl script.- Now you have learnt how to replace a string with another string in a text file, an easy operation but often very useful. Next, inspect the bottom of the perl script, you’ll see the statement
system("diff lab1.tex lab1_auto.tex");
which shows how to execute a system command as if you did it in the shell. Extend the script to also compile the file into a pdf file and, if possible, open it.- Next open the file
lab1.m
and change the figure so that the graphics look a bit better. Try to run the matlab script directly from the perl script by the commandsystem("nohup matlab -nosplash -nodisplay < lab1.m > output.txt");
- Open up a matlab window and look at the help page for
fprintf
. Follow the example with the table of the exponential function and create another table for your \(\LaTeX\) file. Either copy part of the ‘’meal’’ table and input a modified exp.txt. The&
splits columns and\\
makes a newline in \(\LaTeX\), see also meals.tex- If you are done with the assignments make sure you commit and push them to your repository. Feel free to help your friends if you finish early, or start looking at Homework 2.