Gossamer Forum
Home : General : Perl Programming :

Reading and writing files.

Quote Reply
Reading and writing files.
Hi again,

Again, I'm not sure if this is possible, but someone may be able to tell me one way or the other. If it is, I'd appreciate it if someone could give me a hand with the routines.

This is for a shopping cart, and these routines would be for reading and writing the data files. To write the data file, the only required input will be a product code ($pid). As well as that, there will be other optional fields such as size and colour. I want to have the options unlimited, meaning there can be none, or however many attributes the product needs.

The format for the data file would be (This is what would be printed to the data file for the *first* product):

Code:
~ # Start this item.
$pid|$url|$name|$price # Basic product data, read from the database using the $pid (Product ID).
option1 = value #
option2 = value # There could be many options, or none at all.
~ # End the item.

Each additional item that is added to the cart will print only the final tilde (~) so the script will know that everything between the item separators will be one product, in the same way Matt's random text script works.

I'll use something like the query routine from dbMan to get the rest of the data from $pid before I write, so I can probably do the pipe-delimited bit myself, but I'm not sure how to write the options. Since I don't know the options and number of them (if any) until I get the form input, how do I get it to add the *other* variables from the form input to the data file? ie. foreach $othervariable print $name=$value. And how do I get the *actual* name of the variable as a text string to print so I'll be able to read it out again? For example, the input will be $colour=$value and I'll want to print that as "Colour=Red" (i.e. capitalise the first letter of the variable name), so I can read it back out again. Or is there a better way of doing it?

As to reading it, I really don't have a notion. I can probably get as far as putting the contents of each item into a variable, but I don't know how to separate out the pipe-delimited product data and the options??? I'll also need to be able to do this to modify quantities in the cart.

One more thing. There will be several databases that I want to read from, identifiable from the product code ($pid), which will be in the format "productdb-product". Is there a simple regular expression to separate the first part (product id, before the hyphen) out so I can read from the right database?

Any help with this will be much appreciated. I can probably figure out most of the rest of the stuff, but these routines and the cart id routines I've asked about in another thread are the only major stumbling blocks I think I'll have.

Thanks in advance,
adam

[This message has been edited by dahamsta (edited March 30, 1999).]
Quote Reply
Re: Reading and writing files. In reply to
Just clawin' me way back up to the top in the hope I'll get noticed...
Quote Reply
Re: Reading and writing files. In reply to
Anybody gonna help me out here?

Even a "That's impossible you freakin' dolt" would be better than sullen silence...

Frown

adam
Quote Reply
Re: Reading and writing files. In reply to
Well, I'm always better at answering specifics. Wink

To read the data file you could do:

while (<DB> ) {
$data_line = $_;
while 1 {
$option_line = <DB>;
last if !(defined $option_line) or ($option_line =~ /\~/);
($name, $value) = split /=/, $option_line;
$options{$name} = $value;
}
# Now you have the first line in $data_line and an options hash in %options.
}

Hope that gives you a start. =)

Alex
Quote Reply
Re: Reading and writing files. In reply to
Yaaaaaaaaaaaaay!

Thanks Alex, I was just about to try and do it another way, with options written to another data file -- cart-product.options, but that way I'd just end up with too many data files and checking to see if they exist and all that.

So that saves me a hell of a lot of trouble, and in future I know I reckon I'll just ask one little question at a time! Smile

Thanks again,
adam
Quote Reply
Re: Reading and writing files. In reply to
Aaaaggghh! I leaped before I looked. I'm afraid that didn't work Alex, I get a 500 error before I can go any further. The datafile is opening OK, and if I remove the "while 1" loop, I can get it to print out the LAST line in the file. But it doesn't like the rest. I'm curious also, is this for a file with just one product? ie. Will I have to build another loop in to get out subsequent products, since it looks like it'll stop when it hits a tilde? I'm just trying it with one now, and I'm not even trying to get the options out of %options, cos I get the error. Here's the code in full:


Code:
#!e:/perl/bin/perl.exe

$datafile = "/path/to/data.file";
require "cgi-lib.pl";

open (DB,"$datafile") | | &cgierror("Can't access $datafile\n");
while (<DB> ) {
$data_line = $_;
while 1 {
$option_line = <DB>;
last if !(defined $option_line) or ($option_line =~ /\~/);
($name, $value) = split /=/, $option_line;
$options{$name} = $value;
}
}
close(DB);

print "Content-type: text/html\n\n";
print "<html>";
print "Data Line = $data_line";
print "</html>";

exit;

And here's the datafile, with just one listing for now:

Code:
~
data1|data2|data3|data4|data5
option1=value1
option2=value2
~

*tearing me feckin hair out* Smile

adam

[This message has been edited by dahamsta (edited April 06, 1999).]
Quote Reply
Re: Reading and writing files. In reply to
Save yourself some frustration by starting your scripts:

Code:
use CGI::Carp qw/fatalsToBrowser/;
$|++;
print "Content-type: text/html\n\n";

That way, if you do get an error, it will display in the browser, not as a 500 server error.

Cheers,

Alex

Quote Reply
Re: Reading and writing files. In reply to
Thanks Alex,

I did that, but I'm running Apache/Perl on my Win32 machine and it doesn't seem to give the same details on errors as I get online. Running the above code with use CGI:Carp all I get is the mightily annoying "aborted due to compilation errors" errors (almost as annoying as "premature end of script headers" Smile).

I just can't seem to figure it out. I've tinkered, played and rearranged, but nothing. I think it may just be easier to just put the lines one after the other in the data file like data1=value, data2=value, option1=value, etc etc. Probably be easier to split anyway.

You know what it's like when you've your heart set on something though, quite often your blinded to an easier way to do it!

Thanks again Alex,
adam
Quote Reply
Re: Reading and writing files. In reply to
Is anything worth while showing up in your Apache error log on your system?
Quote Reply
Re: Reading and writing files. In reply to
Nah, I actually get a more helpful error message using what Alex gave me than the error log, so thanks for that Alex. (Although I did have to remove the Content-Type header, since it was printing out all the headers I'd inserted in the subroutines. And the error page seemed to work ok without it.)

Anyway, it doesn't matter now because I've redone the whole thing in a different way, which I should've done originally. I get ahead of myself! Splitting it up into tilde-delimited items was all well and good, but I couldn't get the data back out again so I guess I'm just not good enough yet! Smile Now I've put all the data for each item into one long string, like a url-encoded string, except each item is separated by a pipe rather than an ampersand.

So now I can get the data in and out pretty easily and I've got the whole thing running, albeit in a fairly basic form. I guess I might be adding a little to the overhead by adding each variable name into the string, since I know what some of them are already, but my thinking is that if I have so many carts on the server that it slows it down, I can probably afford to send myself on a Perl training course to find a better way of doing it. Maybe somewhere in the Carribean. Smile

Next up is using cookies to get the cart id (I'm currently using the IP address), so if anyone had a look at my previous posting about checking for cookies, I'd appreciate any answers. Actually, I'll probably post about it again, except this time I might just try and simplify it a little, I seem to get better answers that way! Smile

So I guess this thread is closed! Thank you!

adam