in reply to Variable scope & subroutines

The two code examples you have posted should be functionally equivalent.

Yep. So that leaves one possibility that there might be something implemented in either your code (as mentioned) or deep in the DBI code that is causing your problem. However, by looking at your prodigious use of DBI->connect I have the ask: why don't you just create a single database handle and simple reuse it? Last time I checked the database handle does not dictate read/write/update/delete access to the database.

Celebrate Intellectual Diversity

Replies are listed 'Best First'.
Re^2: Variable scope & subroutines
by ikegami (Patriarch) on Apr 12, 2011 at 15:27 UTC

    Last time I checked the database handle does not dictate read/write/update/delete access to the database.

    The handle results from connecting as a user, so it definitely does. That said, I don't see how this relates.

      The code shown has a different function for different operations like update, insert, etc. Each function creates another database handle inside of the function rather than accepting the handle as an argument.

      Celebrate Intellectual Diversity

Re^2: Variable scope & subroutines
by Nyyr (Initiate) on Apr 14, 2011 at 13:44 UTC

    Thank you, I finally found the culprit, it is really DBI (mysql).

    This workaround made my program working:

    The beginning of my subroutine:

    sub write_status($$$$) { my $host = $_[0]; my $app = $_[1]; my $status = $_[2]; my $version = $_[3]; my $result = $dbh->begin_work(); if (! defined $result) { $result = -1; } if ($result < 0) { print STDERR "\n" . $0 . ": CHYBA: " . $dbh->errstr . "\n"; return ISHN_ERR_INTERNAL; }
    I needed to add this dummy UPDATE to somehow reset something (without it only few or none rows were updated):
    my $statement = q{UPDATE results SET status = ?, verze = 'N/A' WHERE host = ? AND app = ?;}; $result = $dbh->do($statement, undef, $status, $host, $app); if (! defined $result) { $result = -1; } if ($result <= 0) { print STDERR "\n" . $0 . ": CHYBA: " . $dbh->errstr . "\n"; $dbh->rollback(); return ISHN_ERR_INTERNAL; }
    This is the rest of my subroutine:
    $statement = q{UPDATE results SET status = ?, verze = ? WHERE host = ? AND app = ?;}; $result = $dbh->do($statement, undef, $status, $version, $host, $app +); if (! defined $result) { $result = -1; } if ($result <= 0) { print STDERR "\n" . $0 . ": CHYBA: " . $dbh->errstr . "\n"; $dbh->rollback(); return ISHN_ERR_INTERNAL; } $statement = q{INSERT INTO history (host, app, status, verze) VALUES (?, ?, ?, ?);}; $result = $dbh->do($statement, undef, $host, $app, $status, $version +); if (! defined $result) { $result = -1; } if ($result <= 0) { print STDERR "\n" . $0 . ": CHYBA: " . $dbh->errstr . "\n"; $dbh->rollback(); return ISHN_ERR_INTERNAL; } $result = $dbh->commit(); if (! defined $result) { $result = -1; } if ($result < 0) { print STDERR "\n" . $0 . ": CHYBA: " . $dbh->errstr . "\n"; $dbh->rollback(); return ISHN_ERR_INTERNAL; } else { return ISHN_OK; } }