The reason those values are getting longer each time appears to be because you do not clear %in (like with: %in = ();) between reads of the database. Thus, your code takes the original value1 and adds on to the version left from the previous read of the database, etc.

(update: As HyperZonk states, explictly clearing %in would probably not be neccessary if strict were in use, though this will work if you for some bizarre silly reason can't use it.)

General comments on your code: you should use strict and warnings. I cannot tell if you have warnings on, but I can tell that you do not have strict on. If this is a CGI script it may be worthwhile to turn on taint checking. Also, please check the return value of your open like: ((update) so you'll know if the file moves, etc.)

open DATABASE, $Rawdata or die "opening $Rawdata: $!\n";

It is probably better to use the constants provided with the Fcntl module for flock (since there is a slight chance they might vary from system to system.)

use Fcntl qw(:flock); flock DATABASE, LOCK_SH or warn "Couldn't obtain shared lock: $!\n";

Additionally, there is almost never a need to flock to unlock a file; it should be done automatically on closing. In fact, locking a file instead of just closing it can often lead to data loss when writing files due to buffering.

You may find you can save memory by processing the file as you read it; this might be worthwhile if the file is to become very large.

You could use the CGI module to parse your query strings like:

my $q = CGI->new( $param_string ); foreach my $key ($q->param()) { my $value = $q->param($param); # ... }

I cannot say whether this would be more efficient. (Though it will prevent problems if/when browsers start deliminating queries with ;. (;)

1: Except if the original value is false -- for example, 0.


In reply to Re: Parsing Query Strings in a file by wog
in thread Parsing Query Strings in a file by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.