Perl CGIDBI

  You are currently not logged in. You can view the forums, but cannot post messages. Log In | Register

16-Nov-09 12:27
Hi, I am new for Perl & Perl CGI/DBI module. I have managed to learn the basics of both. Can anyone suggest how could I start my career in this. Let me know what material I can use to learn further. Thanks in advance.
17-Nov-09 00:34
Hi again perlprofesional, and welcome to ELATED!

The best bet is probably to practise writing some useful database-driven Perl scripts. Once you start using CGI.pm and the DBI for real-world projects then you'll find that your knowledge expands pretty rapidly!

A great reference book for Perl in general is Programming Perl, AKA "The Camel Book":

http://oreilly.com/catalog/9780596000271

It's quite technical so I wouldn't recommend it for absolute beginners, but it sounds like you're over the first hurdles now.

Hope that helps!

Cheers,
Matt

--
Matt Doyle, Elated
Second Edition of my jQuery Mobile book out now! Learn to build mobile web apps. Free sample chapter: http://store.elated.com/
17-Nov-09 10:48
Thanks Matt for your info. Please help me out on the below query.

Currently I am facing problem using syntax...I am using as below.

if I use --- print <<'EOF'; in CGI program --- am not able to supply variable inside HTML tags if I use single quotes .....Eg. $input (the value of $input is not displaying in HTML page.its just diplaying $input).

If I use --- print <<"EOF"; ---- If I use double quotes...the above condition is working but am not able to use Javascript inside HTML Tags........(Eg. Form Validation is not working).

I want to use both above ( need to pass Variable successfully and use Javascript for form validation).

Please help me out.

Thanks in advance.
18-Nov-09 04:38
There shouldn't be any problem with JavaScript inside tags when using the double-quote heredoc syntax. Can you post your code so I can take a look?

Cheers,
Matt

--
Matt Doyle, Elated
Second Edition of my jQuery Mobile book out now! Learn to build mobile web apps. Free sample chapter: http://store.elated.com/
18-Nov-09 10:55
It is a lengthy one, I have cut shorted few things and changed..........I have used double quotes here....form validation funtionality is not working....I have given isAlphabet function for "Originator" option to get the name without any numericals but it's just accepting everything if Iuse double quotes..no alert message.........but its showing the date value correctly.

But if I use single quotes....am alert message for getting numerical input for "Originator" but the value of date is $date.


#!/usr/bin/perl -w
#Program to create EPM value addition.

#use strict;
use CGI;
use CGI::Carp qw(warningsToBrowser fatalsToBrowser);
$query=new CGI;

my $date=`date +%m/%d/%Y`;

print "Content-type: text/html\n\n";
print <<"EOF";
<html>
<title>EPM_value_add_log</title>
<head>
<script type="text/javascript">
function formValidator(){
var user = document.getElementById('user');
var date = document.getElementById('date');
var req_time = document.getElementById('x_time');
var org_time = document.getElementById('xx_time');
var new_time = document.getElementById('xxx_time');
var num_events = document.getElementById('y');
var rep_save = document.getElementById('yy');
var onet_save = document.getElementById('yyy');
var value_point = document.getElementById('xyxy');

if(isAlphabet(user, "Please Enter Valid Name")){
if(dateCheck(date, "Please Enter the Date in the Given Format")){
if(timeCheck(req_time, "Please Enter Time in Mins - Time Required To Create")){
if(timeCheck(org_time, "Please Enter Time in Mins - Original Time/Event")){
if(timeCheck(new_time, "Please Enter Time in Mins - New Time/Event")){
if(timeCheck(num_events, "Please Enter No. of Events")){
if(amountCheck(rep_save, "Please Enter The Amount for Repeatable Process Savings")){
if(amountCheck(onet_save, "Please Enter The Amount for One Time Savings")){
if(timeCheck(value_point, "Please Enter The Correct Value Point")){
return true;
}

}
}
}
}
}
}
}
}
return false;
}

function amountCheck(elem, helperMsg){
var amountCalc = /[0-9\.]/;
if(elem.value.match(amountCalc)){
return true;
}else{
alert(helperMsg);
elem.focus();
return false;
}
}

