#!/usr/bin/perl -w #Darryl Barnhart #darryl@dbarnhart.ca #April 2009 use strict; use CGI qw(:standard); print header, start_html("Book Listings"), h1("Programming Books Online"), hr, start_form(-method=>"GET", -action=>"webbook.cgi"), "Subject: ", textfield("subject"), br, "Title: ", textfield("title"), br, "Publisher: ", textfield("publisher"), br, "ISBN: ", textfield("isbn"), br, "Price: ", textfield("price"), br, "URL: ", textfield("url"), br, submit("add", "Add Book"), submit("find", "Find Book"), defaults("Reset"), end_form, hr; if(param) { my @book_data=(param("subject"), param("title"), param("publisher"), param("isbn"), param("price"), param("url")); my $filename="../programming/perl/webbooks.txt"; if(param("add")) { open(book_file, ">>$filename") or die "Cannot open $filename\n$!\n"; print book_file join(":", @book_data), "\n"; close(book_file); } elsif(param("find")) { print h1("Results"); open(book_file, "<$filename") or die "Cannot open $filename\n$!\n"; my $count=0; my $list=""; OUTER: while() { my @item=split(/:/, substr($_, 0, -1)); for(my $i=0; $i<@item; $i++) { next OUTER if($book_data[$i] ne "" && $item[$i] ne $book_data[$i]); } $list.=li("Subject: " . $item[0] . br . "Title: " . $item[1] . br . "Publisher: " . $item[2] . br . "ISBN: " . $item[3] . br . "Price: " . $item[4] . br . "URL: " . $item[5]); $count++; } if($count==0) { print p("Nothing Found"); } else { print ol($list); } close(book_file); } } print end_html;