Part 1: The Basics ------------------ Intro Capabilities of the language: Regexs, OOP, FP, unlimited data structures, The perl cycle - write -> run -> debug -> correct -> run "There is no king's road to mathematics", Euclid Output The print command. Note about semi-colons Expressions Basic operators : +, -, /, *, ( ... ), ., %, ** Note about using parenthesis with the print function Strings ( " ... " ) Basic Escape Sequences - \$ \" \\ \n The length() function The substr() function The int() function Note about converting numbers to strings and vice versa (you don't need to!) Variables Introduction to the concept of variables A scalar variable can hold anything Assigning a value to a variable Note about the "+=", "*=", "++" operators Getting the value of a variable. Note: the dollars are used both for lvalues and rvalues. Simple Examples: $a = "Hello world!\n"; print $a,$a,$a; Input The <> operator. Examples: print "Please enter your name:\n"; $name = <>; print ("Hello " . $name . "!\n"); The For Loop The for loop: for $i (1..100) Examples: Renaming h00.jpg to h000.jpg h01.jpg to h001.jpg etc. Printing the multiplication board. Conditionals: if .. elsif .. elsif .. else Examples: Testing if an input name starts with a. Condition Expressions: The numeric comparison operators The string comparison operators The && and || operators The While Loop Explanation Examples: ################################## print "Input a number:\n"; $number=<>; my $power_of_2 = 1; while ($power_of_2 < $number) { $power_of_2 = ($power_of_2*$power_of_2); } print ("The first power of 2 that is " . "greater than this number is " , $power_of_2, "\n"); ################################## Arrays: Syntax: @array, $array[$index], scalar(@array). The , operator. foreach Note about the fact that the for and foreach keywords are equivalent Note about the .. operator (without too much explanation) Examples: Part 2: More Advanced Topics ---------------------------- The for( .. ; .. ; ..) loop Note: next behaviour in it. Hashes Intro: what is a dictionary. Uses. Examples: Functions Intro: uses Syntax: Examples: Declaring local variables with my the "use strict" pragma Files: open A note about newlines print FILEHANDLE close join("",); chomp Examples: Regexps: Introduction: what they are good for. The // syntax. . * ? + ( ... ) | [ ... ] ^ $ s///; *? +? Flags: g i e Examples: Useful functions: split() Example: Finding the user-id of the user "shlomif". join() map Intro to the defualt variable $_ sort <=> and cmp grep References: Introduction: what they are, use and abuse. Example: the towers of hanoi. Some syntax: \ - takes a reference to an existing variable or a scalar value [ ] - a dynamic refernece to an array. Note about copying an array reference with [ @_ ]; { } - a dynamic reference to a hash. The de-referencing schem @{$ref}, ${$ref}, %{$ref} The various arrow operators: $ref->[$index], $ref->{$key} Examples: Dangers in using references. Using the perl debugger. Introduction - the purpose of debugging Invoking the debugger Stepping (n) and stepping in (n) setting breakpoints (b) continuing (c) Executing perl commands inside the debugger The x command. RTFM - the perldebug man page, the h command. Part 3: Modules and Objects --------------------------- Introduction Modular Perl Programming What are References to Functions and why they are important. Closures and their usefulness Modules and their importance Objects and their importance A note about the fact that there is more than one file References to functions Taking the reference of a function - \&mysub Calling a function from its reference - &{$ref}(...) or $ref->( ... ) Dynamic References to functions - sub { ... } Behaviour of functions inside functions (lexical scoping) Demonstrate the dispatch function Modules Specifying a namespace with the package directive The :: as a namespace separator Where to find a module importing modules - use My::Module; Accessing functions from a different module - My::Module::func(). Exporting Variables use Exporter; @EXPORT @EXPORT_OK Importing variables - use vars qw(...); and $My::Module::var. Why "my" won't work. BEGIN and END - global constructor and destructor for the package. Note: The main namespace. Objects Intro: what are objects and how they are used Intro 2: how it works behind the scenes The object is a reference to a hash Its members are the hash elements The reference is "blessed" to be associated with a module That module contain the methods of the object's class When a method name is called with $obj->method() it is searched for starting at a module class. Note: in perl, passing the reference to the object is done explicitily and it is the first argument of a method. Object use: (demonstrate on Data::Dumper, CGI) Initializing a new object Accessing class members using $obj->{'member'} Calling methods of an object using $obj->method( @args) Making your own objects: The constructor: ############# sub new { my $class = shift; my $self = {}; bless $self, $class; $self->initialize(); return $self; } ############ Explanation Since calling is CGI->new() then CGI is the first argument to to function bless After the blessing is done one can call methods on it. Note: its name need not be "new" Methods: ############## sub add_elems { my $self = shift; foreach my $elem (@_) { $self->{'sum'} += $elem; $self->{'sum_squares'} += $elem**2; $self->{'N'}++; } } ############### Object Inheritance: The use vars qw(@ISA); @ISA=qw(My::Class Parent); scheme Calling methods from base classes The SUPER directive ($self->SUPER::hello()); Destructors The DESTROY method isa() and can() End of lecture Part 4: CPAN, sprintf, system functions, excpetions --------------------------------------------------- * Installing and Using CPAN Modules * sprintf * Alternate forms for writing strings. - q{} qq{}, etc. - Here Documents - <{'name'} cmp $b->{'name'} || $a->{'address'} cmp $b->{'address'} - The "and" and "or" operators * Exceptions: - die - eval - The Carp module (carp, croak) - The module on CPAN * Some more system functions - opendir/readdir and friends - seek/tell - -X (-e, -f and friends) - chdir - mkdir - stat Part 5: Good Programming Practices: ----------------------------------- * Testing: - Motivation for testing. - Your code is probably not bug free from the start. - A contract for your code. - As a complement to documentation - Makes sure refactoring and enhancement do not introduce regressions. - Demo: - Download the skeleton. - Write tests using Test::More. - Type "./Build test". - Unit tests vs. Integration tests vs. System Tests. - Mocking. - Demo - Data-driven testing. - The older XP method of putting in HTML - Ask Ran Eilam about it. - Test::Base * Version Control - Motivation: - Not lose your code by accident. - Keep historical version of your code, for easy reverting. - Maintain several simultaneous lines of your code. - More convenient than patches+tarballs. - Demonstrate Subversion a bit and refer to a good tutorial (perhaps "Bottom-Up Subversion") * Class Accessors - Motivation - So you can later have control on setting and accessing a property. - In case it turns out to be calculated. - Because hash values can be mis-spelled. - Example for a simple accessor. - Accesor modules on CPAN: - Class::Accessor - Spiffy - not discussed. - Warning: lots of magic. - Moose. - Others: - Should mention some in passing. - Class::MethodMaker - Class::Methodist * Plugins, multi-inheritance, traits, etc. - NEXT -> MRO::Compat/mro. * Some Niceties in Advanced versions of Perl: - use base instead of @ISA - Lexical Filehandles. * local - Its judicious use and abuse. - Refer to "Coping with Scoping", MJD's Local article, my Linux-IL post * Documentation - POD - Demonstrate POD. - Refer to perldoc perlpod. - Literate Programming. - Why I'm not using it. (yet) - DocBook/XML - Extensions: - Pod::PseudoPod - MOD by Mark Jason Dominus. - on CPAN by Ingy. * IO::Handle: - IO::File - IO::Dir - IO::Socket - IO::All * Tying it all together - Module-Starter and Module-Build. - The module-starter invocation command. - arguments - Generate MyMath::Ops - What to do after we have the module. - perl Build.PL - ./Build - ./Build test - ./Build disttest. - ./Build dist - Add a meaningful MyMath::Ops::Add module. - Test first. - Get rid of the failures in t/boilerplate.t. * Summary. Part 6: Practical Perl Programming: ----------------------------------- * Working with Databases - SQL and DBI. - RDBMS concepts: - Tables. - Records - Indexes - Explanation about SQL with demonstration. - The SELECT statement - Object-Relational Mapping: - DBIx::Class * CGI Programming. - Overview of HTTP - Overview of CGI. - The modules. - CGI params. - path_info - other environment variables. - Form processing. - CGI::Application and other web frameworks. - Common vulnerabilities - SQL injection. - XSS - Path injection. (e.g: index.cgi?page=/etc/passwd) - Comment spam * Networking - Short explanation about sockets and TCP/IP - IO::Socket - POE (?) * Web Automation - LWP - HTML::TokeParser, HTML::TokeParser::Simple, HTML::TreeBuilder. - WWW::Mechanize - Give some demos. * GUI Design - Overview of: - Perl/Tk - Gtk+ - Qt - wxPerl - Some sample code using the Wx module.