function timeCheck(elem, helperMsg){
var timeCalc = /^[0-9]{1,3}$/;
if(elem.value.match(timeCalc)){
return true;
}else{
alert(helperMsg);
elem.focus(); // set the focus to this input
return false;
}
return true;
}

function dateCheck(elem, helperMsg){
var numericExpression = /^[0-9]{1,2}\/[0-9]{1,2}\/[0-9]{4}$/;
if(elem.value.match(numericExpression)){
return true;
}else{
alert(helperMsg);
elem.focus();
return false;
}
}

function isAlphabet(elem, helperMsg){
var alphaExp = /^[a-zA-Z]+/;
if(elem.value.match(alphaExp)){
return true;
}else{
alert(helperMsg);
elem.focus();
return false;
}
}
</script>
</head>

<h1 align=center>XXXXXX</h1>
<hr/>
<body bgcolor="beige">
<p align=center style="font-size:20px;"><b>Please Provide the XXX Details:</p>
<form onsubmit='return formValidator()'>
<p align=center>Today's Date:$date</p>
<h3>Project Details:</h3>
<table cellpadding=20>
<tr><td><b>Project:
<select name="project_name">
<option value="xx">xx</option>
<option value="xx">xx</option>
</td>
<td><b>Year:
<select name="year">
<option value="2006">2006</option>
<option value="2007">2007</option>
<option value="2008">2008</option>
<option value="2009">2009</option>
<option value="2010">2010</option>
<option value="2011">2011</option>
</td>
<td><b>Status:
<select name="status">
<option value="xx">xxx</option>
<option value="xxx">xxx</option>
<td><b>Originator:<input type='text' id='user'/></td>
</tr>
<td><b>QTR:
<select name="quater">
<option value="QTR1">QTR1</option>
<option value="QTR2">QTR2</option>
<option value="QTR3">QTR3</option>
<option value="QTR4">QTR4</option>
</td>
<td><b>Date_Submitted:<input type='text' id='date'/> (MM/DD/YYYY)</td></tr>
</table>
<hr/>
<input type="submit" value="Submit"/>
</form>
</body>
</html>
EOF


[Edited by matt on 18-Nov-09 17:24]
18-Nov-09 17:32
(I put code markers around your code to make it easier to read!)

For one thing, you have an escaping issue. For example, your JavaScript contains lines like this:


var amountCalc = /[0-9\.]/;


You've escaped your dot in the JavaScript with a backslash (\). However, Perl also uses a backslash for escaping. This results in your JavaScript actually being output like this:


var amountCalc = /[0-9.]/;


(Run your Perl script then view source in your web browser to see what I mean.)

The solution is to double-escape the backslash in all cases, eg:


var amountCalc = /[0-9\\.]/;


Frankly though, you'd be better off moving all your JavaScript functions into separate .js files, thereby avoiding the problem altogether.

Cheers,
Matt

--
Matt Doyle, Elated
Second Edition of my jQuery Mobile book out now! Learn to build mobile web apps. Free sample chapter: http://store.elated.com/
19-Nov-09 09:08
Thanks Matt for your excellent info..let me try and get back to you.
23-Nov-09 14:19
Hi Matt,

thanks for the info. I have created a seperate .js file and invoked it as below. It's working(form Validation) for both syntax - print <<"EOF"; & print <<'EOF'; But the called variable is not showing the value still.........instead, its showing as $date. Please assist me.
23-Nov-09 19:58
Hi perlprofesional,

It should work OK provided you use double quotes (or miss out the quotes entirely). Can you post your new code please, and I'll take a look?

Cheers,
Matt

--
Matt Doyle, Elated
Second Edition of my jQuery Mobile book out now! Learn to build mobile web apps. Free sample chapter: http://store.elated.com/
25-Nov-09 10:38
Please find the modified code below. Now, once I click the EPM_value.CGI, the page with correct date is coming first time correctly. then once i give any value and click submit or enter...it is changing to $date with blank values. After that, the validation part is working properly.

I have used two new lines to load js files as below.

<script src='formvalidator.js'></script> and <body onLoad='initTable("table0");' bgcolor="#C0C0C0">

Script Name - EPM_value.CGI



#!/usr/bin/perl -w
#Program to create EPM value addition.

