By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,156 Members | 1,004 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,156 IT Pros & Developers. It's quick & easy.

Error: Can't call method "prepare" on an undefined value $dbh

P: 7
Hi All i have created the script which calls method from other script lib and while executing i get and error Can't call method "prepare" on an undefined value at /home/patriat/sourcescr/input/lib/patriat_lib.pl line 226.

Expand|Select|Wrap|Line Numbers
  1. # conect to database
  2. $dbh = DBI->connect( $ENV{ORACLE_SID}, $ENV{ORAUSER}, $ENV{ORAPASSWD}, 'Oracle');
  3.  
  4. ################################ {
  5.  
  6. # inserting into job_audit table
  7. print PLOG &now, " : Inserting into the JOB_AUDIT Table\n";
  8. &insert_job_audit($opt_p, $COMPLETE, $COHESION, $timestamp, $cohfileout, $LAST_FILE_PROCESSED);
  9.  
  10. print PLOG &now, " : inserted into the JOB_AUDIT Table \n";
  11.  
  12. $dbh->disconnect;
  13.  
another script is

Expand|Select|Wrap|Line Numbers
  1. #################################
  2. #Insert into JOB_AUDIT table
  3. #################################
  4.  
  5. sub insert_job_audit {
  6.  
  7.     local($l_opt_p, $l_COMPLETE, $l_COHESION, $l_timestamp, $l_cohfileout, $l_LAST_FILE_PROCESSED) = @_;
  8.     $SQL = "
  9. INSERT INTO JOB_AUDIT@TO_P1DBDEV ('JOB_NAME','STATUS','SENT_TO','RUN_DATE','EXTRACT_FILE_NAME','FILE_SEQ_NO') 
  10. VALUES (?,?,?,?,?,?)";
  11.  
  12.     $csr4 = $dbh->prepare($SQL);
  13.  
  14.     if ($DBI::errstr) {
  15.         &log_error($SQL,$DBI::errstr,$YES);
  16.     }
  17.  
  18.  
  19.     $csr4->execute ($l_opt_p, $l_COMPLETE, $l_COHESION, $l_timestamp, $l_cohfileout, $l_LAST_FILE_PROCESSED);
  20.  
  21.     if ($DBI::errstr) {
  22.         &log_error($SQL,$DBI::errstr,$YES);
  23.     }
  24.  
  25.     $dbh->commit;
  26.     $csr4->finish();
  27.     printf STDLOG ("values inserted in the JOB_AUDIT table\n");
  28. }
  29.  
Aug 30 '07 #1
Share this Question
Share on Google+
11 Replies


KevinADC
Expert 2.5K+
P: 4,059
thisis the only line I see with "prepre" in it:

$csr4 = $dbh->prepare($SQL);

so $dbh is undefined when you try and call the prepare method. You have to determine why it is undefined.
Aug 30 '07 #2

P: 7
i defined it but again its giving same error. but i dont think we need to define the handle
Aug 30 '07 #3

numberwhun
Expert Mod 2.5K+
P: 3,503
You definitely have to determine why that value isn't being set and I think you should set some print statements after variable declarations to ensure they are being set.

Another thing, you definitely aren't using "use strict" because you don't have 'my' in front of your initial declarations. This makes me wonder if you are using "use warnings". They both will help you to avoid the silly errors and allow people to trouble shoot the actual issues in your code, not the silly stuff.

Regards,

Jeff
Aug 30 '07 #4

KevinADC
Expert 2.5K+
P: 4,059
i defined it but again its giving same error. but i dont think we need to define the handle

Well, the error message clearly tells you that $dbh needs to be defined and needs to be defined properly. How did you 'define' it?

Note what Jeff said above too about using "strict" and warnings".
Aug 30 '07 #5

P: 7
hi,

i have use strict and defined the dbh too. can there is some other problem with perl module as i am new to perl. still getting error as

Can't call method "prepare" on an undefined value at /home/patriat/sourcescr/input/lib/patriat_lib.pl line 226.

$csr4 = $dbh->prepare($SQL);
Sep 3 '07 #6

numberwhun
Expert Mod 2.5K+
P: 3,503
Since we cannot see the whole of your code, we are only going to be able to speculate what is going on.

I wonder, do you previously in your code have the line to assign the new() object to $dbh?

ie:
Expand|Select|Wrap|Line Numbers
  1. my $dbh = new(DBI..... );
  2.  
Just curious.

Regards,

Jeff
Sep 3 '07 #7

miller
Expert 100+
P: 1,089
Expand|Select|Wrap|Line Numbers
  1. # conect to database
  2. $dbh = DBI->connect( $ENV{ORACLE_SID}, $ENV{ORAUSER}, $ENV{ORAPASSWD}, 'Oracle');
  3.  
  4. ...
  5.  
  6. &insert_job_audit($opt_p, $COMPLETE, $COHESION, $timestamp, $cohfileout, $LAST_FILE_PROCESSED);
  7.  
