LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   Perl Script..Difficult to understand ?? (https://www.linuxquestions.org/questions/linux-general-1/perl-script-difficult-to-understand-622468/)

ajeetraina 02-20-2008 06:01 AM

Perl Script..Difficult to understand ??
 
My Senior Manager has delivered me the script (Perl+ CGI+ HTML+Javascript) and hope you will help me in understanding the same.
I am sorry for the entire script being posted but at the end of day I dint understand what this perl script means.I am pasting you few excerpt.Hope you will defntly help me :

---------------------------------------------------------------
Code:

#!/usr/bin/perl
#use CGI;# qw(:standard);
use CGI::Carp qw(warningsToBrowser fatalsToBrowser);
#my $query = new CGI;
#use strict;
use DBI;
use Spreadsheet::WriteExcel;
print "Content-type: text/html\n\n";

$REMOTE_USER=$ENV{REMOTE_USER};
$mailServer="10.14.236.120:25";
$sendEmail="/var/www/action/sendEmail";

###############################################
local(*queryString) =@_ if @_;
  $buffer =$ENV{"QUERY_STRING"};
if($buffer eq "")
{
  read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
}

  @pairs = split(/&/, $buffer);

  foreach $pair (@pairs) {
      ($name, $value) = split(/=/, $pair);
      $value =~ tr/+/ /;
      $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
      $value =~ s/<!--(.|\n)*-->//g;
      $value =~ s/\n/""/eg;
      $value =~ s/\r/" "/eg;
      #$value =~ s/\"/""/eg;
                  $in{$name} = $value;
                                  $common_allgetpostdata[$common_i]="$name~~~~$value";
                                                          $common_i++;

                                                                                  }
#######################################################

open(OUTOA, "<db.inf");
while(<OUTOA>)
{
$w1q="";($eq1,$w1q)=split/database=/;
if($w1q ne ""){
$w1q =~ s/  /""/eg;
$w1q =~ s/\n/""/eg;
$w1q =~ s/\r/""/eg;
chomp $w1q;
$database=$w1q;
}
$w1q="";($eq1,$w1q)=split/user=/;
if($w1q ne ""){
$w1q =~ s/  /""/eg;
$w1q =~ s/\n/""/eg;
$w1q =~ s/\r/""/eg;
chomp $w1q;
$user=$w1q;
}
$w1q="";($eq1,$w1q)=split/host=/;
if($w1q ne ""){
$w1q =~ s/  /""/eg;
$w1q =~ s/\n/""/eg;
$w1q =~ s/\r/""/eg;
chomp $w1q;
$host=$w1q;
}
$w1q="";($eq1,$w1q)=split/pass=/;
if($w1q ne ""){
$w1q =~ s/  /""/eg;
$w1q =~ s/\n/""/eg;
$w1q =~ s/\r/""/eg;
chomp $w1q;
$pass=$w1q;
}
}

close(OUTOA);


$showerror=1;
$dbh = DBI->connect("DBI:mysql:$database:$host",$user,$pass) ||  die "Unable to connect to Contacts Database: $dbh->errstr\n";
###########################################################
$sqlquery="select userID,userName,firstName,lastName,mail,userRoleID from users where userName='$REMOTE_USER'" ;
$sth = $dbh->prepare($sqlquery);$sth->execute;$eee=$dbh->errstr;if($showerror==1){print "$eee";}
        while (@rows=$sth->fetchrow) {
$userid=@rows[0];
$username=@rows[1];
$firstName=@rows[2];
$lastName=@rows[3];
$userMail=@rows[4];
$roleid=@rows[5];
                        }
##############################################
open(OUTOA, "<head.html");
while(<OUTOA>)
{
$htmlheader.=$_;
}
close(OUTOA);
#################################################
@timelist = localtime( time() ) ;
if( $month eq "" ) {
  $month = $timelist[4] + 1 ;
}
if( $year eq "" ) {
  $year = $timelist[5]+1900 ;
}
$dayofmonth = $timelist[3] ;
$curmonth = $timelist[4]+1 ;
$curmonth = "0$curmonth";
$curyear = $timelist[5]+1900;

%monthnames = ('01' => Jan, '02' => Feb, '03' => Mar, '04' => Apr, '05' => May, '06' => Jun, '07' => Jul, '08' => Aug, '09' =>Sep, '10' => Oct, '11' => Nov, '12' => Dec) ;


%months_num = ('Jan' => '01', 'Feb' => '02', 'Mar' => '03', 'Apr' => '04', 'May' => '05', 'Jun' => '06', 'Jul' => '07', 'Aug' => '08', 'Sep' => '09', 'Oct' => '10', 'Nov' => '11', 'Dec' => '12');

sub name_mon {
$date=shift;
($syear,$smon,$sdate)=split /-/, $date;
$smon_name=$monthnames{$smon};
$date="$sdate-$smon_name-$syear";
                                        }
sub num_mon {
$date=shift;
($date,$mon,$year)=split /-/, $date;
$mon_num=$months_num{$mon};
$date="$year-$mon_num-$date";
                                }
if ($userid eq ""){print "<center> <font face=\"MS Sans Serif\" size=4 style=\"color:#000000\"><b>Please register your NIS Login in Effort Tracking Tool to access</font></b><br>";}
else {
print "$htmlheader";}

print "<script language=\"javascript\" type=\"text/javascript\" src=\"datetimepicker.js\"></script>";
if ($in{'action'} eq "add_task" ) {
print <<"XYZ";
<form name="form1" id=guideform method="post" action="$ENV{'SCRIPT_NAME'}"  >
        <!--  <form action="$ENV{'SCRIPT_NAME'}" method = "post" enctype=multipart/form-data> -->
                                                        <tr>
                                                                <td colspan=4>
                                                                        <table border="0" width="100%">
                                                                                <tr>
                                        <td align="right"><b>Date:</b>&nbsp;&nbsp;&nbsp;</td>
                                                                            <td align="left">
                          <input type="text" id="cal" name="txtDate" readonly ><a href="javascript:NewCal('cal','ddmmmyyyy')"><img src="cal.gif" width="16" height="16" border="0" alt="Pick a date"></a>
                                                                    </td>
 <td align="right"><b>Select Type:</b>&nbsp;&nbsp;&nbsp;</td>
                                                                    <td align="left">
<SELECT name="guidelinks" onchange="window.location=document.guideform.guidelinks.options[document.guideform.guidelinks.selectedIndex].value">
 <OPTION SELECTED value="">Select Task
 <OPTION value="calendarPage.html">Support Tasks
 <OPTION value="$ENV{'SCRIPT_NAME'}?action=other_task">Other Task
 </SELECT>
 &nbsp;&nbsp;
                                                </td>
                                                                        </tr>

XYZ

        }
if ($in{'action'} eq "create_task"){

print <<"XYZ";

<script type="text/javascript">
function validate_required(field,alerttxt)
{
with (field)
{
if (value==null||value==""||value==" ")
  {alert(alerttxt);return false}
else {return true}
}
}function validate_form(thisform)
{
with (thisform)
{
if (validate_required(txtEndDate,"End Date must be filled out!")==false)
  {txtEndDate.focus();return false}
if (validate_required(txtTaskName,"Task Name must be filled out!")==false)
  {txtTaskName.focus();return false}
if (validate_required(txtDescription,"Task Description must be filled out!")==false)
  {txtDescription.focus();return false}
}
}
</script>



<script type="text/javascript">
function fillText()
{
 var SimpleString = document.guideform.OperationDropDown.value;
var myArray = SimpleString.split(" ");
document.guideform.DetailText.value = myArray[0];
document.guideform.DetailText1.value = myArray[1];
document.guideform.DetailText2.value = myArray[2];
document.guideform.DetailText3.value = myArray[3];
// document.guideform.DetailText.value=document.guideform.OperationDropDown.value;
}
</Script>

<form name="form1" id=guideform onsubmit="return validate_form(this)" method="post" action="$ENV{'SCRIPT_NAME'}"  >
        <!--  <form action="$ENV{'SCRIPT_NAME'}" method = "post" enctype=multipart/form-data> -->
<table width="480 px" border="0" align="left" valgin="top" cellpadding="3">
                                                      <tr>
        <td colspan="4" height="50 px" align="center" valign="bottom"><h3>Create Task</h3></td>
                                                        </tr>
                                                        <tr>
                                                                <td colspan=4>
                                                                        <table border="0" width="100%">
                                                                                <tr>
                                        <td align="right"><b>Start Date:</b>&nbsp;&nbsp;&nbsp;</td>
                                                                            <td align="left">
                          <input type="text" id="cal" size=12 name="txtStartDate" readonly value='$dayofmonth-$monthnames{$curmonth}-$curyear' ><a href="javascript:NewCal('cal','ddmmmyyyy')"><img src="images/cal.gif" width="16" height="16" border="0" alt="Pick a date"></a>
                                                                    </td>
 <td align="right"><b>End date:</b>&nbsp;&nbsp;&nbsp;</td>
                                                                    <td align="left">
<input type="text" id="ecal" size=12 name="txtEndDate" readonly value='' ><a href="javascript:NewCal('ecal','ddmmmyyyy')"><img src="images/cal.gif" width="16" height="16" border="0" alt="Pick a date"></a>
                                                </td>
                                                                        </tr>
<tr>
<td align="right"><b>Task Name:</b>&nbsp;&nbsp;&nbsp;</td>
<td align="left" colspan=3><input type=text name="txtTaskName" maxlength=35 size=43 rows=2 cols=42></textarea></td>
                                                                        </tr>


<tr>
                                          <td align="right"><b>Description:</b>&nbsp;&nbsp;&nbsp;</td>
  <td align="left" colspan=3><textarea name="txtDescription" rows=2 cols=42></textarea></td>
</tr>
<tr>
<td align="right"><b>Common Task:</b>&nbsp;&nbsp;&nbsp;</td><td align="left" colspan=3><input type="checkbox" name="common" value="y"  ></td>
</tr>
<input type="hidden" id="DetailText" size=12 name="DetailText" readonly value='' >
<input type="hidden" id="DetailText1" size=12 name="DetailText1" readonly value='' >
<input type="hidden" id="DetailText2" size=12 name="DetailText2" readonly value='' >
<input type="hidden" id="DetailText3" size=12 name="DetailText3" readonly value='' >
XYZ
if ($roleid == '2'){
print "<tr>";
print "<td align=\"right\"><b>Select userID to assign:</b></td>";
print "<td align=\"left\" colspan=3>";
print "<SELECT NAME=\"OperationDropDown\" onChange=\"fillText();\" >";
print "<OPTION selected  value=''>select User";
$sqlquery="select userID,firstName,lastName,mail from users" ;
$sth = $dbh->prepare($sqlquery);$sth->execute;$eee=$dbh->errstr;if($showerror==1){print "$eee";}
while (@rows=$sth->fetchrow) {
$mail=@rows[3];
        print "<Option value=\"@rows\">@rows[1] @rows[2]</option>";
                                }


print "</tr></td>";

                        }
print <<"XYZ";
                                                                                </tr>
<tr>
                      <!--                                                        <tr>
                                                                      <td colspan="4">&nbsp;</td>
                                                                                </tr>
<tr>
                    <td align="right"><b>Completed:</b>&nbsp;&nbsp;&nbsp;</td>
                                              <td align="left">
                                              <input type="Radio" id=radio name="txtstatus" size=5 maxlength=3 value='completed'>
                                        </td>
                                      <td align="right"><b>Status:</b>&nbsp;&nbsp;&nbsp;</td>
                                      <td align="left">
                        <input type="textbox" name="txtcompstatus" readonly size=5 maxlength=3 value='100'>%
                                      </td>
                                            </tr>

                                                                                <tr>
                    <td align="right"><b>Pending:</b>&nbsp;&nbsp;&nbsp;</td>
                                              <td align="left">
                                              <input type="Radio" id=radio name="txtstatus" size=5 maxlength=3 value='pending'>
                                        </td>
                                      <td align="right"><b>Status:</b>&nbsp;&nbsp;&nbsp;</td>
                                      <td align="left">
                        <input type="textbox" name="txtnotstatus" size=5 maxlength=3 value=''>%
                                      </td>
                                            </tr>
<tr>
                    <td align="right"><b>Start Time:</b>&nbsp;&nbsp;&nbsp;</td>
                                              <td align="left">
                                              <input type="textbox" name="txtStartTime" size=5 maxlength=5 value=''>
                                        <br>
                                      (<b>hh:mm</b>-10:00-)
                                        </td>
                                      <td align="right"><b>End Time:</b>&nbsp;&nbsp;&nbsp;</td>
                                      <td align="left">
                        <input type="textbox" name="txtEndTime" size=5 maxlength=5 value=''>
                                <br>
                                  (<b>hh:mm</b>-13:00-)
                                      </td>
                                            </tr> -->

                                                <tr>
                              <td colspan="4">&nbsp;</td>
                                                </tr>
                                            <tr>
                                      </tr>
<tr>
XYZ
if ($roleid == '2'){print "<td align=\"center\" colspan=4><input type=\"hidden\" name=\"action\" value=\"createtask_assign\">"; }
else {print" <td align=\"center\" colspan=4><input type=\"hidden\" name=\"action\" value=\"createtask\">"; }
print <<"XYZ";
                                        <input type="submit" name="btnSave" value='Add'>
                                                                                                &nbsp;&nbsp;&nbsp;&nbsp;<input  type="reset" name="ion" value="clear"></td>
                                                                                        </td></tr>
XYZ
                }


Su-Shee 02-20-2008 06:46 AM

That's simple and pretty straightforward - nothing special supadupa perl-ish secretive in there. An average CGI script including some DB connections and contructing some JavaScript for validating form dield input with a little bit of HTML tables around it.

It uses DBI as database API and the CGI module as it is common in the Perl world.

Or are you asking us to do your job and analyze the script in detail? ;)

ajeetraina 02-20-2008 11:17 PM

One thing I want to ask is which database server it is pointing to.He gave me Mysql backup tar file ,webtool for report generation and all I ran them.I think its hsould point to the localhost only.But doubt about it.
Pls help me to understand first few perl codes


All times are GMT -5. The time now is 02:59 PM.