Week 8 - Computational Media, Take Two: Perl

Unix, Pico, Perl Basics, Perl vs. Director, Getting Input from command line, Working with forms, Getting Input from froms, Saving to file, Reading from file, Perl debugging

Unix

We will be working with Perl on the ITP Stage server, which runs the operating
system Linux. Linux is a version of Unix - and you interact with it through
the command line. Very geeky. So you need to know some basic commands:

Unix commands
cd - change directory
mkdir - make directory
ls - shows all files in current directory
ls -l - shows all files with permission information
cd .. - takes you up a directory
pwd - print working directory
chmod - changes permissions
mv - moves a file cp - copies a file rm - removes a file
For more on unix, take a look at this Unix Tutorial for Beginners

Getting Started Step-by-step
1. Telnet into stage.itp.tsoa.nyu.edu
So you need a telnet program. Mac OSX you can use the terminal. You can go to tucows.com to download Putty for Pc or Nifty Telnet for Mac. 2. Enter userid and password (same as NYU mail account)
3. Go into your public_html folder by typing cd public_html
4. Create a folder called cgi by typing mkdir cgi
5. Go into the new cgi folder by typing cd cgi
6. Open a new file called program1.cgi in pico (word processing program on server) by typing pico program1.cgi
7. Type the following:
#!/usr/local/bin/perl
print "hello world!\n";

8. Exit out of pico with Control-X
9. Make your program executable by typing chmod a+x program1.cgi
10. Run your program by typing program1.cgi
11. Congratulations! You have written and executed your first perl program.

Pico

Pico is an easy to use (relatively speaking) word processing program
on the server. It is the same program used by Pine mail. If you end up
doing lots of perl programming, it's probably worthwhile to learn another,
more powerful unix text editor, like vi or emacs. Much more complicated,
but much more powerful. Pico will suit our purposes just fine.
Use arrows to move around. Delete on the keyboard should work.
Control-x to save and exit.
Control-g to get help.

Perl Basics

Ok - A Bit About Perl
1. All perl programs need to start with
#!/usr/local/bin/perl
This tells the server where to find the perl interpreter.
2. Use # to comment
3. All lines of a Perl program must end with ;
4. Variables start with $
5. Lists start with @
6. To print a new line, use \n
7. To get input from command line, use <STDIN>

Great resource: Free online book Learning Perl

Perl vs. Director: A little bit of compare and contrast

Perl Director
Server-side Client-side (though it can communicate with server - next week)
text-based (though you can point to multi-media elements from browser) more inherently multi-media
user interaction only through text (unless you are using web elements) user interaction from mouse events, key down, text fields, etc.
Is a programming language with somewhat intimidating syntax Has a built-in scripting language, Lingo
To comment: use # To comment: use --
if ($seventh eq "on") {
print "You want to know\n";
} else {
print "Oh well, you don't want to know\n";
}
if seventh = on then
   put "You want to know"
else
   put "Oh well, you don't want to know"
end if
for ($i = 1; $i <= 5; $i++) {
print "$i ";
}
repeat with i = 1 to 5
   put i
end repeat
@myarray = (a, b, c, d, e);
foreach $letter (@myarray) {
print "$letter\n";
}
mylist = ["a", "b", "c", "d", "e"]
repeat with i = 1 to mylist.count
   put mylist[i]
end repeat

Getting Input from command line

Getting input and doing something with it
1. Open a new file called program2.cgi
2. Type the following:
#!/usr/local/bin/perl
print "Hello, what is your name?\n";
$name = <STDIN>;
chomp($name);
print "hi $name, how are you?";

Program will stop and wait until something gets typed in.
chomp cuts off the new line character.

Working with forms

Forms allow user to input information into a website.
They can be handled with javascript (client-side) or cgi scripts
(server-side). We will be using CGI scripts written in perl.
Check out my simple form. View source.

See how each form element gets a name. You will later use these as variables in your script. Text elements and checkboxes get values assigned based on what the user puts in. If you are using a pulldown menu or radio buttons, you have to set explicit values.

Check out this forms tutorial if you want to know more.

Getting input from forms

1. You need to specify which script your form is going to call when submitted.
<form method = "get" action = "http://stage.itp.tsoa.nyu.edu/~eqd6730/cgi/test1.cgi">
[For now we will use "get" rather than "post". Main difference is that "get" sends
data attached to url and "post" sends as part of body.]

2. $in=$ENV{'QUERY_STRING'};
This is known as the environment variable.
Everything from your form gets added to the url of the script after a ? -
and it all gets put into the variable $in in this case.

3. use CGI qw(:cgi);
This allows you to easily work with the environment variable.
rather than using the split function, etc.
Then you can play around with them.

See example script to come...

Saving data to a file

This allows for persistence of data. So you can start communicating across web. Can combine people's data, etc.

example script:
#!/usr/local/bin/perl
use CGI qw(:cgi);
print "Content-type: text/html\n\n";
$name = param('name');
$food = param('food');
$eyecolor = param('eyecolor');
#open file on server
# >> means we are appending
# die is in case our permissions are off
open(OURDATA,">>ourdatafile") || die ("something is wrong\n");
#print what we want to save to the end of the file
print OURDATA "$name has $eyecolor eyes and likes to eat
$food.
\n";
close(OURDATA);



Reading data from file

open(OURDATA,"ourdatafile")|| die ("something is so very wrong\n");
#this is a loop that goes through file line by line
while ($line = ) {
print "$line\n"


Perl debugging

1. Always always run your script from the command line. It gives you much better error messages.
Many problems are syntax-related.

2. Use print statements to let you know what's going on - same as using put statements in Director.

3. When running programs from Web, you don't get useful error messages. You can check out the server error log that Nancy has kindly made available. Well, it's actually only the last 50 errors - so be quick about checking. Search for your userid.

4. You can use the perl syntax checker from the command line. Go to the cgi directory. Type:
perl -c nameofyourfile.cgi
It will tell you if your syntax is correct or if it has errorso.