81
Lecture 10 Perl (cont.) UNIX Development Tools

Lecture 10 Perl (cont.) UNIX Development Tools. Perl (Part II) Last week: –Perl data types Scalar $x List @x Hash %x

  • View
    220

  • Download
    1

Embed Size (px)

Citation preview

Page 1: Lecture 10 Perl (cont.) UNIX Development Tools. Perl (Part II) Last week: –Perl data types Scalar $x List @x Hash %x

Lecture 10

Perl (cont.)

UNIX Development Tools

Page 2: Lecture 10 Perl (cont.) UNIX Development Tools. Perl (Part II) Last week: –Perl data types Scalar $x List @x Hash %x

Perl (Part II)

• Last week:– Perl data types

• Scalar $x• List @x• Hash %x

Page 3: Lecture 10 Perl (cont.) UNIX Development Tools. Perl (Part II) Last week: –Perl data types Scalar $x List @x Hash %x

Subroutines

• sub myfunc { … }$name=“Jane”;

sub print_hello {

print “Hello $name\n”; # global $name

}

&print_hello; # print “Hello Jane”

print_hello; # print “Hello Jane”

hello(); # print “Hello Jane”

Page 4: Lecture 10 Perl (cont.) UNIX Development Tools. Perl (Part II) Last week: –Perl data types Scalar $x List @x Hash %x

Arguments• Parameters are assigned to the special array @_• Individual parameter can be accessed as $_[0], $_[1], …

sub sum {my $x; # private variable $xforeach (@_) { # iterate over params

$x += $_;}return $x;

}$n = &sum(3, 10, 22); # n gets 35

Page 5: Lecture 10 Perl (cont.) UNIX Development Tools. Perl (Part II) Last week: –Perl data types Scalar $x List @x Hash %x

More on Parameter Passing• Any number of scalars, lists, and hashes can be passed to a

subroutine• Lists and hashes are “flattened”

func($x, @y, %z);– Inside func:

• $_[0] is $x• $_[1] is $y[0]• $_[2] is $y[1], etc.

• The scalars in @_ are implicit aliases (not copies) of the ones passed, i.e. changing the values of $_[0], etc. changes the original variables

Page 6: Lecture 10 Perl (cont.) UNIX Development Tools. Perl (Part II) Last week: –Perl data types Scalar $x List @x Hash %x

Return Values

• The return value of a subroutine is the last expression evaluated, or the value returned by the return operatorsub myfunc {my $x = 1;$x + 2; #returns 3

}

• Can also return a list: return @somelist;• If return is used without an expression (failure), undef

or () is returned depending on context

sub myfunc {my $x = 1;return $x + 2;

}

Page 7: Lecture 10 Perl (cont.) UNIX Development Tools. Perl (Part II) Last week: –Perl data types Scalar $x List @x Hash %x

Lexical Variables

• Variables can be scoped to the enclosing block with the my operatorsub myfunc {my $x;my($a, $b) = @_; # copy params…

}

• Can be used in any block, such as an if block or while block– Without enclosing block, the scope is the source file

Page 8: Lecture 10 Perl (cont.) UNIX Development Tools. Perl (Part II) Last week: –Perl data types Scalar $x List @x Hash %x

Another Subroutine Example

@nums = (1, 2, 3);$num = 4;@res = dec_by_one(@nums, $num); # @res=(0, 1, 2, 3) # (@nums,$num)=(1, 2, 3, 4)dec_by_1(@nums, $num); # (@nums,$num)=(0, 1, 2, 3)

