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!

Intro to Perl Programming - Day 4

Goals

In this session you will:
  • learn to use DBM hashes
  • use file testing and directory ops
  • use a simple object-oriented module

POST

  • What are some differences between these two statements:
    $someVar = <DATA>;
    @someVar = <DATA>;
  • What are some differences between these two statements:
    $someVar = $array[0];
    $someVar = @array[0];
  • Spot the bug:
    
    format _TOP =
    @>>
    $id
    
    
  • Spot the bug:print("The chicken says "bock! bock! bock");
  • Spot the bug:@result = split(/:/, 'Mario;555-1212;Dayton, OH');
  • What does this syntax do? qq//
  • What does this symbol indicate? $
  • What does this symbol indicate? @
  • What does this symbol indicate? %
  • What does this symbol indicate? &

learn to use DBM hashes

DBM is a simple database structure common on unix boxes. It is common because of its simplicity and wide availability. This structure is used in many system files like sendmail aliases, apache .htpasswd files on larger systems, etc. File locking depends on the dbm implantation, so this tool is most common in situations where there is one entry point and multiple reads.
  • dbmopen(%hash, [dbm_name], [mode]);
  • dbmclose(%hash);
  • two-field workaround: compound value, ie: $office{11981}="John Price:1100 Elm Street:blah:foo:bar";

use file testing and directory ops

  • file testing for control structures: (-x [filename] ). See chart on 113
  • opendir(HANDLE, "path");
  • @filenames=readdir(HANDLE);
  • close(HANDLE);

use a simple object-oriented module - subroutine style

[perldoc Trig]
NAME
Math::Trig - trigonometric functions
        
SYNOPSIS
use Math::Trig;
	        
$x = tan(0.9);
$y = acos(3.7);
$z = asin(2.4);
							      
$halfpi = pi/2;
				      
$rad = deg2rad(120);

use a simple object-oriented module - method style

[perldoc Ping]
NAME
Net::Ping - check a remote host for reachability
        
SYNOPSIS
use Net::Ping;
		    
$p = Net::Ping->new();
print "$host is alive.\n" if $p->ping($host);
$p->close();
						      
$p = Net::Ping->new("icmp");
foreach $host (@host_array)
	{
	print "$host is ";
	print "NOT " unless $p->ping($host, 2);
	print "reachable.\n";
	sleep(1);
        }
$p->close();

...


 Net::Ping->new([$proto [, $def_timeout [, $bytes]]]);      
Create a new ping object.  All of the parameters are
optional.  $proto specifies the protocol to use when
doing a ping.  The current choices are "tcp", "udp" or
"icmp".  The default is "udp".

...

$p->ping($host [, $timeout]);
Ping the remote host and wait for a response.  $host
can be either the hostname or the IP number of the
remote host.  The optional timeout must be greater
than 0 seconds and defaults to whatever was specified
when the ping object was created.  If the hostname
cannot be found or there is a problem with the IP
number, undef is returned.  Otherwise, 1 is returned
if the host is reachable and 0 if it is not.  For all
practical purposes, undef and 0 and can be treated as
the same case.

looking ahead (optional material)

  1. references
    • to scalars
    • to arrays
    • to hashes
    • to subroutines and typeglobs
    • anonymous scalars, arrays, and hashes
  2. symbolic references
  3. Something.pm
  4. packages (namespace), exporting
  5. modules (reusable units of code)
  6. class objects

Conclusion

Where to go from here

  • Grab a perl interpreter
  • perl 2
  • debugger, profiler
  • use Modules;
  • write your own function libraries and modules


http://www.mousetrap.net/introperl/day4.html
$Id: day4.orb,v 1.6 2002/06/19 16:21:07 mouse Exp $


© 1994-2002 jason carr.
distributed under the terms of the GNU Free Documentation License.

jason carr

Reminders

  • Classroom temperature can be wildly variable. Dress lightly and bring layers.
  • your username is based on the class title and the last two digits of your workstation's hostname.
  • remember to take your work with you.