Its behavior is weird, some db rows get updated, some not. But if I change the $dbh variable names, so that they are different, it works perfectly:use strict; use warnings; ... sub write_status($$$$) { my $dbh = DBI->connect("DBI:mysql:mysql_socket=" ..... ); ... my $result = $dbh->begin_work(); ... $result = $dbh->do("UPDATE results ...."); ... $result = $dbh->commit(); } sub check_apps($\@) { my $dbh = DBI->connect("DBI:mysql:mysql_socket=" ..... ); ... my $sth = $dbh->prepare("SELECT * FROM ...."); ... if (! defined $sth->execute()) { ... } ... write_status($host, $app, $status, $version); } my $dbh = DBI->connect("DBI:mysql:mysql_socket=" ..... ); ... my $sth = $dbh->prepare("SELECT * FROM ...."); ... ... check_apps($host, @processes);
I thought that when I declare a variable in a subroutine with "my", any references to that variable within the subroutine point to the "local" one and all other variables with the same name defined elsewhere are invisible within that subroutine and thus stay untouched by the subroutine. Anyone capable of explaining it to me? :-)use strict; use warnings; ... sub write_status($$$$) { my $dbh_write = DBI->connect("DBI:mysql:mysql_socket=" ..... ); ... my $result = $dbh_write->begin_work(); ... $result = $dbh_write->do("UPDATE results ...."); ... $result = $dbh_write->commit(); } sub check_apps($\@) { my $dbh_apps = DBI->connect("DBI:mysql:mysql_socket=" ..... ); ... my $sth = $dbh_apps->prepare("SELECT * FROM ...."); ... if (! defined $sth->execute()) { ... } ... write_status($host, $app, $status, $version); } my $dbh_main = DBI->connect("DBI:mysql:mysql_socket=" ..... ); ... my $sth = $dbh_main->prepare("SELECT * FROM ...."); ... ... check_apps($host, @processes);
In reply to Variable scope & subroutines by Nyyr
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |