PDA

View Full Version : New to Perl, looking for optimization tips


barrett777
03-07-2006, 09:45 PM
Hi All,

I'm programming an online game, and player's data will be saved and loaded from my server using CGI. I have working CGI scripts, but, if my game becomes popular (hopefully), I think I will need to optimize my scripts some. I'm new to Perl, so if there are shortcuts to what I'm doing, please help. My code is posted below, with comments where I have ideas for help:

#!/usr/bin/perl

print "content-type: text/plain\n\n";

open(SAVE, "+< playerData.txt") or die "OPEN";

# All data is stored in this file. This .cgi file does the saving portion.

flock(SAVE, 2) or die "LOCK";

# Makes sure I get an exclusive lock on the file, since I will be writing.

my @playerData = <>;
@playerData = split(/\s+/, $playerData[0]);

# This is my player data from my game. It will always be one line.
# Can I do those two lines in one line of code?

@fileData = <SAVE>;

# This is the contents of my player data file. I've had problems reading
# through and writing over the parts I need (Because what I'm replacing
# might be longer/shorter than what I'm replacing with, if that makes any
# sense). So I have been reading the whole file into an array, and
# rewriting the file, replacing what I'm looking for.

seek SAVE, 0, 0;

# Again, starting with the beginning of the file, I will write over all of it
# If there is a way I can do this differently, please let me know. My
# playerData.txt file is in the format "username password playerData"
# Each entry is on a new line, but each entry is of different lengths.
# Is there a way I can replace the line I want without running over
# the next line, or leaving parts of previous data if new line is shorter?

foreach (@fileData)
{
@line = split(/\s+/, $_);
if ($playerData[0] eq $line[0])
{
if ($playerData[1] eq $line[1])
{
print SAVE $playerData[0] . " " . $playerData[1] . " " . $playerData[2] . "\n";
print "SAVE";
}
else
{
print SAVE $_
print "PASSWORD";
}
}
else
{
print SAVE $_;
}
}

# I go through each line, looking for a username + password match. If I find
# one, I print the new data instead of the old stuff. If I find a username
# match, but different passwords, then the player entered the wrong
# password and I keep the old data. If no match, I keep the old data.

# If I find a way to read through and replace only the one line I want to,
# is there a way to end the loop when I find what I'm looking for?

close (SAVE);

# Should I try to unlock the file before closing, or just close it?

exit 0;

All help is greatly appreciated!
Thanks!
Ben Barrett

mlseim
03-08-2006, 01:01 AM
Just my opinion, but as your database might get large, I would recommend
using MySQL, either with Perl or PHP. I realize it might have a learning curve,
but you will be glad you went that route in the future.

barrett777
03-08-2006, 03:44 AM
I've thought about that, but I really don't know anything about mySQL, other than it's a database for something. If you're familiar with Java, would I be able to access the database using Java, or how do you access the database?

My web hosting lets me have 5 SQL databases, so I figure that means that it's built into the server, making it easy to install on the server-side, but I'm really new to databases in general.

Do you have any good websites I could research that?

mlseim
03-08-2006, 03:47 PM
Well, this is the Perl section of the forum, but you'll find thousands of
tutorials (using Google) for PHP/MySQL.

And, there are many good books.

I mention using PHP (you can't use Javascripting) because it has become
such a popular union (PHP and MySQL). You'll find many free scripts that
do things such as membership logins, content management. You'll find
some free simple scripts that don't do much, but will give you an idea of
how it all works.

Tutorials online are usually pretty good, but I always like having a book
next to me. A good one for beginners like you (and myself, by the way),
is this one: ISBN 0672317842

http://www.amazon.com/gp/product/0672317842/002-5342939-9558460?v=glance&n=283155

It will especially be helpful if you already have some programming skills.

But, you have to start somewhere. I just want to steer you away from
the flat-file database method for your project. I think you'll become
frustrated with it when it grows too large.

barrett777
03-09-2006, 07:51 PM
Thanks, I think I'll stick with mySQL!

Hopefully things will turn out after I do some reading.