Transcript Lec-1-perl
CS 360
Internet Programming
Objectives for this class period
Web programming is important
Administrative organization of the course
Introduction to first two labs
Basic Perl
Job Postings
In terms of specific technologies you should be
familiar with, you should be comfortable
programming in Java, PHP, and/or .NET (any,
but not necessarily all, are desirable). Excellent
working knowledge of HTML is a must. It would
also be helpful to have some experience with
JavaScript, HTTP, and regular expressions.
Familiarity with other web-related technologies
such as Active Server Pages and J2EE is
helpful. You should also have experience
working with relational databases (e.g.,
mySQL).
Job Postings
This full-time or part-time programmer
would help develop our current web
application in JavaScript, PHP, MySQL,
Flex (a bonus), (minimum 2 years
experience in PHP). We are moving in a
transition to AJAX and Python for portions
of our application and knowledge in these
areas is a bonus.
What is CS 360?
It gives you the skills you need to apply for
these jobs
It provides you with Operating System
Fundamentals
File Systems
Semaphores
Sockets
You should have a good architectural view of
internet programming
Overview
15 Laboratories will keep you busy
Due nearly every week
Midterm and Final covering book material
Homework
Due each Friday
Create a web page
This class should be a lot of fun!
First Lab Due Wednesday
Create a web page for homework
assignments
Make it passwd protected
The first real lab is due on
Friday
Install Apache
Write Perl code to test performance
You will apply this to your web server in
future labs
This shouldn’t be hard, but you need to
get started now
Perl Introduction
Uses
Shell scripts
CGI, web engines
Good at
Text processing
Small/Medium sized projects
Quick and dirty solutions
Portability (to a certain degree)
Perl Introduction
Bad at
Efficient large scale computation
Neat code!
Hello World
#!/usr/bin/perl
# This is a comment. It goes to the end of the line.
# Perl is easy. It helps to be neat.
# Just like C, every statement must end with ;
print “Hello World\n”;
Perl Data Types
Three Main Datatypes
Scalar
•
•
•
•
A single number, string or reference.
$sequence = “acgtac”;
$num = 6;
$num = $num + 1;
List
• A collection of scalar datatypes.
• @insects = ( “hopper acgtacacactgca”,
“flick acgcacacattgca”, “Catapillar acgcatattttgca”);
• $insects[$num] = “ant attaccagga”;
• Length of an array $#insects”
Hash
• Pairs of scalars, accessed by keys.
• %hash = ( “Spins” => “Atlas”, “Sings” => “Goldfish” );
• $hash{“Spins”} = “Globe”;
Perl Context
Operations may return diferent values, or do different
things based on the context in which they are called.
Reading from a filehandle
$one_line = <FILEHANDLE>;
@whole_file = <FILEHANDLE>;
Removing Newlines
chomp($string);
chomp(@list);
Perl Syntax
Basic Operators: + - * / ++ -- *= += -= /=
String concatenation: $newstring = $s . “another string”;
Numerical operations
if($x == $y)
if($x <= $y)
if($x > $y)
Equality
Less than or Equal to
Greater than
String operations (Based on lexial ordering)
if ($s1 eq $s2)
if($s1 gt $s2)
if($s1 le $s2)
Stringwise equality
Stringwise greater than
Stringwise less or equal
Perl Basics – ‘if’
Selective evaluate blocks of code depending on a condition.
If ($string eq “blah”)
{
print “String is blah\n”;
}
elsif ($string eq “spoo”)
{
Print “String is spoo\n”;
}
else
{
Print “String isnt either?\n”;
}
Perl Basics - ‘if’
Can also be done as a ‘one-liner’
print “String is foo\n” if ($string eq “foo”);
Unless (Not if)
print “String is not foo\n” unless ($string eq “foo);
Unless cannot have else or else if components.
Perl Basics – ‘for’
Practically the same as C/C++/Java
for ($i = 0; $i < 10; $i++)
{
print “i is “ . $i . “\n”;
}
Perl Basics – ‘foreach’
A handy way of processing a list
foreach $grocery (@groceries)
{
scan($grocery);
}
Can use the default variable ($_)
foreach (@groceries)
{
scan($_);
}
Perl Default Variables
Most basic functions will operate on the default
variable by default.
foreach (@list)
{
chomp;
print;
}
Above chomps each element, and prints it.
Perl Basics
Command-line arguments
Contained in @ARGV
Strings just like C
$ARGV[0] is not the program name
Environment variables
Contained in %ENV
print $ENV{“PATH”};
Perl Basics - Subroutines
Identified by keyword “sub”
Arguments come in @_
Local variables identified by keyword “my”
#!/usr/bin/perl
$t1 = 1;
$t2 = 2;
$t3 = testsubroutine($t1, $t2);
print "The answer from testsubroutine is: $t3\n";
sub testsubroutine
{
my $arg1 = shift @_;
my $arg2 = shift;
return $arg1 + $arg2;
}
#Since _ is default variable, we don’t have to type it
Perl Filehandles
An interface to an open file.
Line read operator is angle brackets.
open(INFILE, “dna1.txt”) or die “cant open dna1.txt: $!”;
while(<INFILE>)
{
print “line $_\n”;
<STDIN>
<STDERR>
Opening files in other modes
Change the filename string
“<dna1.txt”
read dna1.txt
“>dna2.txt”
output to dna2.txt
“>>dna2.txt”
append to dna2.txt
}
Perl File Reading & Writing
$FILE1 = $ARGV[0];
$FILE2 = $ARGV[1];
open(FILE1) or die "Unable to open $FILE1 for reading\n";
open(FILE2, ">” . $FILE2) or die "Unable to open $FILE2 for writing\n";
while (<FILE1>)
{
print FILE2 $_;
}
close(FILE1);
close(FILE2);
Running other programs
Use back ticks
Use the system command
$people = `who`;
system(“who > who.dat”);
Using piped input
open(PEOPLE, “who |”);
while(<PEOPLE>)
{
print “$_ is logged on\n”;
}
Libwww
Don’t worry about the CGI in the examples, we
will get to it later
# Create a user agent object
use LWP::UserAgent;
$ua = LWP::UserAgent->new;
$ua->agent("MyApp/0.1 ");
# Create a request
my $req = HTTP::Request->new(POST => 'http://search.cpan.org/search');
$req->content_type('application/x-www-form-urlencoded');
$req->content('query=libwww-perl&mode=dist');
# Pass request to the user agent and get a response back
my $res = $ua->request($req);
# Check the outcome of the response
if ($res->is_success) {
print $res->content;
}
else {
print $res->status_line, "\n";
}
This is really easy
Practice and work with examples from the
web.
Look at all of the response elements
You will want to use this perl script to
debug your web server.