sub dec_by_one { my @ret = @_; # make a copy for my $n (@ret) { $n-- } return @ret;}sub dec_by_1 { for (@_) { $_-- }}

Page 9: Lecture 10 Perl (cont.) UNIX Development Tools. Perl (Part II) Last week: –Perl data types Scalar $x List @x Hash %x

Reading from STDIN

• STDIN is the builtin filehandle to the standard input

• Use the line input operator around a file handle to read from it$line = <STDIN>; # read next line

chomp($line);

• chomp removes trailing string that corresponds to the value of $/ - usually the newline character

Page 10: Lecture 10 Perl (cont.) UNIX Development Tools. Perl (Part II) Last week: –Perl data types Scalar $x List @x Hash %x

Reading from STDIN example

while (<STDIN>) {

chomp;

print “Line $. ==> $_\n”;

}

Line 1 ==> [Contents of line 1]

Line 2 ==> [Contents of line 2]

Page 11: Lecture 10 Perl (cont.) UNIX Development Tools. Perl (Part II) Last week: –Perl data types Scalar $x List @x Hash %x

< >• The diamond operator < > makes Perl programs work like

standard Unix utilities• Lines are read from list of files given as command line

arguments (@ARGV)while (<>) {chomp;print “Line $. from $ARGV is $_\n”;

}• ./myprog file1 file2 -

– Read from file1, then file2, then standard input

• $ARGV is the current filename

Page 12: Lecture 10 Perl (cont.) UNIX Development Tools. Perl (Part II) Last week: –Perl data types Scalar $x List @x Hash %x

Filehandles

• Use open to open a file for reading/writingopen LOG, “syslog”; # read

open LOG, “<syslog”; # read

open LOG, “>syslog”; # write

open LOG, “>>syslog”; # append

• Close a filehandle after using the fileclose LOG;

Page 13: Lecture 10 Perl (cont.) UNIX Development Tools. Perl (Part II) Last week: –Perl data types Scalar $x List @x Hash %x

Errors

• When a fatal error is encountered, use die to print out error message and exit programdie “Something bad happened\n” if ….;

• Always check return value of openopen LOG, “>>syslog”or die “Cannot open log: $!”;

• For non-fatal errors, use warn insteadwarn “Temperature is below 0!”if $temp < 0;

Page 14: Lecture 10 Perl (cont.) UNIX Development Tools. Perl (Part II) Last week: –Perl data types Scalar $x List @x Hash %x

Reading from a File

open MSG, “/var/log/messages”

or die “Cannot open messages: $!\n”;

while (<MSG>) {

chomp;

# do something with $_

}

close MSG;

Page 15: Lecture 10 Perl (cont.) UNIX Development Tools. Perl (Part II) Last week: –Perl data types Scalar $x List @x Hash %x

Writing to a File

open LOG, “>/tmp/log”

or die “Cannot create log: $!”;

print LOG “Some log messages…\n”

printf LOG “%d entries processed.\n”, $num;

close LOG;

no comma after filehandle

Page 16: Lecture 10 Perl (cont.) UNIX Development Tools. Perl (Part II) Last week: –Perl data types Scalar $x List @x Hash %x

Manipulating Files and Dirs

• unlink removes filesunlink “file1”, “file2”

or warn “failed to remove file: $!”;

• rename renames a filerename “file1”, “file2”;

• link creates a new (hard) linklink “file1”, “file2”

or warn “can’t create link: $!”;

• symlink creates a soft linklink “file1”, “file2” or warn “ … “;

Page 17: Lecture 10 Perl (cont.) UNIX Development Tools. Perl (Part II) Last week: –Perl data types Scalar $x List @x Hash %x

Manipulating Files and Dirs cont.

• mkdir create directorymkdir “mydir”, 0755or warn “Cannot create mydir: $!”;

• rmdir remove empty directoriesrmdir “dir1”, “dir2”, “dir3”;

• chmod modifies permissions on a file or directorychmod 0600, “file1”, “file2”;

Page 18: Lecture 10 Perl (cont.) UNIX Development Tools. Perl (Part II) Last week: –Perl data types Scalar $x List @x Hash %x

if - elsif - else

• if … elsif … else …if ( $x > 0 ) {print “x is positive\n”;

}elsif ( $x < 0 ) {print “x is negative\n”;

}else {print “x is zero\n”;

}

Page 19: Lecture 10 Perl (cont.) UNIX Development Tools. Perl (Part II) Last week: –Perl data types Scalar $x List @x Hash %x

while and until

while ($x < 100) {

$y += $x++;

}

• until is like the opposite of whileuntil ($x >= 100) {

$y += $x++;

}

Page 20: Lecture 10 Perl (cont.) UNIX Development Tools. Perl (Part II) Last week: –Perl data types Scalar $x List @x Hash %x

for

• for (init; test; incr) { … }

# sum of squares of 1 to 5

for ($i = 1; $i <= 5; $i++) {

$sum += $i*$i;

}

Page 21: Lecture 10 Perl (cont.) UNIX Development Tools. Perl (Part II) Last week: –Perl data types Scalar $x List @x Hash %x

next

• next skips the remaining of the current iteration (like continue in C)# only print non-blank lines

while (<>) {

if ( $_ eq “\n”) { next; }

else { print; }

}

Page 22: Lecture 10 Perl (cont.) UNIX Development Tools. Perl (Part II) Last week: –Perl data types Scalar $x List @x Hash %x

last

• last exist the loop immediately (like break in C)# print up to first blank linewhile (<>) {if ( $_ eq “\n”) { last; }else { print; }

}

