squeak!
Syllabus Homepage
Course Overview
Course resources
Day 1
Day 2
Day 3
Day 4
Common errors
Internet Glossary
About Your Instructor
Credits: This site powered by the vi text editor, apache webserver, perl scripting, and Debian linux.
squeak!

Intermediate Perl - Day 1

Admin

Texas state survey and Roll.

Goals

In this session you will:
  • review variable scope
  • review subroutine argument passing
  • learn argument prototyping
  • learn to use symbolic and hard references
  • build anonymous structures
  • learn to dereference
  • use the perl debugger

review: variable scope

  • global (actually in main::)
  • my (preferred): scope is limited to the block or function
  • local (deprecated): like my, only called functions can also see the variable. Use my except in those rare instances where it is too restrictive (mainly in formats).

review: subroutine passing

  • passing by value vs. reference
  • @arrays are greedy and will slurp up everything, given the chance:
    • pass the array at the end
    • limit the array with slice notation
    • pass a reference (see below)
    • use prototyping (see below)
  • %hashes are flattened into 2D when passing; only one hash is easily passed (again, use a reference)
  • incoming args found in @_ (the default array)

Practicum: subroutine passing

prototyping

Normal subroutine:
sub Example {
	#do nifty stuff in here;
}
Prototyped subroutine:
#	this sub requires two scalars and allows
#	for an optional array
#	note that all could be stored in one big list
sub Example ($$;@){
	#do nifty stuff in here;
}
Notes:
  • The above example would allow the scalars to be passed in an array (like @someArgs). To enforce that separate datatypes get passed, use a backslash in front of the marker: $$;\@
  • the prototyping only works when you call the subroutines without the &.
  • the prototype must precede the call, either by code or by require in the code before the sub is called.

references

Perl references are scalars that contain a "reference" (think: pointer) to another variable.
Description Definition Accessing
scalar$scalar = 'frogsticker';print $scalar;
array@array = ('frogsticker', 'toadjumper');print $array[0];
print @array;
hash %hash = ('mynickname', 'frogsticker', 'yournickname', 'toadjumper');

%hash = (mynickname => 'frogsticker', yournickname => 'toadjumper');

print $hash{mynickname};
scalar reference$scalarRef = \$scalar;print $$scalar;
array reference$arrayRef = \@arrayprint $$arrayRef[0];
print $arrayRef->[0];
print @$arrayRef;
hash reference$hashRef = \%hash;print $$hashRef{mynickname};
$hashref->{'mynickname'};
constant reference$constant = \'frogsticker';print $constant;
anonymous array reference$anonArrayRef = ['frogsticker', 'toadjumper']print $$anonArrayref[0];
print $anonArrayRef->[0];
print @$anonArrayRef;
anonymous hash reference$hashRef = {mynickname => 'frogsticker', yournickname => 'toadjumper'};print $$anonHashRef{mynickname};
print $anonHashRef->{'mynickname'};

writing better code

  • use strict;
  • use warnings;

perl debugger

The perl debugger is built into the interpreter. Your code must run before it can be debugged.

  • leverages behavior of gdb, etc
  • call it with the -d switch
  • waits before each statement
  • h help
  • p [expression] prints the value of [expression]
  • V [package] [vars] prints out variables
  • T stack backtrace: tells what a subroutine was called from
  • s step into subroutines: next statement, including statements in subroutines
  • n next statement: as above only does not enter subroutines
  • [enter] repeats last step/next
  • L List breakpoints.
  • t toggle trace mode; turns on codeline printing
  • b 10 break on line 10
  • b $counter > 5 break when $counter is greater than 5
  • b SUB foo break on invocation of &foo
  • d 10 delete breakpoint on 10
  • D delete all breakpoints
  • W delete all watches
  • W [var] set a watch on [var]

  • man perldebug
  • perl -Dr regular expression dissection

Homework

  • namespace (packages)
  • read comp.lang.perl


http://www.mousetrap.net/syllabus/interperl/day1.html
$Id: day1.orb,v 1.7 2002/08/18 04:56:41 mouse Exp $

Remember, your login is based on your machine's hostname, not on any other number.
~/[initials] refers to the subdirectory under your homedir, named after your initials. Everything except for .dotfiles will be stored in your ~/[initials] directory.


© 1995-2001 jason carr
Distributed under the terms of the GNU Free Documentation License.