50 likes | 153 Views
Basic Input/Output. Review: Perl Basics. Perl Variables Scalar holds number, character, string e.g. $var1 = “Mary”; $var2= 1; Array holds a list of scalars e.g. @array1 = (“Mary”,”Tom”); Standard Input <STDIN> reads 1 line from standard input e.g., $line= <STDIN>;
E N D
Basic Input/Output Web Programming
Review: Perl Basics • Perl Variables • Scalar holds number, character, string • e.g. $var1 = “Mary”; $var2= 1; • Array holds a list of scalars • e.g. @array1 = (“Mary”,”Tom”); • Standard Input • <STDIN> reads 1 line from standard input • e.g., $line= <STDIN>; • Standard Output • print writes to standard output • e.g., print “My name is $name\n”; Web Programming
File Input/Output • Reading from a file • open (INF,”$file1”);# open $file1 for reading • $line = <INF>;# read in one line • Writing to a file • open (OUTF, “>$file2”);# open $file2 for writing • open (OUTF, “>>$file2”);# open $file2 for appending • print OUTF “This is line1\n”;# print to $file2 • Closing a file after reading/writing • close (FILE); • Terminating program for bad file I/O • open (FILE,$file) || die “can’t read $file”; Example script Web Programming
Determining File Status • Syntax • if ( -test $file ) { statements } • File test operators • -d : Is $file a directory? • -e : Does $file exist? • -f : Is $file is an ordinary file? • -l : Is $file a symbolic link? • -s : Is $file a non-empty file? • -z : Is $file an empty file? • -r/-w/-x : Is $file readable/writable/executable? Example script Web Programming
Working with Directories • Open a directory • opendir (IND, $directory); • Read the contents. • @files = readdir (IND); • Close the directory • closedir (IND); Example script Web Programming