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

I have am building my first app in perl and I'm having a hard time making it work properly. I would like to have the information saved to the clients folder and when they return to the page, the fields that they filled in should still show as saved in the browser. Also, I'd like the files to upload to the company repository. I have a working page I'm using as a template but when I try to edit it, it just won't work. Here's the script...
#!/usr/bin/perl ################################################################ ######################################################## sub valid_cookie{ $flag=0; my $session_file=$companyrepository."/session.txt"; #file where se +ssions are stored unless(open(INTT, "$session_file")){print "Session not found $sess +ion_file. $cookie<br>";} while (<INTT>){ #find existing cookie if($_=~/^$cookie\;/){#if cookie exist $flag=1; last; } } close INTT; return $flag; } ###################################################################### +###### sub writeData { unless(open (OUT,">>".$companyrepository."/PO.txt")){ open (OUT, ">".$companyrepository."/PO.txt"); } print OUT "$write_data\n"; close (OUT); } ###################################################################### +###### sub readData { if (-e $datafile) { open (IN,"<".$datafile); while (<IN>) { push @values, $_; } close (IN); } } ###################################################################### +###### use CGI; $query = new CGI; $repository="/home/warp/userdata/"; #$flag2=0; #init flag for existing users ####check for valid cookie#### $cookie=""; @temp=""; $cookie=$ENV{'HTTP_COOKIE'}; #identify cookie @temp_cookie=split(';',$cookie); $cookie=$temp_cookie[0]; ($sess_id,$company,$site,$user)=split('%2C', $cookie); $companyrepository=$repository.$company."/".$site; valid_cookie(); # If cookie not valid, then redirect to login page if ($flag==0){print $query->redirect('http://services.genband.com/cgi- +bin/dbq/na/DBQ_welcome.cgi'); }#redirect to another location $answer=$query->param("answer"); print "Content-type: text/html\n\n"; foreach $paramname ($query->param) { $formcomplete=1; #$paramcount++; $paramvalue=$query->param($paramname); $$paramname=$paramvalue; # preserve ratearea names if($paramname =~ /^sub/){ # read in subject chomp $$paramname; $write_data="$${paramname}:::"; }elsif($paramname =~ /^com/){ # read in PO chomp $$paramname; $com=$$paramname; $write_data=$write_data."$$paramname"; }elsif (($paramname eq "answer") && ($answer eq "finish")) { $finished=1; } } # create company specific variables $datafile=$companyrepository."/PO.txt"; $master_file=$companyrepository."/master_grpnum.txt"; ################################ # start html $header_file = "header5.html"; open(FILE,"$header_file"); while(<FILE>) { print $_; } close FILE; print <<'ENDHTML'; <tr> <td width="1020" bgcolor="#f4f4f4" align="center"> <table width="900" border="0" cellspacing="0" cellpadding="0"> <tr> <td bgcolor="#232021" height="25px" width="499" align="left" valig +n="bottom"><p class="h1">Project Overview</p></td> <tr><td colspan="2" bgcolor="#ffffff"><div id="container"> ENDHTML $datafile="$companyrepository/PO.txt"; if ($finished){ writeData; print "<p class=status>"; print "The Project Overview has been saved. <br>Choose your region + to continue<br><I>Note: Any location other than North America is considered International</I></p>"; print "<p class=status><button type=button type=button type=button + type=button onclick=\"window.location='http://services.genband.com/c +gi-bin/dbq/na/home.cgi'\">North American Database Questionnaire</butt +on> <button type=button type=button type=button type=button onclick=\"wind +ow.location='http://services.genband.com/cgi-bin/dbq/int/home.cgi'\"> +International Database Questionnaire</button></p>"; }else{ print "<form action='PO.cgi' method='post'>"; if (-e $datafile){ } print "<br>"; $cnt=1; foreach $v (@values){ @com=split(/:::/, $v); print "<b>$cnt. $com[0]</b><br>$com[1]<br><br>"; $cnt++; } $paramname='txt1'; print "<p class=status><b>Application</b> <i>(check all that apply +)</i><br></p>"; $paramname='app'; print "<p class=status><INPUT TYPE=CHECKBOX NAME='app' VALUE=appcl +ass4> Class 4 - trunk to trunk tandem calls only </p>"; print "<p class=status><INPUT TYPE=CHECKBOX NAME='app' VALUE=appcl +ass5> Class 5 - station to staion, station to trunk, trunk to staion +only </p>"; $paramname='type'; print "<p class=status><br><b>Type of Install</b><br><br><SELECT N +AME='type'> <OPTION SELECTED>Greenfield - new switch, no existing customer base pe +r say</OPTION> <OPTION>Cap and Grow - subtend and existing sysytem and provide growth + Capacity</OPTION> <OPTION>Migration - replace an existing system</OPTION> </SELECT><br><br></p>"; print "<p class=status> <form action='/home/warp/userdata/$companyrepo +sitory' method='post' enctype='multipart/form-data'> </p>"; print "<p class=status><b>Network Diagram:</b><br><br><input type=file + name='network_diagram' /></p>"; print "<p class=status><i>Present the node to be provisioned and show +all connections to external nodes.<br /> Other SP's, STP's, VMS, Servers or Remote Hosts.</i><br><br></p>"; print "<p class=status> <form action='/home/warp/userdata/$companyrepo +sitory' method='post' enctype='multipart/form-data'> </p>"; print "<p class=status><b>Call Flow Models: </b><br><br><input type=fi +le name='call_flow_models' /></p>"; print "<p class=status><i>Povide high level and detailed information.< +/i></p>"; ###################################################################### +##################################### # initial presentation of the page if ((!$formcomplete) && ($error eq "")) { print "<p class=status>"; print "<input type=submit name=answer value=finish>"; print "</p>"; # errors found when processing }elsif((!$formcomplete) && ($error ne "")){ print "<p class=status>"; print "<input type=submit name=answer value='re-submit'>"; print "<button type=button type=button type=button type=button + onclick=\"window.location='home.cgi'\">"; print "cancel</button><p class=main>$error</p>"; print "</p>"; # form has been completed without errors }elsif ($formcomplete) { print "<p class=status>"; print "<input type=submit name=answer value=finish>"; print "</p>"; } print "</form>"; } print <<'ENDHTML'; </div> </td></tr> </table> </td></tr> <tr> <td colspan="4" height="25px" bgcolor="#f4f4f4"></td> </tr> <tr> <td colspan="4" height="25px" bgcolor="#F0251A"><p class="h2">&nbs +p;</p></td> </tr> ENDHTML $footer_file = "footer1.html"; open(FILE,"$footer_file"); while(<FILE>) { print $_; } close FILE;