Page 23: Lecture 10 Perl (cont.) UNIX Development Tools. Perl (Part II) Last week: –Perl data types Scalar $x List @x Hash %x

Logical AND/OR

• Logical AND : &&if (($x > 0) && ($x < 10)) { … }

• Logical OR : ||if ($x < 0) || ($x > 0)) { … }

• Both are short-circuit operators - the second expression is only evaluated if necessary

Page 24: Lecture 10 Perl (cont.) UNIX Development Tools. Perl (Part II) Last week: –Perl data types Scalar $x List @x Hash %x

Regular Expressions

• Use EREs (egrep style)• Plus the following character classes

– \w “word” character: [A-Za-z0-9_]– \d digits: [0-9]– \s whitespace: [\f\t\n\r ]– \b word boundary– \W, \D, \S, \B are complements of the corresponding

classes above

• Can use \t to denote a tab

Page 25: Lecture 10 Perl (cont.) UNIX Development Tools. Perl (Part II) Last week: –Perl data types Scalar $x List @x Hash %x

Backreferences

• Support backreferences

• Subexpressions are referred to using \1, \2, etc. in the RE and $1, $2, etc. outside the REif (/^this (red|blue|green) (bat|ball) is \1/)

{

($color, $object) = ($1, $2);

}

Page 26: Lecture 10 Perl (cont.) UNIX Development Tools. Perl (Part II) Last week: –Perl data types Scalar $x List @x Hash %x

Matching

