This is a wierd one that I've run into - me being Perl the newbie that I am.
Here is a simple script that I have been trying to make work.
Everything works fine until I get to the part where it tries to open the
file. On Unix, I get the following error message:
Software error:
flock() unimplemented on this platform at c:/apache/cgi-bin/catalog.cgi line
35.
On Windows 98 running Apache it just says that it couldn't open the file. I
can't find anything wrong with my code - if anyone here could take a quick
look and give me a few pointers I'd appreciate it. Thanks,
Justin
---- Here is the code:
#!/usr/local/bin/perl
use CGI;
use CGI::Carp qw( fatalsToBrowser );
$query = new CGI;
$catalog_file = "products.txt";
$cart_file = "cart.txt";
if ($query->param('product_id')) {
if ($query->cookie('session_id')) {
$session_id = ($query->cookie('session_id'));
}
else {
$session_id = time . $$;
}
&set_variables;
&add_to_cart;
&print_page_start;
&open_catalog;
&display_catalog;
&print_page_end;
else {Quote:}
&print_page_start;
&open_catalog;
&display_catalog;
&print_page_end;
sub add_to_cart {Quote:}
open (CART, "+>> $cart_file")
or die "Can't open shoppint cart file";
flock CART, 2;
seek CART, 0, 0;
$found = 'no';
chomp $record;
($rec_session_id, $rec_product_id, $rec_quantity) =
split /\t/, $record;
if (($session_id eq $rec_session_id) and
($product_id eq $rec_product_id))
$new_quantity = $quantity + $rec_quantity;
$new_record = "$session_id\t$product_id\t$new_quantity";
$record = $new_record;
$found = 'yes';
}
$record .= "\n";
}
seek CART, 0, 0;
truncate CART, 0;
if ($found eq 'no') {
print CART "$session_id\t$product_id\t$quantity\n";
}
close CART;
sub set_variables {Quote:}
$product_id = $query->param('product_id');
$quantity = $query->param('quantity');
sub open_catalog {Quote:}
eval {
open (CATALOG, "< $catalog_file")
or die "Can't open $catalog_file";
};
sub display_catalog {Quote:}
print "<CENTER>\n<TABLE BORDER=1 CELLPADDING=4\n";
print "<TR>\n<TH>Description</TH>\n<TH>Price</TH>\n</TR>\n";
while (<CATALOG>) {
($product_id, $product_desc, $product_price) = split /\t/;
print "<TR>\n";
print "<FORM>\n";
print "<INPUT TYPE=\"hidden\" NAME=\"product_id\" ";
print "VALUE=\"$product_id\">\n";
print "<TD>$product_desc</TD>\n<TD>$product_price</TD>\n";
print "<TD><INPUT TYPE=\"text\" NAME=\"quantity\" ";
print "VALUE=\"1\" SIZE=2></TD>\n";
print "<TD><INPUT TYPE=\"submit\" VALUE=\"add\"></TD>\n";
print "</FORM>\n";
print "</TR>\n";
}
print "</TABLE>\n</CENTER>\n";
sub print_page_start {Quote:}
if ($session_id) {
$cookie = $query->cookie(-name=>'session_id',
-value=>$session_id);
print $query->header(-cookie=>$cookie);
}
else {
print $query->header;
}
print "<HTML>\n<HEAD>\n<TITLE>PC Product Catalog</TITLE>\n";
print "</HEAD\n<BODY>\n";
print "<H1 ALIGN=\"center\">PC Product Catalog</H1>\n";
sub print_page_end {Quote:}
print "<P><CENTER><B>";
print "<A HREF=\"cart.pl\">view cart</A></B></CENTER></P>\n";
print "</BODY>\n</HTML>\n";
--- Thanks againQuote:}