Introduction to Perl Programming

Download Report

Transcript Introduction to Perl Programming

Intermediate
Perl Programming
Class One
Instructor: Byrne Reese
X401
Copyright 2007 Byrne Reese.
Distributed under Creative Commons, share and share alike with attribution.
About the Instructor
• Byrne Reese
– Product Manager, Six Apart
– Lead Developer, SOAP::Lite
– Perl Hacker and Open Source Junkie
• Contact Info:
– [email protected]
– AIM: byrnereese
– http://www.majordojo.com/
Copyright 2007 Byrne Reese.
Distributed under Creative Commons, share and share alike with attribution.
Intended Audience
• Anyone interested in taking their basic
Perl knowledge to the next step for:
– professional use
– personal use
• Someone with a strong grasp of the Perl
fundamentals: scalars, lists, arrays,
subroutines, etc.
Copyright 2007 Byrne Reese.
Distributed under Creative Commons, share and share alike with attribution.
Objectives
•
•
•
•
Deepen your knowledge and fluency in Perl
Learn practical Perl skills
Master CPAN and frequently used modules
Improve your programming and problem solving
skills
• Learn how to help yourself
• Learn by doing:
– Build and publish web pages
– Execute queries against a database
– Extract and mine the web for data
– Parse XML
– Create your own Perl modules
Copyright 2007 Byrne Reese.
Distributed under Creative Commons, share and share alike with attribution.
“Required” Reading
• Perl Cookbook
Second Edition
by Tom Christiansen,
Nathan Torkington
Copyright 2007 Byrne Reese.
Distributed under Creative Commons, share and share alike with attribution.
Recommended Reading
• The O’Reilly Perl
Series is always a
good reference!
Copyright 2007 Byrne Reese.
Distributed under Creative Commons, share and share alike with attribution.
Recommended Reading
• But O’Reilly is not
the only publisher of
Perl books!
Copyright 2007 Byrne Reese.
Distributed under Creative Commons, share and share alike with attribution.
Setting Expectations
• This is an incredibly challenging class
– Intensive
– Technical
• This can be a very frustrating class
– “Some” system administration required
• Bottom line:
– It takes more than the knowledge of a
programming language to make a software
engineer.
Copyright 2007 Byrne Reese.
Distributed under Creative Commons, share and share alike with attribution.
Homework and Grades
• Primary goal of this class:
– To learn
• Your key to success: Engage
– Class participation
– Homework
• How your final grade is determined
Copyright 2007 Byrne Reese.
Distributed under Creative Commons, share and share alike with attribution.
Getting Started
A Perl Primer
Copyright 2007 Byrne Reese.
Distributed under Creative Commons, share and share alike with attribution.
Your First Program
1. Write the ubiquitous Hello World script
2. Customize it to take a parameterized
name to say “hello” to
3. If input is not provided, prompt the user
for a name
Copyright 2007 Byrne Reese.
Distributed under Creative Commons, share and share alike with attribution.
Remember…
• Don’t get overwhelmed by the problem:
–
Solve the problem in pieces.
• Don’t be afraid to look online for help!
• Ask each other questions!
• Code along with me…
Copyright 2007 Byrne Reese.
Distributed under Creative Commons, share and share alike with attribution.
Hello World
#!/usr/bin/perl
print “Hello World”;
Copyright 2007 Byrne Reese.
Distributed under Creative Commons, share and share alike with attribution.
Hello World
#!/usr/bin/perl
my $say_hello_to = $ARGV[0];
$say_hello_to ||= “World”;
print “Hello $say_hello_to\n”;
Copyright 2007 Byrne Reese.
Distributed under Creative Commons, share and share alike with attribution.
Hello World
#!/usr/bin/perl
use Getopt::Long;
my $say_hello_to = “World”;
GetOptions(“to=s"=>\$say_hello_to);
print “Hello $say_hello_to\n”;
Copyright 2007 Byrne Reese.
Distributed under Creative Commons, share and share alike with attribution.
Hello World
#!/usr/bin/perl
use Getopt::Long;
my $say_hello_to = undef;
GetOptions(“to=s"=>\$say_hello_to);
if (!defined($say_hello_to)) {
print “Please enter a name: ”;
$say_hello_to = <STDIN>;
chomp $say_hello_to;
}
print “Hello $say_hello_to\n”;
Copyright 2007 Byrne Reese.
Distributed under Creative Commons, share and share alike with attribution.
Hello World on the Web
Copyright 2007 Byrne Reese.
Distributed under Creative Commons, share and share alike with attribution.
A Few Basics First
• Setting up your Web server
• Content Headers
• File Permissions
Copyright 2007 Byrne Reese.
Distributed under Creative Commons, share and share alike with attribution.
helloworld.cgi
#!/usr/bin/perl
print “Content-type: text/plain\n\n”;
print “Hello World”;
Copyright 2007 Byrne Reese.
Distributed under Creative Commons, share and share alike with attribution.
Perl and the Web
• Creating a web form – an HTML primer
• How Perl handles web requests
• Parsing web form input
Copyright 2007 Byrne Reese.
Distributed under Creative Commons, share and share alike with attribution.
CGI.pm
Don’t re-invent the wheel: use a Perl
module!
• Using CGI.pm
• print header;
• param(‘say_hello_to’)
Copyright 2007 Byrne Reese.
Distributed under Creative Commons, share and share alike with attribution.
Homework
Copyright 2007 Byrne Reese.
Distributed under Creative Commons, share and share alike with attribution.
Your First Assignment
This sounds simple, but…
• Setup a web server at home, or access
Berkeley’s network.
• Deploy the helloworld.cgi to that web
server
• Make sure it works!
Copyright 2007 Byrne Reese.
Distributed under Creative Commons, share and share alike with attribution.