#use strict;
use CGI;
use CGI::Carp qw(warningsToBrowser fatalsToBrowser);
$query=new CGI;

my $date=`date +%m/%d/%Y`;

my $original_time=$query->param('original_time');
my $new_time=$query->param('new_time');
my $saved_time=($original_time - $new_time);
my $num_of_events=$query->param('num_of_events');
my $time_reduction=($saved_time * $num_of_events * 52);
my $repeat_savings=$query->param('repeat_savings');
my $one_savings=$query->param('one_savings');
my $total_savings=($repeat_savings + $one_savings);

if ( $original_time =~ /^d+$/ && $new_time =~ /^d+$/ ){
my $saved_time=($original_time - $new_time);
}

print "Content-type: text/html\n\n";
print <<"EOF";
<html>
<script src='formvalidator.js'></script>
<title>EPM_value_add_log</title>
<h1 align=center>EPM VALUE ADDITION LOG</h1>
<hr/>
<body onLoad='initTable("table0");' bgcolor="#C0C0C0">
<p align=center style="font-size:20px;"><b>Please Provide the Value Addition Details: <input type="button" value="Back" onClick="history.go(-1)"></p>
<form action='EPM_value_add_log.CGI' method='post' onsubmit='return formValidator()'>
<p align=center>Today's Date:$date</p>
<h3>Project Details:</h3>
<table cellpadding=20>
<tr><td><b>Project:
<select name="project_name">
<option value="NA_ASG_EFS">NA_ASG_EFS</option>
<option value="WW_ASG_IBD">WW_ASG_IBD</option>
<option value="WW_IMASG_L1">WW_IMASG_L1</option>
<option value="WW_ER_ASG">WW_ER_ASG</option>
<option value="WW_AI_HST_L1">WW_AI_HST_L1</option>
<option value="NA_ASG_RCMCL1">NA_ASG_RCMCL1</option>
<option value="NA_ASG_RCMC">NA_ASG_RCMC</option>
</td>
<td><b>Year:
<select name="year">
<option value="2006">2006</option>
<option value="2007">2007</option>
<option value="2008">2008</option>
<option value="2009">2009</option>
<option value="2010">2010</option>
<option value="2011">2011</option>
</td>
<td><b>Status:
<select name="status">
<option value="Indevelopment">Indevelopment</option>
<option value="Idea">Idea</option>
<option value="Scripted Testing">Scripted Testing</option>
<option value="Testing">Testing</option>
<option value="Implemented">Implemented</option></td>
<td><b>Originator:<input type='text' id='user'/></td>
</tr>
<tr><td><b>Team:
<select name="team">
<option value="EFS">ERS</option>
<option value="ER">ER</option>
<option value="FID">FID</option>
<option value="IBD">IBD</option>
<option value="MSID">MSID</option>
<option value="MSPA">MSPA</option>
<option value="MSPM">MSPM</option>
<option value="MM">MM</option>
<option value="R-MW">R-MW</option>
<option value="SCALE">SCALE</option>
<option value="L1">L1</option>
<option value="CRISK">CRISK</option>
<option value="Custody">Custody</option>
<option value="AAG">AAG</option>

