http://qs321.pair.com?node_id=11135935


in reply to Re: Not sure how to handle this scope (updated)
in thread Not sure how to handle this scope

Strongly agree. "Re-using" a variable name, particularly one like $self, is a recipe for obtuse code that will be very difficult for your inevitable successor to understand and debug.
  • Comment on Re^2: Not sure how to handle this scope (updated)

Replies are listed 'Best First'.
Re^3: Not sure how to handle this scope (updated)
by misterperl (Pilgrim) on Aug 18, 2021 at 18:39 UTC
    Thanks guys, I left out a lot of details- but the { } are actually a forked loop and the self that contains the mysql handle goes away for each fork. That's why I'm trying to do this- BUT I want to run sometimes forked , and sometimes not, and for the not case- I want to use the object from outside the loop.. I realize this if pretty fugly but there is a lot of motivation to do it.

    But aisde from all that the real question is why does self go undef, and not use the scope outside the braces?

      Maybe give the inner variable a different name and assign it the value from the "main" handle in the non-fork case?

      my $main_dbh = DBI->connect(...); while( 1 ) { my $dbh; if( $forked ) { $dbh = DBI->connect(...); } else { $dbh = $main_dbh; }; $dbh->selectall_arrayref($sql); }