cjd143SD
cjd143SD

Reputation: 869

troubleshooting perl sub routine and dbi error

Getting unable to connect error on calling my second subroutine.

I'm using 2 sub routines where the main checks on the result of sub 1, if condition is set then run sub 2. I can see values are correct to initiate sub 2 (doing print)

Both subs are pretty similar. I'm not sure where this is failing.

#subs

    sub get_flg
    {
      undef $/;
      open (my $FH, "< l.sql") or die "error can't open this file $!";
      my $sth= $dbh->prepare(<$FH>) ||
          die ("Cannot connect to the database: ".$DBI::errstr."\n");
      $sth->execute;
      close $FH;
      my $row = $sth->fetchrow_hashref;
      $sth->finish;
      return $row->{A};
}

sub get_msg
{
  undef $/;
  open (my $FH, "< 2.sql") or die "error can't open this file $!";
  my $sth= $dbh->prepare(<$FH>) ||
      die ("Cannot connect to the database: ".$DBI::errstr."\n");
  $sth->execute;
  close $FH;
  my $row = $sth->fetchrow_hashref;
  $sth->finish;
  return @$row{'B','C'}; 
}


#MAIN:

my $dbh = DBI->connect($cf{dsn},$cf{user},$cf{pw},
      {AutoCommit => 0, RaiseError => 0, PrintError => 0 }) || die ("Cannot connect to the database: ".$DBI::errstr."\n");
my $val = get_flg();
print $val;
$dbh->disconnect();

if ($val == 0 ) {
    print "$val\n"; 
    } elsif
      ($val == 1) {
    print "My val is $val\n";
    my ($val2,$val3) = get_msg();
    print "$val2:$val3\n";    
    exit;
};
$dbh->disconnect;

Upvotes: 0

Views: 282

Answers (1)

unpythonic
unpythonic

Reputation: 4070

You're disconnecting from the database after your call to the first subroutine.

my $val = get_flg();
print $val;
$dbh->disconnect();

Remove the $dbh->disconnect and you should be OK.

Upvotes: 2

Related Questions