LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
LinkBack Search this Thread
Old 02-20-2008, 06:01 AM   #1
ajeetraina
Member
 
Registered: Jun 2007
Location: India
Distribution: Ubuntu,Red Hat, Fedora
Posts: 292

Rep: Reputation: 30
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
		}
 
Old 02-20-2008, 06:46 AM   #2
Su-Shee
Member
 
Registered: Sep 2007
Location: Berlin
Distribution: Slackware
Posts: 509

Rep: Reputation: 41
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?
 
Old 02-20-2008, 11:17 PM   #3
ajeetraina
Member
 
Registered: Jun 2007
Location: India
Distribution: Ubuntu,Red Hat, Fedora
Posts: 292

Original Poster
Rep: Reputation: 30
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Help me understand this shell script dwhitney67 Linux - Software 2 10-18-2007 09:02 AM
Converting a Windows Perl script to a Linux Perl script. rubbercash Programming 2 07-19-2004 10:22 AM
Tools to help read/understand perl codes?? friendklay Programming 0 04-21-2004 03:56 PM
fat fs code difficult to understand ramya272 Programming 7 03-07-2004 12:43 AM
Including methods from a perl script into another perl script gene_gEnie Programming 3 01-31-2002 05:03 AM


All times are GMT -5. The time now is 03:58 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration