%					-*- LaTeX -*-
%\documentclass[landscape]{slides}
% $Id: perl-talk-slides.tex,v 1.2 1998/06/17 05:16:10 arensb Exp arensb $
\documentclass{slides}
\usepackage{graphics}
\usepackage{color}

\settoheight{\headheight}{%
  \includegraphics{logo-small.eps}
}
\newlength{\logowidth}
\settowidth{\logowidth}{\includegraphics{logo-small.eps}}
\setlength{\topmargin}{0pt}
%\setlength{\headheight}{2cm}
\makeatletter
\renewcommand{\ps@slide}{%
    \renewcommand{\@evenfoot}{\@empty}
%    \renewcommand{\@oddfoot}{\@empty}
\renewcommand{\@oddfoot}{\hfil\theslide}
    \renewcommand{\@evenhead}{\hfil UMIACS\hfil}%
%    \renewcommand{\@oddhead}{\hfil UMIACS\hfil}%
    \renewcommand{\@oddhead}{%
% XXX
      \includegraphics{logo-small.eps}%
%      \hfil
%      {\hskip-2em\large\textbf{\underline{\slidetitle}}}
\hskip 1em%
      {\large\textbf{\underline{\slidetitle}}}
      \hfil
    }
}
\newcommand{\slidetitle}{}

