File Streams in UNIX
The difference
between the file streams
$GetLine = <STDIN>
· Perl will wait for you to type something
· followed by end-of-line character on keyboard
print STDERR "Bad user input.\n"
Code examples
print STDOUT "Enter a number: ";
$number = <STDIN>;
print STDOUT "The number is $number\n";
example of your input: 9 is really "9\n"
may want to do this:
chop($number = <STDIN>);
same as:
$number = <STDIN>;
chop($number);
while ($tickets_sold < 10000) {
$available = 10000 - $tickets_sold;
print "$available tickets are available. How many
would you like: ";
$purchase = <STDIN>;
chomp($purchase);
$tickets_sold += $purchase;
}
print "Sorry, no more tickets left.\n";
exit;
BTW...
die "Cannot open file: $!\n."
contains a string of most recent system error
exit versus die?
Piping data in and
out of a program
open(INPUT, 'uptime |');
open tells Perl
run uptime command
takes STDOUT and feed to INPUT file handle
uptime outputs single line
read this way
$uptimeIn = <UPIN>;
open(INPUT, 'ls -l /tmp | ');
while(<INPUT>) {
$aLine = $_;
chomp(aLine);
if(substr($aLine, 0, 1) eq 'd') {
$lastSpc = rindex($aLine, ' ');
$dirName = (substr($aLine, $lastSpc + 1));
print "$dirName is a directory\n";
}
}
reminder name/value pairs separated by ?
lookfor=john?hometown=milwaukee?age=10
name=wood&address=510+brookside+drive&age=under+11&state=OR
lookfor=john #name/value pair
510+brookside+drive #plus is a space
...drive&age #& separates each pair from other pair
lookfor=john #equals separates name/values
sub Parse_Form {
if ($ENV{'REQUEST_METHOD'} eq 'GET') {
@pairs = split(/&/, $ENV{'QUERY_STRING'});
} elsif ($ENV{'REQUEST_METHOD'} eq 'POST') {
read (STDIN, $buffer,
$ENV{'CONTENT_LENGTH'});
@pairs = split(/&/, $buffer);
if
($ENV{'QUERY_STRING'}) {
@getpairs =split(/&/,
$ENV{'QUERY_STRING'});
push(@pairs,@getpairs);
}
} else {
print "Content-type: text/html\n\n";
print
"<P>Use Post or Get";
} foreach $pair (@pairs) {
($key, $value) = split (/=/, $pair);
$key =~ tr/+/ /;
$key =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$value =~ s/<!--(.|\n)*-->//g;
if ($formdata{$key}) {
$formdata{$key} .= ", $value";
} else {
$formdata{$key} = $value;
}
}
}
Hashes or
associative arrays
List:
@names
access fist name:
$names[0]
Consider that you would like to store a pair of values:
employee numbers and their names
ID |
Name
|
742 |
Jim Blank |
334 |
Tina Blue |
431 |
Lisa Blum |
(742, 'Jim Blank', 334, 'Tina Blue', 431, 'Lisa Blum')
Locate items by their keys
$anEmployee = $IDList{"334"};
$anEmployee is now "Tina Blue"
Create an associative array
%IDList = (742, 'Jim Blank', 334, 'Tina Blue', 431, 'Lisa Blum')
difficult to read pairs?
Perl 5+ can do this:
%IDList = (742 => 'Jim Blank',
334 => 'Tina Blue',
431 => 'Lisa Blum')
if keys are more than one value, use quotes on keys
%IDList = () # begins an empty assoc array.
Add records:
$IDList{566} = "Gin Rummy";
Delete records:
delete($IDList{"Gin Rummy"});
delete returns value of key just deleted
%asDay = ("mon", "Monday", "tue", "Tuesday",
"wed", "Wednesday", "thu", "Thursday",
"fri", "Friday", "sat", "Saturday",
"sun", "Sunday");
# This prints the string "Friday"
print "$asDay{'fri'} \n";
# This also prints the string "Friday"
print "$asDay{fri} \n";
# This prints the string "Wednesday"
$sWhichDay="wed";
print "$asDay{$sWhichDay} \n";
# This adds another day to the associative array
$asDay{"frog"}="Froggyday";