Solved

perl uninitialized error

Posted on 2010-09-09
9
396 Views
Last Modified: 2013-12-25
Hi,

I am trying to draw the last created record's id number from an access database and use it in perl to repopulate a new record (with the drawn id number +1).
I keep getting an "uninitialized value...in addition (+) at..." error.

I'm still kind of fresh to perl. Can someone take a peek at my code and tell me what's wrong?

Thanks.
#!c:\perl\bin\perl.exe -w

use strict;
use CGI ':standard';
use Win32::ODBC; #use ODBC package

### Connect to a data source
my $db = new Win32::ODBC("DSN=rreAccesscm485a5") || die Win32::ODBC::Error();

### Perform INSERT of new customer

#Retrieve values for new customer from HTML form

my $f_name	= param('f_name');
my $mi		= param('mi');
my $l_name	= param('l_name');
my $h_phone	= param('h_phone');
my $street	= param('street');
my $city	= param('city');
my $state	= param('state');
my $zip		= param('zip');

#Getting a value for the next customer ID
my $last_id;

$db-> FetchRow;
$last_id = $db->Data();
my $next_id = $last_id+1;

my $sql;

$sql = qq{INSERT INTO customer (cust_id, f_name, mi, l_name, h_phone, address_street, address_city, address_state, address_zipcode)};
#$sql .= qq{VALUES ('$next_id','$f_name', '$mi', '$l_name', '$h_phone', '$street', '$city', '$state', '$zip' ) };
print 'SQL string to execute=' . $sql . "\n";

#execute INSERT statement
$db->Sql($sql); 
print "<h2>Database INSERT successful.</h2>";
# Disconnect
$db->Close();
# Provide an option to show updated table contents
print '<form ACTION="cust_data.pl" METHOD="get">' .
'<input TYPE="submit" VALUE="Show All Customers">' .
'</form>'

Open in new window

0
Comment
Question by:g_currier
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
  • 2
  • +1
9 Comments
 
LVL 84

Accepted Solution

by:
ozo earned 50 total points
ID: 33638112
It might help if you showed what was in the  ...
splain
/usr/bin/splain: Reading from STDIN
Use of uninitialized value $a in addition (+) at -e line 1.
Use of uninitialized value $a in addition (+) at -e line 1 (#1)
    (W uninitialized) An undefined value was used as if it were already
    defined.  It was interpreted as a "" or a 0, but maybe it was a mistake.
    To suppress this warning assign a defined value to your variables.
   
    To help you figure out what was undefined, perl will try to tell you the
    name of the variable (if any) that was undefined. In some cases it cannot
    do this, so it also tells you what operation you used the undefined value
    in.  Note, however, that perl optimizes your program and the operation
    displayed in the warning may not necessarily appear literally in your
    program.  For example, "that $foo" is usually optimized into "that "
    . $foo, and the warning will refer to the concatenation (.) operator,
    even though there is no . in your program.


the only + I see in your program is
my $next_id = $last_id+1;
which could suggest that $last_id was undefined
0
 

Author Comment

by:g_currier
ID: 33638796
yes, $last_id is undefined.  But what do i define it as except for $last_id = $db->Data(); ? It was my thinking that that statement was what defined the variable.  Obviously I am wrong, but how?
0
 
LVL 84

Assisted Solution

by:ozo
ozo earned 50 total points
ID: 33638907
Did FetchRow find anything? you did not check whether it succeeded.
I also do not see a query being made


FetchRow ( [ ROW [, TYPE ] ] )

    Retrieves the next record from the keyset. When ROW and/or TYPE are specified, the call is made using SQLExtendedFetch instead of SQLFetch.

    NOTE 1: If you are unaware of SQLExtendedFetch and its implications, stay with just regular FetchRow with no parameters.

    NOTE 2: The ODBC API explicitly warns against mixing calls to SQLFetch and SQLExtendedFetch; use one or the other but not both.

    If ROW is specified, it moves the keyset RELATIVE ROW number of rows.

    If ROW is specified and TYPE is not, then the type used is RELATIVE.

    Returns true when another record is available to read, and false when there are no more records
0
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 
LVL 5

Assisted Solution

by:fredmc
fredmc earned 50 total points
ID: 33647317
What ozo is implying, but I don't see her explicitly saying, is that if $db->Data(); returns no value, then $last_id will be unitialized. You might try this (including all that ozo suggested)
my $last_id = '';  
to give it an inital value of an empty string. Then,
 $last_id = $db->Data() || '';
which will give $last_id a value of an empty string if $db->Data() returns nothing. An empty string isn't the same as "nothing" or undefined.
0
 
LVL 39

Assisted Solution

by:Adam314
Adam314 earned 25 total points
ID: 33650948
I think what ozo was saying is that:
    $db-> FetchRow;
occurs before any query is define.  So there is no row to fetch.  
0
 
LVL 5

Assisted Solution

by:fredmc
fredmc earned 50 total points
ID: 33651054
Correct. Which is why I said "including all that ozo suggested"
:-)
0
 