% This just redefines \slidetitle, so it'll show up in the page header
\newcommand{\heading}[1]{%
%  \begin{center}
%    \large\textbf{\underline{#1}}
%  \end{center}
%  \vspace{1ex minus 1ex}}
\renewcommand{\slidetitle}{#1}%
}

\newcommand{\inlineheading}[1]{%
%  \begin{center}
\hskip\logowidth
\hskip 1em
    {\large\textbf{\underline{#1}}}
%  \end{center}
  \vspace{1ex minus 1ex}\par}

\newcommand{\code}[1]{\texttt{#1}}
% Make curly braces come out right.
\renewcommand{\{}{\symbol{"7B}}
\renewcommand{\}}{\symbol{"7D}}
\newcommand{\bs}{\texttt{\symbol{"5C}}}	% Backslash
\renewcommand{\tilde}{\symbol{"7E}}	% Tilde
\newcommand{\amp}{\symbol{"26}}		% Ampersand
\newcommand{\vs}{\texttt{\char`\ }}	% Visible space
\newcommand{\perl}{Perl}

\newcommand{\bull}{$\bullet$\hskip8pt}
%\newlength{\bullwidth}
%\settowidth{\bullwidth}{$\bullet$\hskip8pt}
%\newcommand{\bull}{\hbox to\bullwidth{\hss$\bullet$\hskip8pt}}

\newcommand{\alttwo}[2]{%
\item[\vbox{\hbox to0pt{\hss#1}%
            \hbox to0pt{\hss#2}}]%
}
\newcommand{\altthree}[3]{%
\item[\vbox{\hbox to0pt{\hss#1}%
            \hbox to0pt{\hss#2}%
            \hbox to0pt{\hss#3}}]%
}

\newcommand{\bigitem}[1]{%
\item[\vbox{\hbox to\leftmargin{\hskip-\leftmargini#1\hss}
	    \hbox to0pt{\hss}}]%
}

%\def\@listi{\parsep .5\parskip
%            \topsep \parsep
%            \itemsep\parskip
%            \partopsep \z@}
% This is a hack to make fairly narrow 'itemize' lists
\def\@listi{\parskip 2pt
            \parsep .5\parskip
            \topsep \parsep
            \itemsep\parskip
            \partopsep \z@}

\newcommand{\undergroup}[1]{%
  \newdimen{\groupwidth}%
  \settowidth{\groupwidth}{#1}%
  \vtop{\hbox{#1}%
	\hbox{\raise3pt\hbox to\groupwidth{%
		% XXX
		$\bracelu$\leaders\vrule height2pt\hfill$\bracerd$%
		$\braceld$\leaders\vrule height2pt\hfill$\braceru$%
	}}}%
}

\begin{document}
\small
\pagestyle{slide}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{slide}
\begin{center}
{\Huge \perl\ Programming}\\
{\large Part 2}
\vskip1cm
{\large Andrew Arensburger}
\vskip1cm
{\code{http://www.cfar.umd.edu/\tilde arensb/text/perl/}}
\end{center}
\end{slide}

\begin{slide}
\heading{Packages}

\begin{itemize}
\item \code{package}~\textit{name} sets the default namespace.

\item \code{\$}\textit{package}\code{::}\textit{var} explicitly specifies
	the package.
\end{itemize}

\bigskip
\begin{quote}
\begin{verbatim}
package English;
$hello = "Hello";       # Sets $English::hello

package French;
$hello = "Bonjour";     # Sets $French::hello

package main;
print $hello;           # Wrong: $main::hello
                        # does not exist
print $French::hello;

package English;
print $hello;           # Prints $English::hello
\end{verbatim}%$
\end{quote}

\end{slide}

\begin{slide}
\heading{Packages (cont.)}
\begin{itemize}
\item You can use \code{package} anywhere.
\end{itemize}
\bigskip

\begin{quote}
\begin{verbatim}
if ($language eq "english") {
        package English;
        print $hello;   # Print $English::hello
}                       # End of English scope
elsif ($language eq "french") {
        package French;
        print $hello;   # Print $French::hello
}                       # End of French scope
\end{verbatim}
\end{quote}
\bigskip

\begin{itemize}
\item Packages can be nested.
\item This does not imply any relation between the outer and inner
        packages (but others may think it does).
\end{itemize}

\bigskip
\begin{quote}
\begin{verbatim}
package Lang;
$hello = "Huh?";

package Lang::English;
$hello = "Hello";

package Lang::French;
$hello = "Bonjour";
print $hello;   # Prints $Lang::French::hello
\end{verbatim}%$
\end{quote}
\end{slide}

\begin{slide}
\heading{\code{BEGIN} and \code{END}}
\begin{itemize}
\item Packages may contain \code{BEGIN} and \code{END} blocks.
\item \code{BEGIN} blocks are executed during compilation, as soon as
they are seen.
\item \code{END} blocks executed in reverse order of declaration.
\item \code{BEGIN} and \code{END} blocks are executed even if you're
only checking syntax!
\end{itemize}

\bigskip
%
\begin{quote}
\begin{verbatim}
package Moo;

print "Munch munch munch...\n";

BEGIN {
    print "Welcome to Moo.\n";
}
\end{verbatim}
\end{quote}

\bigskip
\begin{quote}
\begin{verbatim}
package Editor;
BEGIN {	open THEFILE, "/some/file"; }
END   {	close THEFILE; }

# Initialize lots of stuff.
$big_thing = &initialize();
END { &flush_changes($big_thing, THEFILE); }
\end{verbatim}%$
\end{quote}
\end{slide}

\begin{slide}
\heading{\code{AUTOLOAD}}
\begin{itemize}
\item \code{AUTOLOAD} will be called if \perl\ can't find a function.
\item Called with the arguments to the original function.
\item \code{\$AUTOLOAD} set to the name of the function.
\item \code{AUTOLOAD} function is responsible for handling errors.
\end{itemize}

\bigskip
\begin{quote}
\begin{verbatim}
package Adder;

sub AUTOLOAD {
    if ($AUTOLOAD !~ /add(\d+)$/)
    {
        die("No such function: $AUTOLOAD");
    }

    my $toadd  = $1;
    my $number = shift;

    return $number + $toadd;
}

package main;

$x = 1;
print &Adder::add1($x);
\end{verbatim}%$
\end{quote}
\end{slide}

\begin{slide}
\heading{Modules}
\begin{itemize}
\item \code{require "}\textit{filename}\code{"} loads a file.
\item \code{require}~\textit{filename} loads \textit{filename}\code{.pm}.
\item \code{@INC} specifies the search path.
\item \code{\%INC} lists loaded packages, to prevent multiple
inclusion.
\item A module should return a true value. Usually just put
%
\begin{quote}
\begin{verbatim}
1;
\end{verbatim}
\end{quote}
%
at the end.
\item Nested packages are translated into directories:
%
\begin{quote}
\begin{verbatim}
require Lang::French::Canadian;
\end{verbatim}
\end{quote}
%
loads \code{Lang/French/Canadian.pm}.
\end{itemize}
\end{slide}

\begin{slide}
\heading{Modules (cont.)}
\begin{itemize}
\item \code{use}~\textit{package [list]}
\item \code{use MyPackage 1, 2, 3;} is (almost) equivalent to
%
\begin{quote}
\begin{verbatim}
BEGIN {
    require MyModule;
    MyModule::import("MyModule", 1, 2, 3);
}
\end{verbatim}
\end{quote}
\item \code{use} is evaluated at compile-time.
\item \code{import} function provides a hook.
%
\bigskip
\begin{quote}
\begin{verbatim}
package Directories;

sub import {
    my $package = shift;

    # Add all arguments to the list of
    # include directories
    unshift @INC, @_;
}

1;
\end{verbatim}%$
\end{quote}
\bigskip
%
\item To avoid calling \code{import}, use
\begin{quote}
\begin{verbatim}
use MyModule();
\end{verbatim}
\end{quote}
%
\item \code{no}~\textit{package}: similar to \code{use}, but calls
	\code{unimport} (rarely used).
\end{itemize}
\end{slide}

\begin{slide}
\heading{Symbolic References}
Consider:
\begin{quote}
\begin{verbatim}
sub nroff { ... };
sub troff { ... };

if ($using_nroff) {
    &nroff("-ms", $filename);
} else {
    &troff("-ms", $filename);
}
\end{verbatim}%$
\end{quote}

\begin{itemize}
\item In a \code{sh} script, you'd just say
%
\begin{quote}
\begin{verbatim}
$TYPESETTER -ms $filename
\end{verbatim}%$
\end{quote}
%
\item Use
%
\begin{quote}
\code{\$}\textit{expr}\\
\code{\$\{}\textit{expr}\code{\}}
\end{quote}

%
\bigskip
\begin{quote}
\begin{verbatim}
$var = 3;
$ref = "var";
$$ref = 4;                  # Sets $var
${$ref} = 5;                # Ditto
${$ref."two"} = 6;          # Sets $vartwo
@$ref = ('a','b','c');      # Sets @var
@refs = ("foo", "bar", "baz");
${$refs[0]} = "howdy";      # Sets $foo
${$refs[0]}[0] = "quux";    # Sets $foo[0]
\end{verbatim}%$
\end{quote}
\end{itemize}
\end{slide}

\begin{slide}
\heading{Symbolic References (cont.)}

The earlier example can be rewritten as
%
\begin{quote}
\begin{verbatim}
sub nroff { ... };
sub troff { ... };
sub groff { ... };

   if ($using_nroff) { $typesetter = "nroff" }
elsif ($using_troff) { $typesetter = "troff" }
elsif ($using_groff) { $typesetter = "groff" }

&$typesetter("-ms", $filename);
\end{verbatim}
\end{quote}
\bigskip

\begin{itemize}
\item Symbolic references don't always work:
%
%
\begin{quote}
\begin{verbatim}
package Passwd;

sub expand_gcos {
    my $username = shift;
    my $gcosref  = shift;

    $$gcosref =~ s/&/$username/;
                # Modifies $Passwd::name, not
                # $main::name
}

package main;

$name = "andrew &urger";
&Passwd::expand_gcos("arensb", "name");
\end{verbatim}
\end{quote}
%
\item Turn them off with \code{use strict 'refs'}.
\end{itemize}
\end{slide}

\begin{slide}
\heading{Hard References}

\begin{itemize}
\item Hard references work like pointers in C, but safer.
\item They can only point to existing things.
\item No pointer arithmetic.
\item Everything is a thingy.
\item Use \code{\bs}\textit{var} to get the address of \textit{var}.
\end{itemize}

\bigskip
\begin{quote}
\begin{verbatim}
$ref    = \$myvar;
$$ref   = 3;            # Sets $myvar
${$ref} = 4;            # Ditto

@array       = (1, 2, 3);
$arrayyref   = \@array;
shift @{$arrayyref};    # shift @array;
$elementref  = \$array[1];
$$elementref = "two";   # @array is now
                        # (1, "two", 3)

sub hi { print "hello" }
$funcref = \&hi;
&$funcref;              # Calls &hi;
\end{verbatim}%$
\end{quote}
\end{slide}

\begin{slide}
\heading{\code{ref}}

\begin{itemize}
\item Can't use a scalar reference as an array, etc.
\item \code{ref(}\textit{reference}\code{)} returns the type of what
\textit{reference} points to, or empty string:
\begin{quote}
\code{REF}\\
\code{SCALAR}\\
\code{ARRAY}\\
\code{HASH}\\
\code{CODE}\\
\code{GLOB}\\
Other.
\end{quote}
%
\item If interpolated into a string, \code{ref} expands to a unique string.
\end{itemize}
\end{slide}

\begin{slide}
\heading{Anonymous References}
Anonymous array:
%
\begin{quote}
\begin{verbatim}
$arrayref = [ 'a', 'b', 'c' ];
\end{verbatim}%$
\end{quote}

Anonymous hash:
%
\begin{quote}
\begin{verbatim}
$hashref = { Jan => 31,
             Feb => 29,
             Mar => 31,
             Apr => 30,
           }
\end{verbatim}%$
\end{quote}

Anonymous function:
%
\begin{quote}
\begin{verbatim}
$funcref = sub { print "Hello"; }
\end{verbatim}%$
\end{quote}
\bigskip

\begin{itemize}
\item A reference is a scalar.
\item Hence, you can put references in arrays and hashes.
\item Hence, you can build complex data structures:
%
\begin{quote}
\begin{verbatim}
@array2D = (            # Array of arrays
        [ 0, 1, 2 ],
        [ 'a', 'b', 'c', 'd', 'e' ],
        [ 31, [28, 29], 31, 30, 31, 30,
          31, 31, 30, 31, 30, 31
        ],
);
\end{verbatim}
\end{quote}
\end{itemize}
\end{slide}

\begin{slide}
\heading{An Ugly Example}
\begin{quote}
\begin{verbatim}
$complex = 
        [ [ "Unix",
            "operating system",
            { gurus => [ "Brian",
                         "Dennis",
                         "Ken"
                       ]
            }
          ],
          [ "Perl",
            "language",
            { author => "Larry",
              gurus  => [ "Larry",
                          "Tom",
                          "Randal"
                        ]
            }
          ]
        ];
\end{verbatim}%$
\end{quote}

How do you get at \code{"Randal"}?
\end{slide}

\begin{slide}
\heading{An Ugly Example (cont.)}

Begin with
%
\begin{quote}
\begin{verbatim}
              $complex
\end{verbatim}%$
\end{quote}
%
Dereference this:
%
\begin{quote}
\begin{verbatim}
            @{$complex}
\end{verbatim}%$
\end{quote}
%
and subscript the result:
%
\begin{quote}
\begin{verbatim}
            ${$complex}[1]
\end{verbatim}%$
\end{quote}
%
Dereference and subscript again:
%
\begin{quote}
\begin{verbatim}
          ${${$complex}[1]}[2]
\end{verbatim}%$
\end{quote}
%
Dereference again:
%
\begin{quote}
\begin{verbatim}
        %{${${$complex}[1]}[2]}
\end{verbatim}%$
\end{quote}
%
and subscript:
%
\begin{quote}
\begin{verbatim}
        ${${${$complex}[1]}[2]}{"gurus"}
\end{verbatim}%$
\end{quote}
%
Dereference and subscript again:
%
\begin{quote}
\begin{verbatim}
      ${${${${$complex}[1]}[2]}{"gurus"}}[2]
\end{verbatim}%$
\end{quote}
\end{slide}

\begin{slide}
\heading{An Ugly Example (cont.)}
\begin{itemize}
\item \code{->} syntactic sugar:
%
\begin{quote}
\begin{verbatim}
$ $arrayref  [0]
${$arrayref} [0]
  $arrayref->[0]

$ $hashref  {string}
${$hashref} {string}
  $hashref->{string}
\end{verbatim}%$
\end{quote}
%
\item This only works for scalars.
\end{itemize}

\bigskip
Simplifying the earlier expression:
\begin{quote}
\code{\$\{\$\{\$\{\undergroup{\$\{\$complex\}\ [1]}\}\ [2]\}\ \{"gurus"\}\}\ [2]}\\
\code{\$\{\$\{\undergroup{\$\{\ \ \$complex->[1]\}\ [2]}\}\ \{"gurus"\}\}\ [2]}\\
\code{\$\{\undergroup{\$\{\ \ \ \ \$complex->[1]->[2]\}\ \{"gurus"\}}\}\ [2]}\\
\code{\undergroup{\$\{\ \ \ \ \ \ \$complex->[1]->[2]->\{"gurus"\}\}\ [2]}}\\
\code{\hbox to0pt{}\ \ \ \ \ \ \ \ \$complex->[1]->[2]->\{"gurus"\}->[2]}\\
\end{quote}
\bigskip

\begin{itemize}
\item The \code{->} can be omitted between successive subscripts:
%
\begin{quote}
\code{\$complex->[1][2]\{"gurus"\}[2]}
\end{quote}
%
\item Use the explicit form for debugging.
\item Use the clearer form once it works.
\end{itemize}
\end{slide}

\begin{slide}
\heading{Closures}

\begin{quote}
\begin{verbatim}
package Numbers;

$counter = 0;   # Gratuitous counter

sub makefunc {
    my $divisor = shift;

    # Build a function on the fly, and return it
    return sub {
        my $number = shift(@_);

        $counter++;

        # Return true if $number is
        # evenly divisible by $divisor
        return ($number % $divisor == 0);
    };
}

package main;

$is_even   = &Numbers::makefunc(2);
$isdivby10 = &Numbers::makefunc(10);

foreach $n (1, 2, 3, 4, 5)
{
    print "$n is ",
        &$is_even($n) ? "even" : "odd",
        "\n";
}
\end{verbatim}%$
\end{quote}
\end{slide}

\begin{slide}
\heading{Closures (cont.)}
\begin{itemize}
\item A closure knows what context it was defined in.
\item Anonymous functions are closures.
\item \code{\&is\_even} is defined in \code{Numbers} but called in \code{main}.
\item Which variables, precisely, is it using?
\item \code{\$number} is \code{my} in \code{\&\$is\_even}; it is
private.
\item \code{\$counter} is \code{\$Numbers::counter}. Just look at the
code.
\item \code{\$divisor} is \code{my} in
\code{makefunc}. \code{\&\$is\_even} has a private copy.
\item \code{\$divisor}'s initial value is whatever it was when the
anonymous function was created.
\item Private copies are independent.
\end{itemize}
\end{slide}

\begin{slide}
\heading{Historical Interlude}
In the beginning:
\begin{itemize}
\item \code{JMP} begat \code{GOTO}.
\item No dynamic memory allocation.
\item No complex arrays.
\end{itemize}

The 1960's:
\begin{itemize}
\item Structured Programming.
\item Dijkstra, \textsl{Goto Statement Considered Harmful}.
\item Code should be divided into atomic blocks.
\item One entrance, one exit.
\item Not so fervent about that, nowadays.
\end{itemize}

Code can be grouped, why not data?
\begin{itemize}
\item Encapsulation: \code{struct}s.
\item API functions: advertised means of access.
\end{itemize}

Inheritance:
\begin{itemize}
\item \textit{isa} relationship.
\item Polymorphism (often problematic).
\end{itemize}

Encapsulation + Inheritance = Objects
\end{slide}

\begin{slide}
\heading{\vbox{\hbox{Structured Programing}%
               \hbox{Example 1}}}
%\tiny
\begin{verbatim}
sub count_elements {
    my @list = @_;          # List of elements
    my $i;
    my $num_elements;       # Return value
    my $seen_end = 0;       # Have we reached the end
                            # of the list?

    if (@list == ()) {
        $num_elements = 0;
    } else {
        # @list is not empty
        for ($i = 0; ($i <= $#list) && !$seen_end; $i++) {
            if (&is_invalid($list[$i])) {
                # Do nothing
            } else {
                # Check for end of list
                if (&is_end_of_list($list[$i])) {
                    $seen_end = 1;
                } else {
                    # Current element is valid
                    $num_elements++;
                }
            }
            $i++;
        }
    }
    return $num_elements;
}
\end{verbatim}%$
\end{slide}

\begin{slide}
\heading{\vbox{\hbox{Structured Programing}%
               \hbox{Example 2}}}
Without the one-exit rule:
\begin{verbatim}
sub count_elements {
    my @list = @_;          # List of elements
    my $i;
    my $num_elements = 0;   # Number of elements in list

    return 0 if @list == ();# Trivial case

    for ($i = 0; $i <= $#list; $i++) {
        next if &is_invalid($list[$i]);
                            # Ignore invalid elements
        last if &is_end_of_list($list[$i]);
                            # Nothing useful after this
        $num_elements++;
    }
    return $num_elements;
}
\end{verbatim}%$
\bigskip
%
Encapsulation:
%
\begin{quote}
\begin{verbatim}
/* Sales information */
int     serial_no[1024];
double  price[1024];
char *  customer[1024];
\end{verbatim}
\end{quote}
%
\textit{vs.}
%
\begin{quote}
\begin{verbatim}
struct {
    int	    serial_no[1024];
    double  price[1024];
} sales[1024];
char *  customer[1024];
\end{verbatim}
\end{quote}
\end{slide}

\begin{slide}
\heading{Objects}

\begin{itemize}
\item Each thingy can be labeled with a package name.
\item When this happens, it's called an object.
\item Use \code{bless}~\textit{ref [}\code{,}\textit{package]}
%
\begin{quote}
\begin{verbatim}
package Sneeze;

$var = [ "achoo" ];
bless $var, Sneeze;
\end{verbatim}
\end{quote}
%
\item A class is just a package that happens to define methods.
\item A method is just a function.
\item Class methods take a class name as their first argument.
\item Instance methods take an object reference as their first
argument.
\item A single function can be a plain function, a class method, and
an instance method.
\end{itemize}
\end{slide}

\begin{slide}
\heading{Class methods}
%\begin{quote}
\tiny
\begin{verbatim}
package User;

# Create a new User
sub new {
    my $class = shift;          # Class name
    my $newthing = {};          # The new object

    bless $newthing, $class;    # Bless the object
    return $newthing;
}

# Look up a User by username
sub lookup {
    my $class = shift;          # Class name
    my $name  = shift;          # Name to look for
    my $newuser  = {};          # The new object

    # Look up the User
    my ($uname, $passwd, $uid, $gid) =
        getpwnam($name);
    $newuser->{"uname"} = $uname;
    $newuser->{"uid"}   = $uid;
    $newuser->{"gid"}   = $gid;

    bless $newuser, $class;     # Bless the object
    return $newuser;
}

package main;

$user1  = new User;
$arensb = lookup User "arensb";
$bin    = lookup User ("bin");

$user2  = User->new;
$root   = User->lookup("root");
\end{verbatim}%$
%\end{quote}
\end{slide}

\begin{slide}
\heading{Instance Methods}
%\begin{quote}
\tiny
\begin{verbatim}
package User;

sub get_fullname {
    my $self = shift;           # The object
    my $fullname;               # Return value

    $fullname = $self->{"gcos"};    # Get the GCOS field
    $fullname =~ s/&/\u$self->{uname}/;
                                # Replace & with username
    return $fullname;
}

sub add_to_groups {
    my $self   = shift;         # The object
    my @groups = shift;         # Groups to add user to

    push @{$self->{"groups"}}, @group;
}

package main;

$user = new User;
get_fullname $user;
add_to_group $user "operator", "bin";

$arensb = User->lookup("arensb");
$arensb->get_fullname;
$arensb->add_to_group("wheel");
\end{verbatim}%$
%\end{quote}
\end{slide}

\begin{slide}
\heading{Inheritance}

\begin{itemize}
\item Inheritance allows packages to inherit other packages'
functions.
\item \code{@ISA} lists parent packages.
\item \code{@ISA} traversed depth-first.
\item If a function isn't found, \perl\ looks for \code{AUTOLOAD} in
	\code{@ISA}
\item If it still can't find it, looks in \code{UNIVERSAL}.
\end{itemize}

\bigskip
\begin{quote}
\begin{verbatim}
package Child;
@ISA = qw( Mother Father );

package main;
$billy = new Child;
$billy->say("Hello");
Father::say("Da-da!");          # Don't use this
$billy->Father::say("Da-da!");  # Use this
\end{verbatim}%$
\end{quote}
\end{slide}

\begin{slide}
\heading{Inheritance (cont.)}

\begin{itemize}
\item Use the \code{SUPER} pseudo-class to pass the buck:
%
\begin{quote}
\begin{verbatim}
package FilledPolygon;
@ISA = qw( Polygon );

sub draw {
    my $self = shift;

    # Look for parent's draw()
    $self->SUPER::draw;
    &fillme;
}
\end{verbatim}
\end{quote}
%
\item Use the two-argument form of \code{bless}.
\item Don't assume you know what you're blessing into:
\begin{quote}
\begin{verbatim}
package Parent;

sub new {
    my $newobject = {};

    # Bless into current package (wrong!)
    bless $newobject;
    return $newobject;
}

package Child;
@ISA = qw( Parent );

package main;

$thing = new Child;
# $thing is a Parent, not a Child
\end{verbatim}%$
\end{quote}
%
\end{itemize}
\end{slide}

\begin{slide}
\heading{Objects (cont.)}
Destructors:
\begin{itemize}
\item Destructors are named \code{DESTROY}.
\item Destructors are not chained automatically.
\item But they're rarely used.
\end{itemize}

Privacy:
\begin{itemize}
\item \perl\ isn't fascist.
\item There is no built-in privacy enforcement.
\item Use comments and documentation (or lack thereof).
\end{itemize}
\end{slide}

\begin{slide}
\heading{Odds and Ends}
\inlineheading{\code{eval}}
\begin{quote}
\code{eval}~\textit{string}\\
\code{eval}~\textit{block}
\end{quote}

\begin{itemize}
\item Parses and runs its argument.
\item Catches \code{die} (check \code{\$@}).
\item Use the block form if you can.
\end{itemize}

\inlineheading{Typeglobs}

\begin{itemize}
\item Not terribly useful these days.
\item \code{*var} refers to \emph{all} variables named \code{var}.
\item To make everything named \code{foo} an alias for \code{bar}:
%
\begin{quote}
\begin{verbatim}
*foo = *bar;
\end{verbatim}
\end{quote}
%
\item To make \code{\$foo} an alias for \code{\$bar}:
%
\begin{quote}
\begin{verbatim}
*foo = \$bar;
\end{verbatim}
\end{quote}
%
\item Also useful for creating references to filehandles:
%
\begin{quote}
\begin{verbatim}
open(THEFILE, ">/my/file");
$fh = \*THEFILE;
print $fh "Hello, world!\n";
\end{verbatim}
\end{quote}
\end{itemize}
\end{slide}

\begin{slide}
\heading{Tied Variables}
\begin{itemize}
\item Allows you to control precisely how a variable works.
\item Database files are usually accessed through tied hashes.
\item All other uses are suspect.
\end{itemize}

\inlineheading{Scalars}
\begin{itemize}
\item[] \code{TIESCALAR} \textit{class}\code{,}\textit{list}
\item[] \code{FETCH} \textit{this}
\item[] \code{STORE} \textit{this}\code{,} \textit{value}
\item[] \code{DESTROY} \textit{this}
\end{itemize}

\inlineheading{Arrays}
\begin{itemize}
\item[] \code{TIEARRAY} \textit{class}\code{,}\textit{list}
\item[] \code{FETCH} \textit{this}\code{,} \textit{index}
\item[] \code{STORE} \textit{this}\code{,} \textit{index}\code{,} \textit{value}
\item[] \code{DESTROY} \textit{this}
\end{itemize}

\inlineheading{Hashes}
\begin{itemize}
\item[] \code{TIEHASH} \textit{class}\code{,} \textit{list}
\item[] \code{FETCH} \textit{this}\code{,} \textit{key}
\item[] \code{STORE} \textit{this}\code{,} \textit{key}\code{,} \textit{value}
\item[] \code{DELETE} \textit{this}\code{,} \textit{key}
\item[] \code{CLEAR} \textit{this}
\item[] \code{EXISTS} \textit{this}\code{,} \textit{key}
\item[] \code{FIRSTKEY} \textit{this}
\item[] \code{NEXTKEY} \textit{this}\code{,} \textit{lastkey}
\item[] \code{DESTROY} \textit{this}
\end{itemize}
\end{slide}

\begin{slide}
\heading{Style}
\begin{itemize}
\item Check for odd conditions early, so you can make simplifying
	assumptions.
\item Use \code{next} and \code{last} to break out of loops early.
\item \code{return} early.
\item Put functions at the end; make the main body easy to find.
\item Put customization variables at the top.
\item Choose wisely between prefix and postfix conditionals.
\item Choose wisely between \code{if} and \code{unless}:
%
\begin{quote}
\begin{verbatim}
next unless defined($key);
\end{verbatim}%$
\end{quote}
%
vs
%
\begin{quote}
\begin{verbatim}
next if !defined($key);
\end{verbatim}%$
\end{quote}
%
\item You almost certainly want \code{my}, not \code{local}.
\end{itemize}
\end{slide}

\begin{slide}
\heading{Further Reading}

\begin{itemize}
\item Man page roadmap. See perl(1).
\item Command-line arguments. See perlrun(1).
\item PODs. See perlpod(1).
\item Tricks and techniques. See perlbot(1), but mainly other people's
	code.
\item Embedding and extending. Start with perlembed(1).
\end{itemize}

\vskip1in
\begin{quote}
\begin{verbatim}
srand(@}=split $},"Pe thoe\nrslrtcaeu knrJa h");
print  splice  @},  rand @}  =>0<= 1  while  @};
\end{verbatim}%$
\end{quote}
\end{slide}
\end{document}
