Hello there! I'm quite confused. I've read many tutorials about variable scope but it seems I still don't understand: I have script like this:
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);
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_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);
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? :-)

In reply to Variable scope & subroutines by Nyyr

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.