• Pattern match operator: /RE/ is a shortcut of m/RE/– Returns true if there is a match– Match against $_ be default– Can also use m(RE), m<RE>, m!RE!, etc.if (/^\/usr\/local\//) { … }if (m%/usr/local/%) { … }

• Case-insensitive matchif (/new york/i) { … };

Page 27: Lecture 10 Perl (cont.) UNIX Development Tools. Perl (Part II) Last week: –Perl data types Scalar $x List @x Hash %x

Matching cont.

• To match an RE against something other than $_, use the binding operator =~if ($s =~ /\bblah/i) {print “Find blah!”

}

• !~ negates the matchwhile (<STDIN> !~ /^#/) { … }

• Variables are interpolated inside REsif (/^$word/) { … }

Page 28: Lecture 10 Perl (cont.) UNIX Development Tools. Perl (Part II) Last week: –Perl data types Scalar $x List @x Hash %x

Match Variables

• Special match variables– $& : the section matched– $` : the part before the matched section– $’ : the part after the matched section$string = "What the heck!";$string =~ /\bt.*e/;print "($`) ($&) ($')\n";(What ) (the he) (ck!)

Page 29: Lecture 10 Perl (cont.) UNIX Development Tools. Perl (Part II) Last week: –Perl data types Scalar $x List @x Hash %x

Substitutions

• Sed-like search and replace with s///s/red/blue/;$x =~ s/\w+$/$`/;– Unlike m///, s/// modifies the variable

• Global replacement with /gs/(.)\1/$1/g;

• Transliteration operator: tr/// or y///tr/A-Z/a-z/;

Page 30: Lecture 10 Perl (cont.) UNIX Development Tools. Perl (Part II) Last week: –Perl data types Scalar $x List @x Hash %x

RE Functions

• split string using RE (whitespace by default)@fields = split /:/, “::ab:cde:f”;# gets (“”,””,”ab”,”cde”,”f”)

• join strings into one$str = join “-”, @fields; # gets “--ab-cde-f”

• grep something from a list– Similar to UNIX grep, but not limited to using regular expressions@selected = grep(!/^#/, @code);– Modifying elements in returned list actually modifies the elements

in the original list

Page 31: Lecture 10 Perl (cont.) UNIX Development Tools. Perl (Part II) Last week: –Perl data types Scalar $x List @x Hash %x

Running Another program

• Use the system function to run an external program• With one argument, the shell is used to run the command

– Convenient when redirection is needed$status = system(“cmd1 args > file”);

• To avoid the shell, pass system a list$status = system($prog, @args);die “$prog exited abnormally: $?” unless $status == 0;

Page 32: Lecture 10 Perl (cont.) UNIX Development Tools. Perl (Part II) Last week: –Perl data types Scalar $x List @x Hash %x

Capturing Output

• If output from another program needs to be collected, use the backticksmy $files = `ls *.c`;

• Collect all output lines into a single string

my @files = `ls *.c`;• Each element is an output line

• The shell is invoked to run the command

Page 33: Lecture 10 Perl (cont.) UNIX Development Tools. Perl (Part II) Last week: –Perl data types Scalar $x List @x Hash %x

Environment Variables

• Environment variables are stored in the special hash %ENV$ENV{‘PATH’} = “/usr/local/bin:$ENV{‘PATH’}”;

Page 34: Lecture 10 Perl (cont.) UNIX Development Tools. Perl (Part II) Last week: –Perl data types Scalar $x List @x Hash %x

Example: Union and Intersection I

@a = (1, 3, 5, 6, 7);@b = (2, 4, 5, 9);@union = @isect = ();%union = %isect = ();

foreach $e (@a) { $union{$e} = 1}foreach $e (@b) {

if ($union($e) ) { $isect{$e} = 1 }$union{$e} = 1;

}@union = keys %union;@isect = keys %isect;

Page 35: Lecture 10 Perl (cont.) UNIX Development Tools. Perl (Part II) Last week: –Perl data types Scalar $x List @x Hash %x

Example: Union and Intersection II

@a = (1, 3, 5, 6, 7);@b = (2, 4, 5, 9);@union = @isect = ();%union = %isect = ();

foreach $e (@a, @b) {$union{$e}++ && $isect{$e}++;

}@union = keys %union;@isect = keys %isect;

Page 36: Lecture 10 Perl (cont.) UNIX Development Tools. Perl (Part II) Last week: –Perl data types Scalar $x List @x Hash %x

Example: Word Frequency

#!/usr/bin/perl -w# Read a list of words (one per line) and # print the frequency of each worduse strict;my(@words, %count, $word);chomp(@words = <STDIN>); # read and chomp all linesforeach $word (@words) {

$count{$word} += 1;}foreach $word (keys %count) {

print “$word was seen $count{$word} times.\n”;}

Page 37: Lecture 10 Perl (cont.) UNIX Development Tools. Perl (Part II) Last week: –Perl data types Scalar $x List @x Hash %x

Good Ways to Learn Perl

• a2p– Translates an awk program to Perl

• s2p– Translates a sed script to Perl

• perldoc– Online perl documentation$ perldoc perldoc <-- perldoc man page$ perldoc -f sort <-- Perl sort function man page$ perldoc CGI <-- CGI module man page

Page 38: Lecture 10 Perl (cont.) UNIX Development Tools. Perl (Part II) Last week: –Perl data types Scalar $x List @x Hash %x

Modules

• Perl modules are libraries of reusable code with specific functionalities

• Standard modules are distributed with Perl, others can be obtained from

• Include modules in your program with use, e.g. use CGI incorporates the CGI module

• Each module has its own namespace

Page 39: Lecture 10 Perl (cont.) UNIX Development Tools. Perl (Part II) Last week: –Perl data types Scalar $x List @x Hash %x

Perl CGI Module

• Interface for parsing and interpreting query strings passed to CGI scripts

• Methods for creating generating HTML• Methods to handle errors in CGI scripts• Two interfaces: procedural and object-

oriented– Need to ask for the procedural interface

use CGI qw(:standard);

Page 40: Lecture 10 Perl (cont.) UNIX Development Tools. Perl (Part II) Last week: –Perl data types Scalar $x List @x Hash %x

A (rather ugly) CGI Script#!/usr/bin/perl

$size_of_form_info = $ENV{'CONTENT_LENGTH'};read ($STDIN, $form_info, $size_of_form_info);

# Split up each pair of key/value pairsforeach $pair (split (/&/, $form_info)) { # For each pair, split into $key and $value variables ($key, $value) = split (/=/, $pair); # Get rid of the pesky %xx encodings $key =~ s/%([\dA-Fa-f][\dA-Fa-f])/pack ("C", hex ($1))/eg; $value =~ s/%([\dA-Fa-f][\dA-Fa-f])/pack ("C", hex ($1))/eg; # Use $key as index for $parameters hash, $value as value $parameters{$key} = $value;}

# Print out the obligatory content type lineprint "Content-type: text/plain\n\n";

# Tell the user what they saidprint "Your birthday is on " . $parameters{birthday} . ".\n";

Page 41: Lecture 10 Perl (cont.) UNIX Development Tools. Perl (Part II) Last week: –Perl data types Scalar $x List @x Hash %x

A Perl CGI Script

#!/usr/local/bin/perl -w

use strict;use CGI qw(:standard);

my $bday = param("birthday");

# Print headers (text/html is the default)print header(-type => 'text/html');# Print <html>, <head>, <title>, <body> tags etc.print start_html(“Birthday”);# Your HTML bodyprint p("Your birthday is $bday.”);# Print </body></html>print end_html();

• Read the CGI Perl documentation (perldoc CGI)

Page 42: Lecture 10 Perl (cont.) UNIX Development Tools. Perl (Part II) Last week: –Perl data types Scalar $x List @x Hash %x

Further Reading

QuickTime™ and aTIFF (Uncompressed) decompressor

are needed to see this picture.

Page 43: Lecture 10 Perl (cont.) UNIX Development Tools. Perl (Part II) Last week: –Perl data types Scalar $x List @x Hash %x

Software Development Tools

Page 44: Lecture 10 Perl (cont.) UNIX Development Tools. Perl (Part II) Last week: –Perl data types Scalar $x List @x Hash %x

Types of Development Tools

• Compilation and building: make

• Managing files: RCS, SCCS, CVS

• Editors: vi, emacs

• Archiving: tar, cpio, pax, RPM

• Configuration: autoconf

• Debugging: gdb, dbx, prof, strace, purify

• Programming tools: yacc, lex, lint, indent

Page 45: Lecture 10 Perl (cont.) UNIX Development Tools. Perl (Part II) Last week: –Perl data types Scalar $x List @x Hash %x

Make

• make: A program for building and maintaining computer programs– developed at Bell Labs around 1978 by S.

Feldman (now at IBM)

• Instructions stored in a special format file called a “makefile”.

Page 46: Lecture 10 Perl (cont.) UNIX Development Tools. Perl (Part II) Last week: –Perl data types Scalar $x List @x Hash %x

Make Features

• Contains the build instructions for a project– Automatically updates files based on a series of

dependency rules– Supports multiple configurations for a project

• Only re-compiles necessary files after a change (conditional compilation)– Major time-saver for large projects– Uses timestamps of the intermediate files

• Typical usage: executable is updated from object files which are in turn compiled from source files

Page 47: Lecture 10 Perl (cont.) UNIX Development Tools. Perl (Part II) Last week: –Perl data types Scalar $x List @x Hash %x

Dependency Graph

foo.c bar.c baz.c

foo.o bar.o baz.o

myprog

compile

link

generatedoriginalbaz.y

Page 48: Lecture 10 Perl (cont.) UNIX Development Tools. Perl (Part II) Last week: –Perl data types Scalar $x List @x Hash %x

Makefile Format

• Rule Syntax:

<target>: <dependency list>

<command>

– The <target> is a list of files that the command will generate

– The <dependency list> may be files and/or other targets, and will be used to create the target

– It must be a tab before <command>, or it won’t work

– The first rule is the default <target> for make

Page 49: Lecture 10 Perl (cont.) UNIX Development Tools. Perl (Part II) Last week: –Perl data types Scalar $x List @x Hash %x

Examples of Invoking Make

• make -f makefile• make target• make

– looks for file makefile or Makefile in current directory, picks first target listed in the makefile

Page 50: Lecture 10 Perl (cont.) UNIX Development Tools. Perl (Part II) Last week: –Perl data types Scalar $x List @x Hash %x

Make: Sequence of Execution

• Make executes all commands associated with target in makefile if one of these conditions is satisfied:– file target does not exist– file target exists but one of the source files in

the dependency list has been modified more recently than target

Page 51: Lecture 10 Perl (cont.) UNIX Development Tools. Perl (Part II) Last week: –Perl data types Scalar $x List @x Hash %x

Example Makefile# Example MakefileCC=g++CFLAGS=-g –Wall -DDEBUG

foobar: foo.o bar.o $(CC) $(CFLAGS) –o foobar foo.o bar.o

foo.o: foo.cpp foo.h $(CC) $(CFLAGS) –c foo.cpp

bar.o: bar.cpp bar.h $(CC) $(CFLAGS) –c bar.cpp

clean: rm foo.o bar.o foobar

Page 52: Lecture 10 Perl (cont.) UNIX Development Tools. Perl (Part II) Last week: –Perl data types Scalar $x List @x Hash %x

Make Power Features

• Many built-in rules– e.g. C compilation

• “Fake” targets– Targets that are not actually files– Can do just about anything, not just compile– Like the “clean” target

• Forcing re-compiles– touch the required files– touch the Makefile to rebuild everything

Page 53: Lecture 10 Perl (cont.) UNIX Development Tools. Perl (Part II) Last week: –Perl data types Scalar $x List @x Hash %x

Version Control

• Provide the ability to store/access and protect all of the versions of source code files

• Provides the following benefits:– If program has multiple versions, it keeps track only of

differences between multiple versions.– Multi-user support. Allows only one person at the time

to do the editing.– Provides a way to look at the history of program

development.

Page 54: Lecture 10 Perl (cont.) UNIX Development Tools. Perl (Part II) Last week: –Perl data types Scalar $x List @x Hash %x

Version Control Systems

• SCCS: UNIX Source Code Control System– Rochkind, Bell Labs, 1972.

• RCS: Revision Control System– Tichy, Purdue, 1980s.

• CVS: Concurrent Versions System– Grune, 1986, Berliner, 1989.

Page 55: Lecture 10 Perl (cont.) UNIX Development Tools. Perl (Part II) Last week: –Perl data types Scalar $x List @x Hash %x

RCS Basic Operations• Set up a directory for RCS:

– mkdir RCS

• Check in a new file into the repository– ci filename

• Check out a file from the repository for reading– co filename

• Check out a file from the repository for writing– co –l filename– Acquires lock

• Compare local copy of file to version in repository– rcsdiff [–r<ID>] filename

Page 56: Lecture 10 Perl (cont.) UNIX Development Tools. Perl (Part II) Last week: –Perl data types Scalar $x List @x Hash %x

RCS Keywords

• Keywords in source files are expanded to contain RCS info at checkout– $keyword$ $keyword: value $– Use ident to extract RCS keyword info

• $Author$ Username of person checked in the revision

• $Date$Date and time of check-in

• $Id$ A title that includes the RCS filename, revision number, date, author, state, and (if locked) the person who locked the file

• $Revision$ The revision number assigned

Page 57: Lecture 10 Perl (cont.) UNIX Development Tools. Perl (Part II) Last week: –Perl data types Scalar $x List @x Hash %x

SCCS EquivalentsFunction RCS SCCS

Setup mkdir RCS mkdir SCCS

Check in new foo.c ci foo.c sccs create foo.c

Check in update to foo.c ci foo.c sccs delta foo.c

Get read-only foo.c co foo.c sccs get foo.c

Get writeable foo.c co -l foo.c sccs edit foo.c

Version history of foo.c rlog foo.c sccs print foo.c

Compare foo.c to v1.1 rcsdiff sccs diffs –r1.1 foo.c -r1.1 foo.c

Page 58: Lecture 10 Perl (cont.) UNIX Development Tools. Perl (Part II) Last week: –Perl data types Scalar $x List @x Hash %x

CVS Major Features

• No exclusive locks like RCS– No waiting around for other developers– No hurrying to make changes while others wait– Avoid the “lost update” problem

• Client/Server model– Distributed software development

• Front-end tool for RCS with more functions

Page 59: Lecture 10 Perl (cont.) UNIX Development Tools. Perl (Part II) Last week: –Perl data types Scalar $x List @x Hash %x

CVS Repositories

• All revisions of a file in the project are in the repository (using RCS)

• Work is done on the checkout (working copy)

• Top-level directories are modules; checkout operates on modules

• Different ways to connect

Page 60: Lecture 10 Perl (cont.) UNIX Development Tools. Perl (Part II) Last week: –Perl data types Scalar $x List @x Hash %x

CVSROOT

• Environment Variable

• Location of Repository

• Can take different forms:– Local file system: /usr/local/cvsroot– Remote Shell: user@server:/usr/local/cvsroot

– Client/Server: :pserver:user@server:/usr/local/cvsroot

Page 61: Lecture 10 Perl (cont.) UNIX Development Tools. Perl (Part II) Last week: –Perl data types Scalar $x List @x Hash %x

Getting Started• cvs [basic-options] <command> [cmd-options] [files]

• Basic options:– -d <cvsroot> Specifies CVSROOT– -H Help on command– -n Dry run

• Commands– import, checkout– update, commit– add, remove– status, diff, log– tag...

Page 62: Lecture 10 Perl (cont.) UNIX Development Tools. Perl (Part II) Last week: –Perl data types Scalar $x List @x Hash %x

Setting up CVS

• Importing source– Generates a new module– cd into source directory– cvs –d<cvsroot> import <new-module> <vendor-branch> <release-tag>

– cvs –d<cvsroot> checkout <module-name>

Page 63: Lecture 10 Perl (cont.) UNIX Development Tools. Perl (Part II) Last week: –Perl data types Scalar $x List @x Hash %x

Managing files

• Add files: add (cvs add <filename>)• Remove files: remove (cvs remove <filename>)• Get latest version from repository: update

– If out of sync, merges changes. Conflict resolution is manual.

• Put changed version into repository: commit– Fails if repository has newer version (need update first)

• View extra info: status, diff, log• Can handle binary files (no merging or diffs)• Specify a symbolic tag for files in the repository: tag

Page 64: Lecture 10 Perl (cont.) UNIX Development Tools. Perl (Part II) Last week: –Perl data types Scalar $x List @x Hash %x

tar: Tape ARchiver

• tar: general purpose archive utility (not just for tapes)– Usage: tar [options] [files]– Originally designed for maintaining an archive of files

on a magnetic tape.

– Now often used for packaging files for distribution

– If any files are subdirectories, tar acts on the entire subtree.

Page 65: Lecture 10 Perl (cont.) UNIX Development Tools. Perl (Part II) Last week: –Perl data types Scalar $x List @x Hash %x

tar: archiving files options

– c creates a tar-format file– f filename specify filename for

tar-format file,• Default is /dev/rmt0.

• If - is used for filename, standard input or standard output is used as appropriate

– v verbose output– x allows to extract named files

Page 66: Lecture 10 Perl (cont.) UNIX Development Tools. Perl (Part II) Last week: –Perl data types Scalar $x List @x Hash %x

tar: archiving files (continued)

– t generates table of contents– r unconditionally appends the

listed files to the archive files– u appends only files that are more recent

than those already archived– L follow symbolic links– m do not restore file modification times– l print error messages about links it

cannot find

Page 67: Lecture 10 Perl (cont.) UNIX Development Tools. Perl (Part II) Last week: –Perl data types Scalar $x List @x Hash %x

cpio: copying files

• cpio: copy file archives in from or out of tape or disk or to another location on the local machine

• Similar to tar• Examples:

– Extract: cpio -idtu [patterns]

– Create: cpio -ov – Pass-thru: cpio -pl directory

Page 68: Lecture 10 Perl (cont.) UNIX Development Tools. Perl (Part II) Last week: –Perl data types Scalar $x List @x Hash %x

cpio (continued)

•cpio -i [dtum] [patterns]– Copy in (extract) files whose names match

selected patterns.– If no pattern is used, all files are extracted– During extraction, older files are not extracted

(unless -u option is used)– Directories are not created unless –d is used– Modification times not preserved with -m– Print the table of contents: -t

Page 69: Lecture 10 Perl (cont.) UNIX Development Tools. Perl (Part II) Last week: –Perl data types Scalar $x List @x Hash %x

cpio (continued)

• cpio -ov• Copy out a list of files whose names are given on the

standard input. -v lists files processed.

• cpio -p [options] directory• Copy files to another directory on the same system.

Destination pathnames are relative to the named directory

• Example: To copy a directory tree:– find . -depth -print | cpio -pdumv /mydir

Page 70: Lecture 10 Perl (cont.) UNIX Development Tools. Perl (Part II) Last week: –Perl data types Scalar $x List @x Hash %x

pax: replacement for cpio and tar

• Portable Archive eXchange format• Part of POSIX• Reads/writes cpio and tar formats• Union of cpio and tar functionality• Files can come from standard input or command line• Sensible defaults

– pax –wf archive *.c– pax –r < archive

Page 71: Lecture 10 Perl (cont.) UNIX Development Tools. Perl (Part II) Last week: –Perl data types Scalar $x List @x Hash %x

Distributing Software

• Pieces typically distributed:– Binaries

– Required runtime libraries

– Data files

– Man pages

– Documentation

– Header files

• Typically packaged in an archive:– e.g., perl-solaris.tgz or perl-5.8.5-9.i386.rpm

Page 72: Lecture 10 Perl (cont.) UNIX Development Tools. Perl (Part II) Last week: –Perl data types Scalar $x List @x Hash %x

Packaging Source: autoconf

• Produces shell scripts that automatically configure software to adapt to UNIX-like systems.– Generates configuration script (configure)

• The configure script checks for:– programs– libraries– header files– typedefs– structures– compiler characteristics– library functions– system services

and generates makefiles

Page 73: Lecture 10 Perl (cont.) UNIX Development Tools. Perl (Part II) Last week: –Perl data types Scalar $x List @x Hash %x

Installing Software From Tarballs

tar xzf <gzipped-tar-file>

cd <dist-dir>

./configure

make

make install

Page 74: Lecture 10 Perl (cont.) UNIX Development Tools. Perl (Part II) Last week: –Perl data types Scalar $x List @x Hash %x

Debuggers

• Advantages over the “old fashioned” way:– you can step through code as it runs– you don’t have to modify your code– you can examine the entire state of the program

• call stack, variable values, scope, etc.

– you can modify values in the running program– you can view the state of a crash using core files

Page 75: Lecture 10 Perl (cont.) UNIX Development Tools. Perl (Part II) Last week: –Perl data types Scalar $x List @x Hash %x

Debuggers

• The GDB or DBX debuggers let you examine the internal workings of your code while the program runs.– Debuggers allow you to set breakpoints to stop the

program's execution at a particular point of interest and examine variables.

– To work with a debugger, you first have to recompile the program with the proper debugging options.

– Use the -g command line parameter to cc, gcc, or CC• Example: cc -g -c foo.c

Page 76: Lecture 10 Perl (cont.) UNIX Development Tools. Perl (Part II) Last week: –Perl data types Scalar $x List @x Hash %x

Using the Debugger

• Two ways to use a debugger:1. Run the debugger on your program, executing the

program from within the debugger and see what happens

2. Post-mortem mode: program has crashed and core dumped• You often won't be able to find out exactly what happened,

but you usually get a stack trace.• A stack trace shows the chain of function calls where the

program exited ungracefully • Does not always pinpoint what caused the problem.

Page 77: Lecture 10 Perl (cont.) UNIX Development Tools. Perl (Part II) Last week: –Perl data types Scalar $x List @x Hash %x

GDB, the GNU Debugger

• Text-based, invoked with:gdb [<programfile> [<corefile>|<pid>]]

• Argument descriptions:<programfile> executable program file<corefile> core dump of program<pid> process id of already running

program

• Example:gdb ./hello

• Compile <programfile> with –g for debug info

Page 78: Lecture 10 Perl (cont.) UNIX Development Tools. Perl (Part II) Last week: –Perl data types Scalar $x List @x Hash %x

Basic GDB Commands• General Commands:

file [<file>] selects <file> as the program to debugrun [<args>]runs selected program with arguments <args>attach <pid>attach gdb to a running process <pid>kill kills the process being debuggedquit quits the gdb programhelp [<topic>] accesses the internal help documentation

• Stepping and Continuing:c[ontinue] continue execution (after a stop)s[tep] step one line, entering called functionsn[ext] step one line, without entering functionsfinish finish the function and print the return value

Page 79: Lecture 10 Perl (cont.) UNIX Development Tools. Perl (Part II) Last week: –Perl data types Scalar $x List @x Hash %x

GDB Breakpoints

• Useful breakpoint commands:b[reak] [<where>] sets breakpoints. <where> can be

a number of things, including a hexaddress, a function name, a linenumber, or a relative line offset

[r]watch <expr> sets a watchpoint, which will breakwhen <expr> is written to [or read]

info break[points] prints out a listing of all breakpoints

clear [<where>] clears a breakpoint at <where>

d[elete] [<nums>] deletes breakpoints by number

Page 80: Lecture 10 Perl (cont.) UNIX Development Tools. Perl (Part II) Last week: –Perl data types Scalar $x List @x Hash %x

Playing with Data in GDB

• Commands for looking around:list [<where>] prints out source code at <where>

search <regexp> searches source code for <regexp>backtrace [<n>] prints a backtrace <n> levels deepinfo [<what>] prints out info on <what> (like

local variables or function args)p[rint] [<expr>] prints out the evaluation of <expr>

• Commands for altering data and control path:set <name> <expr> sets variables or argumentsreturn [<expr>] returns <expr> from current functionjump <where> jumps execution to <where>

Page 81: Lecture 10 Perl (cont.) UNIX Development Tools. Perl (Part II) Last week: –Perl data types Scalar $x List @x Hash %x

Tracing System Calls• Most operating systems contain a utility to

monitor system calls:– Linux: strace, Solaris: truss, SGI: par

27mS[ 1] : close(0) OK 27mS[ 1] : open("try.in", O_RDONLY, 017777627464) 29mS[ 1] : END-open() = 0 29mS[ 1] : read(0, "1\n2\n|/bin/date\n3\n|/bin/sleep 2", 2048) = 31 29mS[ 1] : read(0, 0x7fff26ef, 2017) = 0 29mS[ 1] : getpagesize() = 16384 29mS[ 1] : brk(0x1001c000) OK 29mS[ 1] : time() = 1003207028 29mS[ 1] : fork() 31mS[ 1] : END-fork() = 1880277 41mS[ 1] (1864078): was sent signal SIGCLD 31mS[ 2] : waitsys(P_ALL, 0, 0x7fff2590, WTRAPPED|WEXITED, 0) 42mS[ 2] : END-waitsys(P_ALL, 0, {signo=SIGCLD, errno=0, code=CLD_EXITED, pid=1880277, status=0}, WTRAPPED|WEXITED, 0) = 0 42mS[ 2] : time() = 1003207028