powerhouse has asked for the wisdom of the Perl Monks concerning the following question:

I am just wondering if you can look at this MAIN script I wrote and tell me if you see any issues with it. When I run this script, it seems to be sucking up a LOT of the Server Load. I don't know why it would be doing that, since I do close every process(I THINK). Can you take a look at it for me, and please point out any "bad" Habits I might have picked up? I will not post the entire system as it's pretty big, I'll just post the main index script.
#!/usr/bin/perl -w BEGIN { push (@INC,"/home/username/mods"); push (@INC,"/home/username2/mods"); } use Carp (); local $SIG{__WARN__} = \&Carp::cluck; use CGI::Carp qw(fatalsToBrowser); use CGI qw(:standard :cgi-lib escapeHTML); use Mail::Sendmail; use Digest::MD5; use Crypt::CBC; use Crypt::Blowfish; use LWP::UserAgent; use Image::Magick; # I really don't use this YET, I should remove it I + guess... Until I need it use SiteDB::Session; ReadParse(\%in); use vars qw($cookie $sess_id $sess_ref $page $is_logged_in %in); $sess_id = cookie("sess_id"); if ($sess_id eq "" && $in{sess_id} ne "") { $sess_id = $in{sess_id}; } $sess_ref = SiteDB::Session->open_with_expiration(undef, $sess_id) if +defined ($sess_id); require "/home/username/site/path/data.conf"; if ((defined($in{pg}) && $in{pg} eq "quit") && defined($sess_ref)) { if ((defined($sess_ref->attr("remember_me")) && $sess_ref->attr("r +emember_me") == 1) && (!defined($in{expire}) || (defined($in{expire}) + && $in{expire} != 1))) { Log_Out_User($sess_ref); } else { Delete_Session_Forever($sess_ref); } } if (!defined ($sess_ref)) { defined ($sess_ref = SiteDB::Session->open (undef, undef)) or error ("Could not create new session: $SiteDB::Session::err +str"); $cookie = cookie (-name => "sess_id", -value => $sess_ref->session_id(), -path => url (-absolute => 1), -expires => "+1y" ); } get_cookie_values($sess_ref); require "/home/username/site/path/config.data"; $sess_ref->expires ($sess_ref->now() + (60*60*24*365)) if !defined ($s +ess_ref->expires()); $Page_Dir = "/home/username/site/path/pages"; $pg = $in{pg}; $enter1 = 0; if (!$pg || !-e "$Page_Dir/$pg.conf") { require "$Page_Dir/enter.conf"; $enter1++; $page .= $page_content; } elsif ($pg =~ /inventory/i) { $item_num = $in{item_num}; require "$Page_Dir/inventory.conf"; if ($item_num eq "") { $page .= show_form(""); } else { $page .= Check_inventory(); } $page .= qq~<table border="0" class="table2" align="center" width +="95%"> <tr valign="top"> <td align="right">Last updated on: <font color="orange">~; open(UPDATE, "/home/username/site/path/stock/updated.conf") or die "Co +uld not get Last Update time: $!"; seek(UPDATE,0,0); while(<UPDATE>) { if ($_ ne "") { $page .= $_; } } close(UPDATE); $page .= qq~</font></td> </tr> </table> ~; } elsif(-e "$Page_Dir/$pg.conf") { $test_cookie2 = cookie ("sess_id"); if (!defined($test_cookie2) || (defined($test_cookie2) && $test_co +okie2 eq "")) { $nocookies = 1; $include_sess_id = "&sess_id=" . $sess_ref->session_id(); } else { $nocookies = 0; $include_sess_id = ""; } $datafile = "$Page_Dir/$pg.conf"; require $datafile; $page .= $page_content; } if ($enter1) { enter_header($title,"",""); } else { top_header("$title","",""); } if ($in{pg} ne "" || $in{pg} ne "enter") { $page .= qq~</td>\n</tr>\n</table> </td>\n</tr>\n</table>~; } $page .= p({-style => "margin-left: 20", -align => "center"}, br() + . qq~<font face="Arial" color="#FFFFFF" size="1">© COPYRIGHT 2002 TH +E GIFT COTTAGE<br />ALL RIGHTS RESERVED<br />Art work by <a href="htt +p://www.thomaskinkade.com/magi/servlet/com.asucon.ebiz.home.web.tk.Ho +meServlet" class="PageLink" target="tom">Thomas Kinkade</a></font>~); print $page, br() x 3, end_html(); # Close page $sess_ref->close($sess_id) if defined($sess_ref);# Close the sessi +on. exit; # Exit system. sub error { my $er_message = shift; $title = "The Gift Cottage - Error Page"; top_header("$title","",""); print h3({-align=>"center"},"Error"), p(strong($er_message)), br(), br(), qq~ </p /> </td /> </tr /> </table /> </td /> </tr /> <tr valign="top"> <td bgcolor="#E1EAF1"> <font size="1"> <div align="center"><font class="copyright">© COPYRIGHT 2002 THE GIFT +COTTAGE<br />ALL RIGHTS RESERVED</font /> </div /> </font /> </td /> </tr /> </table /> ~, p({-style => "margin-left: 20", -align => "center"}, br() . qq~<font f +ace="Arial" color="#FFFFFF" size="1">© COPYRIGHT 2002 THE GIFT COTTAG +E<br />ALL RIGHTS RESERVED</font>~), br() x 4, end_html(); $sess_ref->close($sess_id) if defined($sess_ref);# Close the session. +Used here because exits next! exit; }
If you have any questions, please just let me know. I would really appreciate any advise you have for me.

thx,
Richard

Edit by tye, add READMORE, new title

Replies are listed 'Best First'.
Re: Advise on scripting...
by robartes (Priest) on Mar 12, 2003 at 07:13 UTC
    Hi,

    without all the subroutines, there is no way for us to see where the script might take a lot of CPU time.

    I would suggest profiling your script. The standard Perl distribution includes the Devel::DProf module which can be used for this. Do something like (lifted straight from Devel::DProf pod):

    $ perl -d:DProf script.pl $ dprofpp
    This will show the 15 subs your program spends most time in. That should narrow down the suspect subroutines. Once you have your likely candidates, use Benchmark to try and improve their execution times.

    CU
    Robartes-