Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Reusing a variable declared earlier in the same scope

by reluctant_techie (Novice)
on Apr 10, 2008 at 15:14 UTC ( [id://679504]=perlquestion: print w/replies, xml ) Need Help??

reluctant_techie has asked for the wisdom of the Perl Monks concerning the following question:

Hello, I am writing an script that performs a series of database queries. I need to test each result set determine if any rows were returned. So, for example, my code might look like:
my @AssignmentIDs = (); my $GetAssignmentID_sth=$query_dbh->prepare($SQL); $GetAssignmentID_sth->execute(); my $rows = $GetAssignmentID_sth->rows; if($rows > 0) { while (my ($AssignmentID, $UserCount) = $GetAssignmentID_sth-> fet +chrow_array() ) { push(@AssignmentIDs, $AssignmentID); } } my $AssignmentIDs = join(",",@AssignmentIDs); $GetAssignmentID_sth->finish();
The next query I perform, I would like to reuse the following code:
if($rows > 0) {}
I receive the warning, however, that:
"my" variable $rows masks earlier declaration in same scope
Of course, I can use some other variable than $row but it would look cleaner if I did not have a proliferation of such variables. Is there are way I can undefine the variable after I use it once? I tried adding the following code but it did not correct the issue:
undef $row;
Thanks!

Replies are listed 'Best First'.
Re: Reusing a variable declared earlier in the same scope
by dragonchild (Archbishop) on Apr 10, 2008 at 15:28 UTC
    There are a few different solutions. The first (and most obvious) is $rows = $some_sth->rows;, but only for the second and subsequent.

    A slightly better solution would be to create a bare scope. That could look something like:

    y @AssignmentIDs = (); my $GetAssignmentID_sth=$query_dbh->prepare($SQL); $GetAssignmentID_sth->execute(); { my $rows = $GetAssignmentID_sth->rows; if($rows > 0) { while (my ($AssignmentID, $UserCount) = $GetAssignmentID_sth-> +fetchrow_array() ) { push(@AssignmentIDs, $AssignmentID); } } } my $AssignmentIDs = join(",",@AssignmentIDs); $GetAssignmentID_sth->finish();

    That makes it very clear what the scope of each thing should be. I've used that on many occasions. However, there's a better way - create a subroutine to do stuff. Now, you're thinking "But each while-loop is different!" Ok - encapsulate the differences.

    sub read_sql { my ($dbh, $sql, $callback) = @_; my $sth = $dbh->prepare( $sql ); $sth->execute(); return unless $sth->rows; while ( my @row = $sth->fetchrow_array ) { $callback->( @row ); } $sth->finish; } my @AssignmentIDs; # You don't need the empty assignment. read_sql( $dbh, $sql, sub { my ( $AssignmentID, $UserCount) = @_; push @AssignmentIDs, $AssignmentID; }); my $AssignmentIDs = join ',', @AssignmentIDs;
    Now, you have a piece of reusable code. It should be pretty easy to extend if you want to pass optional parameters in.

    My criteria for good software:
    1. Does it work?
    2. Can someone else come in, make a change, and be reasonably certain no bugs were introduced?
Re: Reusing a variable declared earlier in the same scope
by apl (Monsignor) on Apr 10, 2008 at 15:37 UTC
    I can't echo dragonchild strongly enough. Never clone code when you can create a subroutine!
Re: Reusing a variable declared earlier in the same scope
by Thilosophy (Curate) on Apr 11, 2008 at 03:44 UTC
    my $rows = $GetAssignmentID_sth->rows; if($rows > 0) { while (my ($AssignmentID, $UserCount) = $GetAssignmentID_sth-> fet +chrow_array() ) { push(@AssignmentIDs, $AssignmentID); } }

    How about completely getting rid of the whole $rows part? It seems redundant (all it does is protect a while loop, that would not be executed anyway if there is no data to loop over), and error-prone ($sth->rows is not guaranteed to be the correct number of rows until you have fetched them all).

    while (my ($AssignmentID, $UserCount) = $GetAssignmentID_sth-> fetchro +w_array() ) { push(@AssignmentIDs, $AssignmentID); }
      My purpose in testing whether or not any rows are returned is to prevent a situation where the final $AssignmentIDs variable point at an empty string. I am using this variable in my next SQL statement using the IN syntax. For example,
      SELECT ... FROM ... WHERE AssignmentID IN ($AssignmentIDs)
      This is all very helpful information though. Thanks!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://679504]
Approved by ww
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (5)
As of 2024-04-25 10:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found