Defamiliarization II: Perl

This week’s assignment was either to make the ‘Hello World’ command to work in Perl and document the relevance of this exercise, which is used in most handbooks on programming languages. The second option was to ‘translate’ last week’s product (the command line in Unix) to a script in Perl. The first exercise proved to be easy with a bit of exercise and a perl crashcourse. I learned from this that by working in Perl you can write chunks of code and save them as packages that you can refer to later. In other words, you don’t write directly to terminal but create a separate raw .txt file where you put the script (always beginning with the line #!/user/bin/perl. From terminal you can execute it with the command “perl filename.txt”. I know how to directly create these .txt files in VIM (a very simple texteditor) and then run them in terminal.

I could make the ‘Hello World’ program run fairly easily, so I decided to try my luck at the second exercise.

A recap of the command line I produced earlier: grab a google query of yourself (eg. Loes Bogers) in a textbased webbrowser, dump the results into a morse code translator that prints the translation on screen. I tried to work lynx (textbased webbrowser) through Perl, this didn’t work and after a few hours of trying I gave up and tried to make the second half of the script work: how to send text (STDIN, so from keyboard to make it easier) through a morse code translator. Then I could not find a Perl script for that anywhere, and started to realize that the whole thing might be a bit too much for a first exercise in Perl, since I couldn’t even open a file without resorting back to the command line in terminal.

Back to basics. With Renee and Lisa I started to research the basics: how do you open a file, eg. a simple textfile with the use of a perlscript. We came across the concept of FILEHANDLES: a way of telling Perl how to refer to a file in the future…So we worked on that a little bit. We were able to open a picturefile in terminal.

open (PIC, “flower.bmp”);
while ($blahblah = <PIC>) {
print $blahblah;

Basically, what we did is give the picturefile “flower.bmp” the filehandle called PIC. As you can see PIC is then referred to in order to OPEN the file. The second part of the file says that PIC is now a variable so we can manipulate it. That’s what the $ stands for (we called it blahblah just to indicate that it can basically be called anything you like) and then linked it to the filehandle PIC. Print $blahblah sends it to STDOUT (which is the screen, terminal window). After this the file is closed, again by referring to the file handle rather than the actual file. And it looked like this:

Yay! we opened a file. Textfiles are opened in exactly thesame way but make more sense on the screen (provided that you use raw text files, to keep it looking clean).

We pushed things a little further and researched how you could manipulate text in a textfile by for example substituting certain words with other words. We copied a psalm from the internet and Renee and I tried to make substitutions of the words ‘Lord’, ‘LORD’ and ‘lord’ into ‘Imiant’, and ‘Gods’ and ‘gods’ into ‘New Media’, and some other alterations. Referring to the enigmatic aura that surrounds the IMIANT approach at the moment ; – ) We just have to have faith I guess….Meanwhile, Lisa tried to find out how to change the standard output to a new file: sending the data to a new textfile rather than the screen.

To give you an idea of the psalm:

Psalm 16

(This is) the secret of David.
v1 Keep me safe, my Lord, because I come to you for help.

v2 I said to our LORD, You are my Lord.
All the good things in my life come from you.

v3 I have great pleasure in the saints on earth.
It is they that do such good things.

v4 But people that run after other gods will be sorry.
They will be more than sorry.
I will not offer gifts of blood to other gods with them.
I will not even say the names of these other gods.

v5 You, LORD, chose my part and my cup.
You make safe what is mine.


Logically the substitutions of the words would have to be done before the output is printed to screen so the code would be look something like this:


open (GOD, “psalm16.txt”);

while ($blahblah = <GOD>) {
$blahblah=~ s/[Ll][Oo][Rr][Dd]/Imiant/g;
$blahblah=~ s/[Ss][Aa][Ii][Nn][Tt][Ss]/Technologies/g;
$blahblah=~ s/[Gg][Oo][Dd][Ss]/New Media/g;
$blahblah=~ s/[Dd][Aa][Vv][Ii][Dd]/the MA Interactive Media/g;
print $blahblah;

Again a file handle (GOD) to open the textfile (psalm16.txt). Referred to as a variable. A typical line to substitute words is this

~ s/word1/word2/g

the s stands for ‘substitute’ and then you put in the word that is to be substituted (word1), and the substitute (word2) and ‘g’ which stands for ‘globally’ indicating that the substitution should occur everytime word1 is found in the text, so not only substituting just the first time, but everytime. These three are divided by forward slashes.

By formulating word1 eg. Lord like this: [Ll][Oo][Rr][Dd] you make sure that every occurance of the word lord is recognised, regardless of upper/lowercase letters. So looking at the first substitution line: every occurrance of lord/Lord/LORD/lOrD etc is substituted by ‘Imiant’ in the text. The other variables work similarly: substituting ‘saints’ by ‘technologies, as also ‘gods’ by ‘new media’ and finally ‘david’ by ‘the MA Interactive Media’. The result is then printed to the screen and looks like this:

If that doesn’t give the Imiants hope, I don’t know what will : – )

Defamiliarization, Exercises