Lec9Handouts - CS2044 - Advanced Unix Tools Spring 2009...

Info iconThis preview shows pages 1–9. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: CS2044 - Advanced Unix Tools Spring 2009 Lecture 9 David Slater dms236 at cornell.edu March 9, 2009 David Slater dms236 at cornell.edu CS2044 - Advanced Unix Tools Organization Homework 3 Due Sunday March 15th Start Early! Turn In Code That Works! Follow Directions David Slater dms236 at cornell.edu CS2044 - Advanced Unix Tools file testing Thankfully file testing for Perl is similar to bash-d : checks if directory-e : checks if exists-l : checks if it is a symbolic link-o : checks if file is owned by the user-r : checks if reading-w : checks if writable-z : checks if file is empty And there are many more. David Slater dms236 at cornell.edu CS2044 - Advanced Unix Tools Example #! /user/bin/perl # This script opens file1 and writes it to file2 # if it does not already exist. open(INFILE,"file1") or die "sudden flaming when opening file1 \n"; !(-e "file2") or die "file2 exists!\n"; open(OUTFILE,">file2") or die "sudden death when opening file2 \n"; while(<INFILE>) { print OUTFILE; } David Slater dms236 at cornell.edu CS2044 - Advanced Unix Tools Error Codes Perl stores error codes for the last command executed in $!. So we can modify our script again #! /user/bin/perl # This script opens file1 and writes it to file2 # if it does not already exist. open(INFILE,"file1") or die "Error opening file1: $! \n"; !(-e "outfile") or die "file2 exists!\n"; open(OUTFILE,">file2") or die "Error opening file2 $! \n"; while(<INFILE>) { print OUTFILE; } David Slater dms236 at cornell.edu CS2044 - Advanced Unix Tools Command-Line Arguments Perl uses the array @ARGV to store commandline arguments. For example if we run ./myperlscript.pl arg1 arg2 Then we can access agr1 and agr2 as $ARGV[0] and $ARGV[1] respectively. You can also do $numargs = @ARGV; To get the number of arguments. David Slater dms236 at cornell.edu CS2044 - Advanced Unix Tools Lets look at a formatting script #!/usr/local/bin/perl #Usage: perl phone.pl phonedb.txt areacode.txt #Entries in the first argument are of the form #Mary Smith (607) 255-0010 (607) 342-0019 #John Peter (507) 675-7241 (607) 255-5555 #The second file will contain the names of each person, #this time last name first, and the area codes for each #the person’s phone numbers if (open(INFILE,"$ARGV[0]")) { if (open(OUTFILE,">$ARGV[1]")) { while ($myline=<INFILE>) { $myline =~s/^\s*(\w+)\s+(\w+)/$2 $1/; $myline=~s/\((\d{3})\)\s*\d{3}-\d{4}/$1/g); print OUTFILE "$myline"; } close(INFILE); close(OUTFILE); } else { print "Error opening $ARGV[1]\n"; close(INFILE); exit(1); } } else { print "Error opening $ARGV[0]\n"; exit (1); } David Slater dms236 at cornell.edu CS2044 - Advanced Unix Tools But we can do it with less text #!/usr/local/bin/perl #Usage: perl phone.pl phonedb.txt areacode.txt #Entries in the first argument are of the form #Mary Smith (607) 255-0010 (607) 342-0019 #John Peter (507) 675-7241 (607) 255-5555 #The second file will contain the names of each person,...
View Full Document

This document was uploaded on 05/21/2009.

Page1 / 28

Lec9Handouts - CS2044 - Advanced Unix Tools Spring 2009...

This preview shows document pages 1 - 9. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online