CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Use of uninitialized value in concatenation (.) or string at.......

 



adept
New User

Jul 21, 2006, 6:42 PM

Post #1 of 8 (1504 views)
Use of uninitialized value in concatenation (.) or string at....... Can't Post

Ok everyone, first time PERL scripter here, although I have worked with many other languages. I was recently asked to help out a friend with his accounting scripts and for the life of me I cant seem to get this one damn script to compile I know this is a probably due to some miniscule little mistake on my behalf in the coding and thus have hit the net looking for assistance.

firstly I'll start off by stating, I know what that error means. but I cant see why this is not initializing my variable. The error related to the following lines of code.

my $query = "";
<ERROR HERE> $query = "SELECT chart.accno, chart.description, parts.income_accno_id
FROM invoice
WHERE invoice.trans_id = $acc_trans_id
AND parts.id = invoice.parts_id
AND chart.id = parts.income_accno_id
";

my $sth = $dbh->prepare($query);

$sth->execute;

if ($DBI::errstr) {
$dbh->disconnect;
exit 0;
}

here is the full error i receive when compiling with komodo
-Use of uninitialized value in concatenation (.) or string at line <ERROR>
-DBD::Pg::st execute failed: ERROR: syntax error at or near "AND" at character 104
-disconnect(DBI::db=HASH(0x21d2680)) invalidates 4 active statements. Either destroy statement handles or call finish on them before disconnecting.

this is only one part of my script, if anyone would care to help me out with this It would be greatly appreciated


Ill attach the whole script incase people want to compile it in its entirety
Attachments: script.pl (11.2 KB)


perl
stranger

Jul 21, 2006, 7:14 PM

Post #2 of 8 (1499 views)
Post deleted by perl [In reply to]

 


adept
New User

Jul 21, 2006, 7:28 PM

Post #3 of 8 (1494 views)
Re: [perl] Use of uninitialized value in concatenation (.) or string at....... [In reply to] Can't Post

Hi thanks for taking a look, i tried what you coded, and i still receive the following errors

Use of uninitialized value in concatenation (.) or string at C:\Documents and Settings\Administrator\Desktop\keith\keith\script.pl line 282.
DBD::Pg::st execute failed: ERROR: syntax error at or near "AND" at character 101
ERROR: syntax error at or near "AND" at character 101
Database handle destroyed without explicit disconnect.



Line 282 being....

my $query = "SELECT chart.accno, chart.description, parts.income_accno_id FROM invoice WHERE invoice.trans_id = $acc_trans_id AND parts.id = invoice.parts_id AND chart.id = parts.income_accno_id";


any other ideas? as Im not really familiar with perl id rather just get this working rather than having to recode it again with the DBix class.


perl
stranger

Jul 21, 2006, 7:41 PM

Post #4 of 8 (1489 views)
Re: [adept] Use of uninitialized value in concatenation (.) or string at....... [In reply to] Can't Post

Yes, your using DBD::Pg and you havent loaded your module for that. And you query is mysql, so replace your connect strings with this.


Code
    my $dbh = DBI->connect("DBI:mysql:$dbname:localhost", $dbuser, "5432", { RaiseError => 1 }) or die $DBI::errstr;



adept
New User

Jul 21, 2006, 8:22 PM

Post #5 of 8 (1482 views)
Re: [perl] Use of uninitialized value in concatenation (.) or string at....... [In reply to] Can't Post

I updated my connect string with the raise error parameter and still no luck getting it to compile with no errors.

my $dbh = DBI->connect($dbconnect, $dbuser, "5432", { RaiseError => 1 }) or die $DBI::errstr;

? any other ideas (and I know Ima n00b at perl so if Im being stupid and this should, its because in regards to PERL. I am.. stupid)

Ive uploaded the current version of the script im working on for anyone who cares to, to look at and attempt to compile
Attachments: scriptb1.zip (3.08 KB)


perl
stranger

Jul 21, 2006, 9:23 PM

Post #6 of 8 (1478 views)
Re: [adept] Use of uninitialized value in concatenation (.) or string at....... [In reply to] Can't Post

you can contact me on aim [at] 'thadiaryofjane'


davorg
Thaumaturge / Moderator

Jul 24, 2006, 9:05 AM

Post #7 of 8 (1453 views)
Re: [adept] Use of uninitialized value in concatenation (.) or string at....... [In reply to] Can't Post

The error strongly implies that $acc_trans_id is undef when that line of code is executed. $acc_trans_id is (supposedly) given a value earlier in the program. It's the third column retrieved from the database in the line


Code
while (my @result_acc_trans_pass = $sth->fetchrow_array) {


However, if I look back through the code, it looks to me as though the statement handle $sth is set up with this code


Code
my $query = "SELECT customer.name 
FROM customer
WHERE $ar_customer_id = customer.id
";

# Checks query:
my $sth = $dbh->prepare($query);
$sth->execute;


And, as you can see, that code only retrieves one column from the database. So your array @result_acc_trans_pass will only have one column in it and $result_acc_trans_pass[2] will therefore always be undefined. So you'll always get that error.

Oh, in fact it's worst than that. The statement handle $sth has already had all of its data fetched earlier in the program, so the call to fetchrow_array that I show above will never return any data and the while loop never gets executed. So @result_acc_trans_pass never gets any data and the assignment to $acc_trans_id will never take place.

I suspect that the problem is that you are trying to read data from the wrong statement handle. Looking a bit closer and guessing I bit, I suspect that you might want to be reading data from $sth1 instead of $sth.

There's a lesson to be learned here about giving variables clearer names :-)

--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


davorg
Thaumaturge / Moderator

Jul 24, 2006, 9:12 AM

Post #8 of 8 (1451 views)
Re: [perl] Use of uninitialized value in concatenation (.) or string at....... [In reply to] Can't Post


In Reply To
Yes, your using DBD::Pg and you havent loaded your module for that. And you query is mysql, so replace your connect strings with this.


Code
my $dbh = DBI->connect("DBI:mysql:$dbname:localhost", 
$dbuser, "5432",
{ RaiseError => 1 })
or die $DBI::errstr;



Two points.

1/ You don't need to load DBD modules explicitly. DBI.pm will look at the connection string and load the correct one.

2/ How do you know that the "query is mysql". It looks to me as though it's just standard SQL and could be run against MySQL, PostgreSQL, Oracle, Access or pretty much any other relational database.

If you know that you are guessing at solutions, then please be polite enough to mention that fact. Nothing that you have said addresses the actual error (the fact that a variable being used was undefined) and most of what you have said is inaccurate. All you have done is wasted the original poster's time. Please be more careful in the future.

--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives