{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

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
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
Background image of page 1

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

View Full Document Right Arrow Icon
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
Background image of page 2
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
Background image of page 3

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

View Full Document Right Arrow Icon
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
Background image of page 4
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
Background image of page 5

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

View Full Document Right Arrow Icon
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
Background image of page 6
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
Background image of page 7

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

View Full Document Right Arrow Icon
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, #this time last name first, and the area codes for each
Background image of page 8
Image of page 9
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}