</td>
<td><b>QTR:
<select name="quater">
<option value="QTR1">QTR1</option>
<option value="QTR2">QTR2</option>
<option value="QTR3">QTR3</option>
<option value="QTR4">QTR4</option>
</td>
<td><b>Category:
<select name="category">
<option value="Script">Script</option>
<option value="Automation">Automation</option>
<option value="Event Elimination">Event Elimination</option>
<option value="Script/Webpage">Script/Webpage</option>
<option value="Tool Development">Tool Development</option>
<option value="Webpage">Webpage</option>
<option value="Webpage Automation">Webpage Automation</option>
<option value="Webpage Script">Webpage Script</option>
<td><b>Date_Submitted:<input type='text' id='date'/> (MM/DD/YYYY)</td></tr>
</table>
<hr/>
<h3>Other Details:</h3>
<table cellpadding=10>
<tr><td><b>Time Required To Create(In Mins): <input type="text" size=3 name="req_time" id="req_time"/></td>
<td><b>Original Time/Event(In Mins): <input type="text" size=3 name="original_time" id="org_time"/></td>
<td><b>New Time/Event(In Mins): <input type="text" size=3 name="new_time" id="new_time"/></td></tr>
<td><b>Number Of Events/Week: <input type="text" size=3 name="num_of_events" id="num_events"/></td>
<td><b>Repeatable Process Savings($$$): <input type="text" size=3 name="repeat_savings" id="rep_save"/></td>
<td><b>One Time Savings($$$): <input type="text" size=3 name="one_savings"/ id="onet_save"></td></tr>
<tr><td><b>Assumed Benifit Value(Point Value): <input type="text" size=3 name="value_point" id="value_point"/></td></tr>
</table>
<hr/>
<h3>Project Description:</h3>
<textarea rows="10" cols="80"></textarea>
<h3>Benefit To Customer:</h3>
<textarea rows="10" cols="80"></textarea>
<br/><br/>
<input type="submit" value="Submit"/>
</form>
<p>Result=$saved_time, $time_reduction, $total_savings</p>
</body>
</html>
EOF


********
.js file - formvalidator.js ,,, I have double slash (\\) in three places..........



<html>
<head>
<script type="text/javascript">
function formValidator(){
var user = document.getElementById('user');
var date = document.getElementById('date');
var req_time = document.getElementById('req_time');
var org_time = document.getElementById('org_time');
var new_time = document.getElementById('new_time');
var num_events = document.getElementById('num_events');
var rep_save = document.getElementById('rep_save');
var onet_save = document.getElementById('onet_save');
var value_point = document.getElementById('value_point');

if(isAlphabet(user, "Please Enter Valid Name")){
if(dateCheck(date, "Please Enter the Date in the Given Format")){
if(timeCheck(req_time, "Please Enter Time in Mins - Time Required To Create")){
if(timeCheck(org_time, "Please Enter Time in Mins - Original Time/Event")){
if(timeCheck(new_time, "Please Enter Time in Mins - New Time/Event")){
if(timeCheck(num_events, "Please Enter No. of Events")){
if(amountCheck(rep_save, "Please Enter The Amount for Repeatable Process Savings")){
if(amountCheck(onet_save, "Please Enter The Amount for One Time Savings")){
if(timeCheck(value_point, "Please Enter The Correct Value Point")){
return true;
}
}
}
}
}
}
}
}
}
return false;
}

function amountCheck(elem, helperMsg){
var amountCalc = /[0-9\\.]/;
if(elem.value.match(amountCalc)){
return true;
}else{
alert(helperMsg);
elem.focus();
return false;
}
}

function timeCheck(elem, helperMsg){
var timeCalc = /^[0-9]{1,3}$/;
if(elem.value.match(timeCalc)){
return true;
}else{
alert(helperMsg);
elem.focus(); // set the focus to this input
return false;
}
return true;
}

function dateCheck(elem, helperMsg){
var numericExpression = /^[0-9]{1,2}\\/[0-9]{1,2}\\/[0-9]{4}$/;
if(elem.value.match(numericExpression)){
return true;
}else{
alert(helperMsg);
elem.focus();
return false;
}
}

function isAlphabet(elem, helperMsg){
var alphaExp = /^[a-zA-Z]+/;
if(elem.value.match(alphaExp)){
return true;
}else{
alert(helperMsg);
elem.focus();
return false;
}
}

</head>
</html>



Thanks.

[Edited by matt on 25-Nov-09 17:17]
25-Nov-09 17:20
Hi perlprofesional,

Please put code markers around your code (the top-right icon above the edit box) - it makes it much easier to read!

Couple of problems I spotted right away:

1. You shouldn't have the <html><head><script type="text/javascript"> ... </head></html> stuff around your JavaScript code in your .js file. JavaScript code files should contain pure JavaScript code - nothing else.

2. You shouldn't use double-slashes (\\) in your JavaScript if it's in a separate .js file. You only need the double-slashes if you're including the JavaScript code directly within your Perl script.

Hope that helps!
Matt

--
Matt Doyle, Elated
Second Edition of my jQuery Mobile book out now! Learn to build mobile web apps. Free sample chapter: http://store.elated.com/

 
New posts
Old posts

Follow Elated