another script is

Expand|Select|Wrap|Line Numbers
  1. #################################
  2. #Insert into JOB_AUDIT table
  3. #################################
  4.  
  5. sub insert_job_audit {
  6. ...
  7.     $csr4 = $dbh->prepare($SQL);
  8. }
  9.  
You have defined $dbh in your primary script, but it is not defined in your secondary script in which the sub insert_job_audit is located. I suggest that you pass the $dbh as the first parameter to the function so that it is defined.

Also, do not use local for subroutine parameters. In fact, unless there is a specific reason why you need it, you should never use local. Instead use my

And I second all of the things that Jeff and Kevin stated about including "use strict;" at the top of all of your scripts. If you really had included it, then an error should have been thrown during compile time instead of at runtime when the prepare statement is reached.

- Miller
Sep 3 '07 #8

P: 7
Since we cannot see the whole of your code, we are only going to be able to speculate what is going on.

I wonder, do you previously in your code have the line to assign the new() object to $dbh?

ie:
Expand|Select|Wrap|Line Numbers
  1. my $dbh = new(DBI..... );
  2.  
Just curious.

Regards,

Jeff

nopes i dint use new()

still i am not able to resolve my problem.

now its saying as

Can't locate strict.pm in @INC (@INC contains: /opt/perl5.6.1/lib/site_perl/5.6.1 /opt/perl5.6.1/lib/5.6.1 /opt/perl5.6.1/lib/5.6.1/PA-RISC2.0 /opt/perl5.6.1/lib/site_perl/5.6.1/PA-RISC2.0 /home/patriat/sourcescr/input/lib /project/hpunix64/902/APACHE/src/pdc_perl/bin/HPUX/Opt/lib/5.00503/PA-RISC2.0 /project/hpunix64/902/APACHE/src/pdc_perl/bin/HPUX/Opt/lib/5.00503 /project/hpunix64/902/APACHE/src/pdc_perl/bin/HPUX/Opt/lib/site_perl/5.005/PA-RISC2.0 /project/hpunix64/902/APACHE/src/pdc_perl/bin/HPUX/Opt/lib/site_perl/5.005 .) at /home/patriat/sourcescr/output/src/ras2coh.pl line 21.
BEGIN failed--compilation aborted at /home/patriat/sourcescr/output/src/ras2coh.pl line 21.
Sep 6 '07 #9

numberwhun
Expert Mod 2.5K+
P: 3,503
That is just weird that you are getting that. Strict is part of the base installation of Perl. If it is not installed, then you are going to have to go to CPAN and install the strict module from there. That should clear up that error.

Regards,

Jeff
Sep 6 '07 #10

KevinADC
Expert 2.5K+
P: 4,059
lets see the first 30 or so lines of ras2coh.pl

if "strict" really is not there your version of perl is no good for reasons unkown. It is long overdue for upgrade anyway since perl 5.6 is very old.
Sep 6 '07 #11

