Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

Error when trying to loop through DB and run a script

by ikkon (Monk)
on Apr 03, 2007 at 15:40 UTC ( #608089=perlquestion: print w/ replies, xml ) Need Help??
ikkon has asked for the wisdom of the Perl Monks concerning the following question:

I am pulling information from a MySQL database one row at a time, then while looping through the table i am running a script that creates a pdf, on each row based on there info. I loop through the first row and it works just fine, it gets to the second record and bails halfway through. this is the error
Uncaught exception from user code: Can't call method "new_obj" on unblessed reference at C:/Perl/site +/lib/PDF/API2/ line 66. at C:/Perl/site/lib/PDF/API2/ line 66 PDF::API2::Resource::new('PDF::API2::Resource::XObject::Image::PNG +', 'undef', 'PxCBQ') called at C:/Perl/site/lib/PDF/API2/Resource/XOb line 64 PDF::API2::Resource::XObject::new('PDF::API2::Resource::XObject::I +mage::PNG', 'undef', 'PxCBQ') called at C:/Perl/site/lib/PDF/API2/Res +ource/XObject/ line 65 PDF::API2::Resource::XObject::Image::new('PDF::API2::Resource::XOb +ject::Image::PNG', 'undef', 'PxCBQ') called at C:/Perl/site/lib/PDF/A +PI2/Resource/XObject/Image/ line 62 PDF::API2::Resource::XObject::Image::PNG::new('PDF::API2::Resource +::XObject::Image::PNG', 'undef', 'assets/VMwareTCOChart35.png') calle +d at C:/Perl/site/lib/PDF/API2/Resource/XObject/Image/ line 298 PDF::API2::Resource::XObject::Image::PNG::new_api('PDF::API2::Reso +urce::XObject::Image::PNG', 'PDF::API2=HASH(0x2eb962c)', 'assets/VMwa +reTCOChart35.png') called at C:/Perl/site/lib/PDF/ line 1816 PDF::API2::image_png('PDF::API2=HASH(0x2eb962c)', 'assets/VMwareTC +OChart35.png', '-lossless', 1) called at C:\WEB_ROOT\dbcall\VMwareTCO line 1032 main::setImage('assets/VMwareTCOChart35.png', 311.811023622047, 46 +7.716535433071, 270, 225) called at C:\WEB_ROOT\dbcall\VMwareTCODBRep line 358
I am not sure why its causing this error, any help would be great, here is the loop I am using minus the pdf code its too long.
#!/usr/bin/perl -w BEGIN { use CGI::Carp qw(carpout); open(\*MYLOG, '>>', "VMwith-db.log") or die("Unable to open perl.log: $!\n"); carpout(MYLOG); } use PDF::API2; use PDF::Table; use perlchartdir; use DBI; use MIME::Lite; use strict; use warnings; use diagnostics; use CGI qw(:standard); use constant mm => 25.4/72; use constant in => 1/72; use constant pt => 1; my %valueHash; my %User_Preferences; my $q = new CGI; print $q->header( "text/plain" ); open( CONFIG, 'variables.conf' ); while (<CONFIG>) { chomp; # no newline s/#.*//; # no comments s/^\s+//; # no leading white s/\s+$//; # no trailing white next unless length; # anything left? my ($var, $value) = split(/\s*=\s*/, $_, 2); $User_Preferences{$var} = $value; } close (CONFIG); my $dbhost = $User_Preferences{"localhost"}; my $dsn = "dbi:mysql:$User_Preferences{\"dbName\"}:$dbhost"; my $db_user_name = $User_Preferences{"db_user_name"}; my $db_password = $User_Preferences{"db_password"}; my $dbh = DBI->connect($dsn, $db_user_name, $db_password); my $selectState = qq{SELECT uid, fparams FROM vmware WHERE flag = 10}; my $sth = $dbh->prepare($selectState); $sth->execute or die "Can't execute SQL statement: $DBI::errstr\n"; ## search through the database - so we can run the pdf for every row while ((my $key, my $row) = $sth->fetchrow_array()) { my $workingFlag = qq{UPDATE vmware SET flag = 20 WHERE uid = $ +key}; $dbh->do($workingFlag) or die "Can't Update DataBase: $DBI::er +rstr\n"; my $fparams = join('', $row); %valueHash = map split(/=/, $_, 2), split /\|\|/, $fparams; #while( my($key2, $value2) = each(%valueHash)){ # print " KEY: $key2..... VALUE: $value2 \n "; #} #do PDF Stuff HERE .... }#WHILE LOOP THROUGH THE db warn"Data fetching terminated early by error: $DBI::errstr\n" if $DBI: +:err;

Replies are listed 'Best First'.
Re: Error when trying to loop through DB and run a script
by mreece (Friar) on Apr 03, 2007 at 16:48 UTC
    i suspect the problem is in the PDF code which you did not post. i recommend you trim your PDF Stuff down to something small that still produces the error, and troubleshoot that.

    specifically, it appears that your $api object has an undef $api->{pdf} value. are you properly constructing a new PDF::API2 object each time through your while loop?

      you know I probably not, But I thought each time through the pdf objects where distroyed?? I figured out where its failing, just not sure what I am doing wrong, you are probably on to something with the $pdf object, but not sure how to do it here is code where the problem lies:
      my $ChartImageName = "$tmpPath/VeTCOChart.png"; setImage($ChartImageName, 110/mm, 165/mm, 95.25/mm, 79.375/mm); sub setImage{ my $photo_file=$pdf->image_png($_[0] ,-lossless => 1); my $photo = $page->gfx; ## add photo to pdf include cordinates and size - image($photo, x, + y, width, hieght); $photo->image($photo_file, $_[1], $_[2], $_[3], $_[4]); }
      setimage call is where its failing! I open a pdf template so this is how I declare my pdf object.
      my $pdf = PDF::API2->open($Template) or print "PDF Object is failing o +r NULL"; my $page = $pdf->openpage(1);
      thanks for the help
        this code appears out of order, perhaps, so i'm not sure what is going on. my guess is that the my $pdf = ... is at file scope, so setImage() is reusing the same $pdf object every time, and it probably shouldn't. consider creating a fresh PDF::API2 object each time through your while loop, and passing it explicitely to the setImage function.
Re: Error when trying to loop through DB and run a script
by roboticus (Chancellor) on Apr 03, 2007 at 16:20 UTC
    Turn on warnings, strict and diagnostics and try again. I'll bet perl will give you a few hints.

    Update: Ignore this. They were turned on, and I missed 'em.

    Reading through the error chain, it seems that the problem might be that main::setImage is passing PDF::API2::image_png an invalid object. (The hint I'm working from is the 'undefined' argument in multiple call levels...)


      I got the error from the script, i always use warnings and diagnostics to debug, its a pain otherwise :) thanks though

        You might want to try the perl debugger. I've only used it recently (due to a recent thread about it), and find it very easy to use for problems like this. Just invoke your script like:

        perl -d and then you can set a breakpoints, step through the code, display variable values, etc.


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://608089]
Approved by Anno
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (4)
As of 2016-08-28 05:19 GMT
Find Nodes?
    Voting Booth?
    The best thing I ever won in a lottery was:

    Results (390 votes). Check out past polls.