Copyright 2007 Byrne Reese. Distributed under Creative Commons, share and share alike with...

23
Copyright 2007 Byrne Reese. Distributed under Creative Commons, share and share alike with attribution. Intermediate Perl Programming Class One Instructor: Byrne Reese X401

Transcript of Copyright 2007 Byrne Reese. Distributed under Creative Commons, share and share alike with...

Page 1: Copyright 2007 Byrne Reese. Distributed under Creative Commons, share and share alike with attribution. Intermediate Perl Programming Class One Instructor:

Copyright 2007 Byrne Reese. Distributed under Creative Commons, share and share alike with attribution.

IntermediatePerl Programming

Class One

Instructor: Byrne Reese

X401

Page 2: Copyright 2007 Byrne Reese. Distributed under Creative Commons, share and share alike with attribution. Intermediate Perl Programming Class One Instructor:

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/

Page 3: Copyright 2007 Byrne Reese. Distributed under Creative Commons, share and share alike with attribution. Intermediate Perl Programming Class One Instructor:

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.

Page 4: Copyright 2007 Byrne Reese. Distributed under Creative Commons, share and share alike with attribution. Intermediate Perl Programming Class One Instructor:

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

Page 5: Copyright 2007 Byrne Reese. Distributed under Creative Commons, share and share alike with attribution. Intermediate Perl Programming Class One Instructor:

Copyright 2007 Byrne Reese. Distributed under Creative Commons, share and share alike with attribution.

“Required” Reading

• Perl CookbookSecond Editionby Tom Christiansen, Nathan Torkington

Page 6: Copyright 2007 Byrne Reese. Distributed under Creative Commons, share and share alike with attribution. Intermediate Perl Programming Class One Instructor:

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!

Page 7: Copyright 2007 Byrne Reese. Distributed under Creative Commons, share and share alike with attribution. Intermediate Perl Programming Class One Instructor:

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!

Page 8: Copyright 2007 Byrne Reese. Distributed under Creative Commons, share and share alike with attribution. Intermediate Perl Programming Class One Instructor:

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.

Page 9: Copyright 2007 Byrne Reese. Distributed under Creative Commons, share and share alike with attribution. Intermediate Perl Programming Class One Instructor:

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

Page 10: Copyright 2007 Byrne Reese. Distributed under Creative Commons, share and share alike with attribution. Intermediate Perl Programming Class One Instructor:

Copyright 2007 Byrne Reese. Distributed under Creative Commons, share and share alike with attribution.

Getting Started

A Perl Primer

Page 11: Copyright 2007 Byrne Reese. Distributed under Creative Commons, share and share alike with attribution. Intermediate Perl Programming Class One Instructor:

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

Page 12: Copyright 2007 Byrne Reese. Distributed under Creative Commons, share and share alike with attribution. Intermediate Perl Programming Class One Instructor:

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…

Page 13: Copyright 2007 Byrne Reese. Distributed under Creative Commons, share and share alike with attribution. Intermediate Perl Programming Class One Instructor:

Copyright 2007 Byrne Reese. Distributed under Creative Commons, share and share alike with attribution.

Hello World

#!/usr/bin/perlprint “Hello World”;

Page 14: Copyright 2007 Byrne Reese. Distributed under Creative Commons, share and share alike with attribution. Intermediate Perl Programming Class One Instructor:

Copyright 2007 Byrne Reese. Distributed under Creative Commons, share and share alike with attribution.

Hello World

#!/usr/bin/perlmy $say_hello_to = $ARGV[0];$say_hello_to ||= “World”;print “Hello $say_hello_to\n”;

Page 15: Copyright 2007 Byrne Reese. Distributed under Creative Commons, share and share alike with attribution. Intermediate Perl Programming Class One Instructor:

Copyright 2007 Byrne Reese. Distributed under Creative Commons, share and share alike with attribution.

Hello World

#!/usr/bin/perluse Getopt::Long;my $say_hello_to = “World”;GetOptions(“to=s"=>\$say_hello_to);print “Hello $say_hello_to\n”;

Page 16: Copyright 2007 Byrne Reese. Distributed under Creative Commons, share and share alike with attribution. Intermediate Perl Programming Class One Instructor:

Copyright 2007 Byrne Reese. Distributed under Creative Commons, share and share alike with attribution.

Hello World

#!/usr/bin/perluse 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”;

Page 17: Copyright 2007 Byrne Reese. Distributed under Creative Commons, share and share alike with attribution. Intermediate Perl Programming Class One Instructor:

Copyright 2007 Byrne Reese. Distributed under Creative Commons, share and share alike with attribution.

Hello World on the Web

Page 18: Copyright 2007 Byrne Reese. Distributed under Creative Commons, share and share alike with attribution. Intermediate Perl Programming Class One Instructor:

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

Page 19: Copyright 2007 Byrne Reese. Distributed under Creative Commons, share and share alike with attribution. Intermediate Perl Programming Class One Instructor:

Copyright 2007 Byrne Reese. Distributed under Creative Commons, share and share alike with attribution.

helloworld.cgi

#!/usr/bin/perlprint “Content-type: text/plain\n\n”;print “Hello World”;

Page 20: Copyright 2007 Byrne Reese. Distributed under Creative Commons, share and share alike with attribution. Intermediate Perl Programming Class One Instructor:

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

Page 21: Copyright 2007 Byrne Reese. Distributed under Creative Commons, share and share alike with attribution. Intermediate Perl Programming Class One Instructor:

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’)

Page 22: Copyright 2007 Byrne Reese. Distributed under Creative Commons, share and share alike with attribution. Intermediate Perl Programming Class One Instructor:

Copyright 2007 Byrne Reese. Distributed under Creative Commons, share and share alike with attribution.

Homework

Page 23: Copyright 2007 Byrne Reese. Distributed under Creative Commons, share and share alike with attribution. Intermediate Perl Programming Class One Instructor:

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!