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 - Day 3

Goals

In this session you will:
  • Use special printing functions
  • Use perl's report formatting
  • write reusable code in subroutines
  • become familiar with perl variable scope

Review

POST

  1. which special variable contains command-line arguments?
  2. what is the difference between ++$a and $a++
  3. what does keys() do?
  4. when do you use each versus foreach
  5. what is the difference between @_ and $_ ?

Use special printing functions

  • printf("[control string]", [list of vars]);
    
    format: %m.nx, where x is one of:
    
    c	character
    d	decimal integer (base 10)
    e	exponential float
    f	float
    g	compact float
    ld	long decimal
    lo	long octal
    lu	long unsigned decimal
    lx	long hex
    o	octal
    s	string
    u	unsigned decimal
    x	hex
    
  • sprintf([control string], [list], returns a string rather than printing it.

Use perl's report formatting

  1. define format (template, fieldlines, valuelines, fieldholders) p120-123
    
    format [leave at default, or use a NAME] =  UC, default if not named
    Hello, my name is @<<<<<<<<<<    field holder w/11 left-justified places
    $name valueline related to fieldline above
    My pencil is @<<<<<<<<<< and @>>>>  right-just, too
    $pencil_color,		     $pencil_size  lined up for humans
    . 1st column, on line by itself, like here-doc terminator 
    
  2. load up variables for printing
  3. invoke the format: write;

write reusable code in subroutines

You can store your subroutines in the current script, or externally (require "something-lib.pl"; like .h header files.
  1. write the subroutine
    
    sub addem 
    {
    my($first_num, $second_num)=@_; #grab the results of the default array 
    				#before it gets changed.
    						
    				#if you pass an @array, it must be the
    				#the last argument passed, or a greedy @array
    				#on the receiving end will eat it all, leaving
    				#nothing for subsequent vars in the list
    				#ex, ($var, @array) not (@array, $var)
    				#see p51	
    
    my($sum)= $first_num + $second_num;
    return $sum;
    
    # or just return ($first_num + $second_num);
    
    }#end, addem
    
    
    $total=&addem(4,5);	#if passing strings use quotes
    #or $total=&addem($some_num, $another_num);# already set values	
    #or $total=&addem(@nums); #already said that @nums=4, 5;
    

an actual example:


sub assignInput
	{
	#capture arguments
	local($line,$delimiter,@definition)=@_;

	#set local hash to hold data
	local(%filled);

	#strip off linefeed, if applicable 
	chomp($line); 
		    
	#split out line from input
	local(@separated) = split(/$delimiter/ ,$line);
			       
	#make association between @definition and the %hash to be returned
	while ($counter=0; $counter<=$#definition; $counter++)
		{
		$filled{ $definition[$counter] }=$separated[$counter];
		}

	#send the %filled hash back to calling program
	return %filled;

Homework

DBM 170-175
dir ops, file testing 129-140, 112-113


http://www.mousetrap.net/introperl/day3.html
$Id: day3.orb,v 1.3 2002/02/11 02:40:27 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.