Author Comment

by:g_currier
ID: 33653501
This is what I, in the end, came up with...it's not perfect (the sql doesn't work - it seems as if the param() function is doing it's job).

The query is written to select the cust_id value (no I didn't write it, but I understand the sql syntax int it)
The $next_id coding is working...a value is generated.

granted, I need to read more and understand better what Data() , param() do and what fetchrow does.

I thought I understood SQL, however.  the insert statement si something that perl doesn't like (at least, i think, the way I wrote it...)
#!c:\perl\bin\perl.exe -w

use strict;
use CGI ':standard';
use Win32::ODBC; #use ODBC package

### Connect to a data source
my $db = new Win32::ODBC("DSN=rreAccesscm485a5") || die Win32::ODBC::Error();

### Perform INSERT of new customer

#Retrieve values for new customer from HTML form



#Getting a value for the next customer ID
#Get last Customer ID used
if ($db->Sql("SELECT MAX(cust_id) as last_cust_id FROM customer")) {
print "SQL Error: " . $db->Error() . "\n";
$db->Close();
exit;
}

#Determine next Cust ID based on existing data
my $last_cust_id;
my $next_cust_id;
my $cust_id;
$db->FetchRow;
$last_cust_id = $db->Data();
$next_cust_id = $last_cust_id + 1;

my $f_name;
$f_name		= param('f_name');
# DEBUG
print "First Name: " . $f_name;
my $mi_name;
$mi_name	= param('mi_name');
my $l_name;
$l_name		= param('l_name');
my $h_phone;
$h_phone	= param('h_phone');
my $address_street;
$address_street		= param('address_street');
my $address_city;
$address_city		= param('address_city');
my $address_state;
$address_state		= param('address_state');
my $address_zipcode;
$address_zipcode	= param('address_zipcode');
my $sql;

$sql = qq{INSERT INTO customer (cust_id, f_name, mi_name, l_name, h_phone, address_street, address_city, address_state, address_zipcode)VALUES ("$next_cust_id","$f_name", "$mi_name", "$l_name", "$h_phone", "$address_street", "$address_city", "$address_state", "$address_zipcode" ) };
#$sql .= qq{};
print 'SQL string to execute=' . $sql . "\n";

#execute INSERT statement
$db->Sql($sql); 
print "<h2>Database INSERT successful.</h2>";
# Disconnect
$db->Close();
# Provide an option to show updated table contents
print '<form ACTION="cust_data.pl" METHOD="get">' .
'<input TYPE="submit" VALUE="Show All Customers">' .
'</form>'

Open in new window

0
 

Author Comment

by:g_currier
ID: 33654174
Problem solved on my own.  This problem had to do with the html <form> tag.  The closing form tag was odid not include the table into it.  Therefore, the param function:  my $action = param('addrecord') was not working.  I added that variable after I reread through my perl handbook.  crap this gets complicated if you're not paying attention!

Thanks for the tips all!
0
 

Author Closing Comment

by:g_currier
ID: 33654181
The solution was found by me, but the tips provided helped me to solve other related problems
0

Featured Post

Docker-Compose to Simplify Multi-Container Builds

Our veteran DevOps Author takes you through how to build a multi-container environment, managed with a single utility in order to simplify your deployments.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses four methods for overlaying images in a container on a web page
In threads here at EE, each comment has a unique Identifier (ID). It is easy to get the full path for an ID via the right-click context menu. However, we often want to post a short link within a thread rather than the full link. This article shows a…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

627 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question