in reply to Why isn't this subroutine working?
If you are running a program that compiles, runs and does something (even something wrong -- i.e. perl doesn't report a syntax error), then the code you are running is different in at least one crucial detail from the code that you posted. So you are showing us the wrong problem.
(Update: in case you didn't figure it out from your dialog with ikegami, when your IDE reports a syntax error, it's because perl reported a syntax error; the script won't run at all until you fix the syntax error.)
As for the single-quotes around $rowName when you use that as a hash key, that's just wrong. Don't do that. You can waste a little time putting double-qoutes around the variable if you really want to do that, but it will make no difference relative to using the variable with no quotes at all.
Try putting some debugging output like this:
at strategic points, and watch what comes out on STDERR. Or step through the process with "perl -d your_script_name", and use the debugger to place breakpoints and inspect variables in this troublesome subroutine.warn "this_var=$this_var; that_var=$that_var\n";
Apart from all that, if the "table's last row number" is really just the number of rows in the table (which is how I would normally interpret the phrase), then what's wrong with doing this:
Or, if "last row number" means something else, like "highest numeric index in the table's primary key field", then you could do something this:sub lastRowNum { my ( $dbh, $table ) = @_; my ( $rowcount ) = $dbh->selectrow_array( "select count(*) from $t +able" ); return $rowcount; }
No extra module or subroutines required. Keep it simple. (Updated to structure these last two examples as complete subroutines.)sub lastRowNum { my ( $dbh, $key_fld, $table ) = @_; my ( $last_id ) = $dbh->selectrow_array( "select max($key_fld) fro +m $table" ); return $last_id; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Why isn't this subroutine working?
by ikegami (Patriarch) on May 20, 2009 at 18:09 UTC |