P: 7
Expand|Select|Wrap|Line Numbers
  1. #
  2. #  Description:
  3. #
  4. #  ftp extract file to cohesion and compress the extract file.
  5. #
  6. #  This script was created to overcome OS problems encountered when
  7. #  this code was included in the ras_attr_extract.pl script. The
  8. #  error encountered was "Not enough space" when the extract files
  9. #  grew in size.
  10. #
  11. #  Change history:
  12. #
  13. #  Manu Kodiyan         04/05/04  Created
  14. #
  15. #
  16. #
  17. unshift (@INC, "/home/patriat/sourcescr/input/lib");
  18. require 'getopts.pl';
  19. require '/home/patriat/sourcescr/input/lib/patriat_lib.pl';
  20. use strict;
  21. use Getopt::Std;
  22. use vars qw($opt_p $opt_d);
  23. use File::Copy;
  24. require "comfunc.pl";
  25. use DBI;
  26. use Data::Dumper;
  27. getopts('p:d:');
  28.  
  29. my $emailmsg;
  30. my $loglines;
  31. my $raspath = "/home/patriat/data/ras/";
  32. my $cohpath = "/home/patriat/data/ras/";
  33. my $backup = "/home/patriat/backup/ras/";
  34. my $weblink = "/home/patriat/sourcescr/web_pages/html/onlinedoc/";
  35. my $total = 0;
  36. my ($timestamp) = timestamp ();
  37. my $LAST_FILE_PROCESSED= 0;
  38. my $COMPLETE = "COMPLETE";
  39. my $COHESION = "COHESION";
  40.  
  41. my ($subject, $content, $cc);
  42. $subject = "PROGRAM $opt_p";
  43. $cc = $ENV{PROD_SUPPORT};
  44. my ($dbh);
  45.  
  46.  
  47. ###################################################################################
  48. # conect to database
  49. #################################################################################
  50.  
  51. my ($subject, $content, $cc);
  52. $subject = "PROGRAM $opt_p";
  53. $cc = $ENV{PROD_SUPPORT};
  54. my ($dbh);
  55.  
  56.  
  57.  
  58.  
  59.  
  60.  
  61. if (!$opt_d || !$opt_p)
  62. {
  63.   $emailmsg = "ftp to cohesion script not configured properly\n\n" .
  64.               "USAGE ras2coh.pl -p <calling program> -d <extract file>\n";
  65.   $subject .= " -FAILURE";
  66.   sendMail ($subject, $emailmsg, $cc);
  67.   exit 1;
  68. }
  69.  
  70.  
  71. my $TMP_LOG = "/home/patriat/log/ras/log.tmp";
  72.  
  73. my $PEX_LOG = "/home/patriat/log/ras/${opt_d}";
  74. $PEX_LOG =~ s/csv/log/;
  75.  
  76. my $PEX_LOG_FTP = $PEX_LOG;
  77. $PEX_LOG_FTP =~ s/\.log/_ftp\.log/;
  78.  
  79. open ( PLOG, ">>$PEX_LOG" );
  80. print PLOG "\n";
  81. print PLOG `date`;
  82. print PLOG "Preparing to ftp ras $opt_d file to cohesion\n\n";
  83.  
  84.  
  85. my ($cohfileout) = $opt_d;
  86.  
  87. # rename extension to .dat fro Cohesion
  88. $cohfileout =~ s/csv$/dat/;
  89.  
  90.  
  91. $total = `wc -l ${raspath}$opt_d`;
  92. $total =~ /^(\d+)/;
  93. $total = $1 - 1; # -1 for the header
  94.  
  95. rename $raspath . $opt_d, $cohpath . $cohfileout;
  96.  
  97. my ($coherrstr, $cohcmd, $coherrstr);
  98.  
  99. #
  100. # ftp to the cohesion drop box
  101. #
  102. #$cohcmd = "/home/patriat/sourcescr/output/src/ftp2coh.sh $cohfileout >$PEX_LOG_FTP 2>&1";
  103. #$coherrstr = system($cohcmd);
  104.  
  105. if ($coherrstr) {
  106.   open (TMP, "<$PEX_LOG_FTP");
  107.   $loglines = '';
  108.   while (<TMP>) {
  109.     $loglines = $loglines . $_;
  110.   }
  111.   close (TMP);
  112.   # some error occured in the ftp script
  113.   $emailmsg = "Error during ftp of $cohfileout extract to cohesion. $loglines\n";
  114.   print PLOG $emailmsg;
  115.   $subject .= " -FAILURE";
  116.   sendMail ($subject, $emailmsg, $cc);
  117.   exit 1;
  118. }
  119.  
  120. print PLOG "DATA file $cohfileout (renamed extension) is Succesfully ftp-ed to Cohesion\n";
  121.  
  122.  
  123.  
  124.  
  125. ##############################################################################
  126. # inserting into job_audit table
  127. ##############################################################################
  128.  
  129. print PLOG &now, " : Inserting into the JOB_AUDIT Table\n";
  130. &insert_job_audit('$opt_p','$COMPLETE','$COHESION','$timestamp','$cohfileout','$LAST_FILE_PROCESSED');
  131.  
  132. print PLOG &now, " : inserted into the JOB_AUDIT Table \n";
  133.  
  134. ###############################################################################
  135.  
  136.  
  137. $dbh->disconnect;
  138.  
  139. # Move the file back and compress
  140. rename $cohpath . $cohfileout, $raspath . $opt_d;
  141.  
  142. unlink ( $raspath. $opt_d . ".Z" );
  143. my($command) = "compress ${raspath}$opt_d";
  144. $coherrstr = system("$command >$TMP_LOG 2>&1");
  145. if ( $coherrstr )
  146. {
  147.   open (TMP, "<$TMP_LOG");
  148.   $loglines = '';
  149.   while (<TMP>) {
  150.     $loglines = $loglines . $_;
  151.   }
  152.   close (TMP);
  153.   $emailmsg = "Error when  compressing $opt_d extract file\n. $loglines";
  154.   $subject .= " -FAILURE";
  155.   print PLOG $emailmsg;
  156.   sendMail ($subject, $emailmsg, $cc);
  157.   exit 1;
  158.  
Sep 7 '07 #12

Post your reply

Sign in to post your reply or Sign up for a free account.