Replies are listed 'Best First'.
Re: PLEASE Help with this script
by GrandFather (Saint) on Dec 16, 2009 at 21:01 UTC

    First off: always use strictures (use strict; use warnings;).

    A few other standard imprecations are:

    • eschew global variables. In particular don't use global variables in subs.
    • use the three parameter version of open.
    • always check the result from open and other such functions.
    • use lexical file handles: open my $inFile, '<', $fileName or die "Can't open $fileName:$!\n";
    • avoid hand rolled and hand parsed HTML. Use suitable modules instead - there are a few: (HTML).

    True laziness is hard work
      I came in at the end of this project and am not very familiar with perl at all. I used a working script and attempted to edit it to create a new file but it's not working out for me.

        At the start of the learning curve this advice may not help a lot, but the conventional wisdom is: first create a unit test for the code you are about to change, then make the change. The idea is that you write a regression test that confirms the current behavior doesn't get broken by changes that you make. It's smart to also write a test the confirms the expected new behavior works too (when it's implemented of course).

        The other important thing is to use a revision control system. That allows you to easily back out changes and to compare what you have now with some past version.


        True laziness is hard work
Re: PLEASE Help with this script
by Corion (Patriarch) on Dec 16, 2009 at 20:43 UTC

    Maybe you want to explain us how it fails to work for you?

      The script is supposed to create a txt file and write the data from the form to the page. But it is only creating the PO.txt file and not storing any info. The page is blank. also, when the customer logs in again of course there's no info to show up in the fields that they've filled out already. I've spent so much time on this and I have even used a working script and tried to edit it to create the page but it just comes out all messed up. I'd be willing to pay someone to fix this among other issues I'm having with this application. I am a web developer and I've never done anything more than a simple email form in cgi.

        You're not using strict, and the script is a horrible mess. I suggest a complete rewrite.

        At least the empty file will likely come from $write_data not being what you think it is. It is assigned to by this line of code:

        $write_data="$${paramname}:::";

        ... and I don't even want to track down to what global variable name is stored in $paramname and where it is then dereferenced to.

        Here is a page from the app that is working, I just don't see what I'm doing wrong.
        #!/usr/bin/perl ###################################################################### +###### # S U B R O U T I N E S ###################################################################### +###### ######################################################## sub valid_cookie{ $flag=0; my $session_file=$companyrepository."/session.txt"; #file where se +ssions are stored unless(open(INTT, "$session_file")){print "Session not found $sess +ion_file. $cookie<br>";} while (<INTT>){ #find existing cookie if($_=~/^$cookie\;/){#if cookie exist $flag=1; last; } } close INTT; return $flag; } ###################################################################### +###### sub writeData { my $j=0; open (OUT,">".$datafile); # Print header foreach $head (@header_names){ print OUT "$head\t"; } print OUT "\n"; # Print data $i=0; # $i represents each row + + for ($i=0;$i<=$#write_data;$i++){ for ($j=0;$j<=$#{$write_data[$i]};$j++){ print OUT "$write_data[$i][$j]\t"; } print OUT "\n"; } close (OUT); } ###################################################################### +###### sub readData { my @temp=""; open (IN,"<".$datafile); $i=0; # $i represents each row while (<IN>) { if ($i==0){ # ignore the header $i++; next; }elsif ($_=~/^(.*?)\t/){# read in and split tabs @temp=split("\t", $_); $j=0; foreach $t (@temp){ # each column $j in row $i $read_data[$i-1][$j]="$t"; # store names $j++; } $i++; } } close (IN); } ###################################################################### +###### sub validate_input{ my $p_name=shift; my $p_value=shift; my $vflag=shift; # cosname must contain an aphanumeric character if ($p_name=~/col0/){ if ($p_value!~/[0-9]/){ $vflag=0; } # cosnum must be a digit between 1 and 32767 }elsif($p_name=~/col1/){ if ($p_value!~/[A-Za-z0-9_]/){ $vflag=0; } } return $vflag; } ###################################################################### +###### # E N D S U B R O U T I N E S ###################################################################### +###### ###################################################################### +###### # M A I N ###################################################################### +###### use CGI; $query = new CGI; # initial global variables $repository="/home/warp/userdata/"; $paramcount=0; $formcomplete=1; @header_names=("LATA"); @read_data=(); @read_names=(); @read_cos=(); @data=(); @cos=(); @names=(); $error=""; # pluck out some common params that may have been passed $answer=$query->param("answer"); ####check for valid cookie#### $cookie=""; @temp=""; $cookie=$ENV{'HTTP_COOKIE'}; #identify cookie @temp_cookie=split(';',$cookie); $cookie=$temp_cookie[0]; ($sess_id,$company,$site,$user)=split('%2C', $cookie); $companyrepository=$repository.$company."/".$site; valid_cookie(); # If cookie not valid, then redirect to login page if ($flag==0){print $query->redirect('http://services.genband.com/cgi- +bin/dbq/na/DBQ_welcome.cgi'); }#redirect to another location if ($flag==1){ $dbqfile=$companyrepository."/dbq_finished.txt"; if (-e $dbqfile){ # DBQ has been finished and database has been built print $query->redirect('http://services.genband.com/cgi-bin/dbq/na +/home.cgi'); #redirect to home } } print "Content-type: text/html\n\n"; # create company specific variables $datafile=$companyrepository."/latas.txt"; ################################ # start html $header_file = "header1.html"; open(FILE,"$header_file"); while(<FILE>) { print $_; } close FILE; print <<'ENDHTML'; <tr> <td width="1020" bgcolor="#f4f4f4" align="center"> <table width="900" border="0" cellspacing="0" cellpadding="0"> <tr> <td bgcolor="#232021" height="25px" width="499" align="left" valig +n="bottom"><p class="h1"><a href="DBQ.cgi?tab=1#TabbedPanels1">Call P +rocessing Resources</a> : LATAs</p></td> <td bgcolor="#232021" height="25px" width="401" align="right" vali +gn="bottom"><p class="blinks"><a href="logout.cgi">Logout</a>&nbsp; | + &nbsp;<a href="DBQ_setPW.cgi">Add User</a>&nbsp; | &nbsp;<a href="co +ntact.cgi">Contact</a>&nbsp; | &nbsp;<a href="comment.cgi">Comment</a +></p></td> </tr> <tr><td colspan=2 align=left bgcolor=#ffffff class=border2 class=b +order2> ENDHTML # if the screens file exists then we have already been here once and s +aved valid data, read this data in if (!$answer) { if (-e $datafile){ readData; $formprocessed=1; }else{$formcomplete=0;} } # process passed parameters foreach $paramname ($query->param) { # $paramcount++; $paramvalue=$query->param($paramname); $$paramname=$paramvalue; $$paramname =~ s/\s+/_/g; # place the values in the appropriate array location if ($paramname =~ /row(\d+)col(\d+)/) { # row(i) and column(j) as +read from webpage $match1="$1"; $match2="$2"; $write_data[$1][$2]="$$paramname"; if ($match2==0){ if ($$paramname=~/[0-9]/){ # validate name push @fail, $match1; } } if ($$paramname=~/\D/){ $formcomplete=0; $error="Lata value must be numeric"; } } # print "$paramname, $$paramname, $paramvalue, $$paramvalue<br>"; if (($paramname eq "answer") && ($answer eq "finish")) { $finished=1; } } # trunkate list where Profile names are undefined $len=@fail; @write_data = splice (@write_data, $fail[0], $len); ##################################################################### # if the finish button has been clicked # ##################################################################### ##################################################################### # when entering this page from the main DBQ the answer will be null # ##################################################################### if (($finished) && ($formcomplete)) { writeData; print "<form action=DBQ.cgi?tab=1#TabbedPanels1 method=post>"; print "<p class=status>"; print "The class of service data has been saved. Please click next + to continue. <input type=submit name=answer value=next>"; print "</p>"; # if we are arriving from the main page, a next or re-submit button wa +s clicked, or there # was an error in the form when the finish button was clicked then red +raw the }else{ # if we are here from the main page and this form has been process +ed before then print this message if ($formprocessed) { print "<p class=main>"; print "The Class of Service form has already been processed at + least once, the data last submitted is below.<br><br>"; print "</p>"; } # print instructions print "<p class=status>"; print "In the table below is a list of the termination types that +will be data filled in the switch. "; print "To define a Class of Service, provide a name in the box at +the top of the column and then to the "; print "right of each termination type click the check box to allow + that termination type "; print "for the given screening class. An uncheck box will indicate + that the termination type is to be "; print "blocked for the screening class. For more details click the + help button."; print "</p>"; print "<hr>"; print "<form action='latacfg.cgi' method='post'>"; # present form print "<div style=background-color:#ffffff;overflow:scroll;width:9 +00px;text-align:top;border:1;>"; print "<table cellpadding=0 cellspacing=0 border=0 style=margin:.1 +in;><tr>"; print "<th class=\"mess left_border\" >LATA<span>Specify the LATA +in which you will be providing service.</span></th>"; print "</tr>"; # Print rows for ($i=0;$i<=10;$i++) { print "<tr >"; for ($j=0;$j<=0;$j++){ $type=""; print "<td align=center>"; $paramname="row".$i."col".$j; if ($read_data[$i][$j]){ $type="<input style=text-align:center type=text size=17 maxlen +gth=16 name=$paramname value=$read_data[$i][$j]>"; }else{$type="<input style=text-align:center type=text size=17 +maxlength=16 name=$paramname value=$$paramname>";} print "$type"; print "</td>"; } print "</tr>"; } print "</table></div>"; print "<div style=margin:.2in>"; # initial presentation of the page if ((!$formcomplete) && ($error eq "")) { print "<input type=submit name=answer value=submit>"; print "<button type=button type=button type=button onclick=\"w +indow.location='DBQ.cgi?tab=1#TabbedPanels1'\">"; print "cancel</button> Click 'cancel' to return to Main Menu w +ithout saving your data.<br>"; print "Click 'submit' to proceed with this form."; # errors found when processing }elsif((!$formcomplete) && ($error ne "")){ print "<input type=submit name=answer value='re-submit'>"; print "<button type=button type=button type=button onclick=\"w +indow.location='DBQ.cgi?user=$user&amp;company=$company'\">"; print "cancel</button> $error"; print "Click 'cancel' to return to the Main Menu<br>without sa +ving your data or"; print " please make corrections and click re-submit to proceed + with this form."; # form has been completed without errors }elsif ($formcomplete) { print "<input type=submit name=answer value=re-submit>"; print "<button type=button type=button type=button onclick=\"w +indow.location='DBQ.cgi?tab=1#TabbedPanels1'\">cancel</button>"; print "<input type=submit name=answer value=finish> "; print "The data on the form has been validated. Click 'cancel' + to return to the Main Menu<br>without saving your data,"; print " click 're-submit' to make changes, or click 'finish' to save y +our data and return to the Main Menu."; } print "</form>"; } print "</td></tr>"; print <<'ENDHTML'; </table> </td> </tr> <tr> <td colspan="4" height="25px" bgcolor="#f4f4f4"></td> </tr> <tr> <td colspan="4" height="25px" bgcolor="#F0251A"><p class="h2">&nbs +p;</p></td> </tr> ENDHTML $footer_file = "footer1.html"; open(FILE,"$footer_file"); while(<FILE>) { print $_; } close FILE;
      I forgot to mention that the upload file function isn't working at all.

        I don't even see where in the script you would have an "upload" functionality. Maybe you should cut down the script to 10 or 20 lines and start working from there. Also this HTML:

        ... <form action='/home/warp/userdata/$companyrepository' method='post' en +ctype='multipart/form-data'> ...

        is quite unlikely to work, unless your webserver actually serves a directory /home/warp/userdata/..., which very few do by default.