I'm having problems with my code and cant quite figure it out. Here's my problem. I wrote a program that goes through and reads each line in a file and then takes that line and send it to a sub routins which contains some sql statements. But each time it gets to the end of the file it tries to run the blank data at the end of the file (the last line) and it ends up sending me errors. SO my question to all of you is how do I make my program stop once it has reached the end of the file and not to send the blank line to the sql section of my program. Below is my code
sub main{ my $log_parsing_files = "/export/home/fio/log_parsing_files"; opendir(FILES,"$log_parsing_files") || die "could not open $log_parsing_files"; #opens directory foreach $name (sort readdir(FILES)) { next unless ($name =~ /\w+\d*$/); &parsefile($name); } } sub parsefile { my $name = shift; my $line; my $value; open (FHIN, "</export/home/fio/log_parsing_files/ $name") || die ("can't open $name for reading.\n"); while (<FHIN>) { $line=$_; #chomp(); $line=~m%(\d+).*%; $line=~s/\s//g; &data_base_insert($line); print "these people where given there 5mb $line\n"; } close FHIN; } sub data_base_insert { my ($line) = @_; $sql = "SELECT user_seq from special_offers where user_seq = $line"; $cmd = $dbh->prepare($sql); $cmd->execute(); $userseq = $cmd->fetchrow; print "COMP: '$userseq' '$line'\n"; if ($userseq eq $line ) { print "There are already in the special offers table $line and $userseq\n"; } else { $sql = "INSERT INTO special_offers (PROMO, PROMODATE, USER_SEQ) VALUES ('XDRIVE', '19-JUNE-00','$line')"; $cmd = $dbh->prepare($sql); $dbh->commit(); $sql= "SELECT quota from user_quota where user_seq = $line"; $cmd = $dbh->prepare($sql); $cmd->execute(); $quota = $cmd->fetchrow; my $newspace= 5120 + $quota; my $sql = "UPDATE user_quota set quota = $newspace where user_seq=$line" ; $cmd = $dbh->prepare($sql); $cmd->execute(); $dbh->commit(); return $line; } }

In reply to Closing program when end of files is reached by fgcr

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.