northwestdev has asked for the wisdom of the Perl Monks concerning the following question:
I have the following subroutine:
#Returns a table's last row number
#input: table name; rownumber
#return: last row number
#side effects: none
sub lastRowNum {
my $tableRef = shift ( @_ );
my $rowName = shift ( @_ );
my $qryAns="";
$qry = "SELECT $tableRef.$rowName FROM $tableRef ORDER by $tableRe
+f.$rowName DESC LIMIT 1 "; #MySQL certified
(my $Ok, $qryAns)= dbTools::MySQLqry( $qry, "get last row number f
+rom $tableRef" );
if( !$Ok ) {
print STDOUT $qryAns. "Failed to get last $rowName from table
+". $tableRef. "<br>\n";
return 0;
} elsf {
my $rowRef = $qryAns->fetchrow_hashref;
return $$rowRef{'$rowName'};
}
}
I manually verified that the MySQL call works, and the dbTools::MySQLqry works perfectly for at least a dozen other subroutines. The problem is that I am getting a null return. I can't tell what the problem is, I am new to perl though.
PS: the O'Reilly Perl book mentions an elsif operator, but when I try it, I get a syntax error (using Eclipse as an IDE), and only elsf works.
Re: Why isn't this subroutine working?
by JavaFan (Canon) on May 20, 2009 at 12:04 UTC
|
$$rowRef{'$rowName'}
Are you sure fetchrow_hashref returns a hash with the name $rowName? Perhaps you want to lose the quotes.
| [reply] [d/l] [select] |
|
Yes, I'm sure. I tried it with, and without the quotes. As for elsf and elsif, I know I've been up most of the night, but I am not hallucinating, eclipse tells me I have a syntax error when I use elsif. Could it be one of the modules I'm using? (I'm using strict, HTTP::Date, XML::DOM, CGI).
PS: I am using eclipse on Windows Vista, but the programs run on a Linux server. I have not figured out, yet, how to run and debug perl on the Eclipse IDE on Vista
| [reply] |
|
use strict;
my $rowRef = {
foo => 'foo value',
bar => 'bar value',
};
my $rowName = 'bar';
for my $rn ($rowName, '$rowName') {
my $rowValue;
if (exists $rowRef->{$rn}) {
$rowValue = $rowRef->{$rn}
} else {
$rowValue = '<does not exist>';
};
print "rowRef for '$rn' is '$rowValue'\n";
};
| [reply] [d/l] |
|
|
|
>perl -e"while ($x) { if ($y) { } } elsif ($z) { } }"
syntax error at -e line 1, near "} elsif"
Execution of -e aborted due to compilation errors.
>perl -e"while ($x) { if ($y) { } elsif ($z) { } }"
>
Replacing elsif with random words is not a suitable bug fix. | [reply] [d/l] [select] |
A reply falls below the community's threshold of quality. You may see it by logging in.
|
Re: Why isn't this subroutine working?
by graff (Chancellor) on May 20, 2009 at 17:12 UTC
|
Okay, you posted code that has an obvious syntax error (elsf) and a fairly clear case where you misuse single-quotes ('$rowName'), you ask why it doesn't work, we mention these things, and you say "no, that's not the problem."
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:
warn "this_var=$this_var; that_var=$that_var\n";
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.
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:
sub lastRowNum
{
my ( $dbh, $table ) = @_;
my ( $rowcount ) = $dbh->selectrow_array( "select count(*) from $t
+able" );
return $rowcount;
}
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, $key_fld, $table ) = @_;
my ( $last_id ) = $dbh->selectrow_array( "select max($key_fld) fro
+m $table" );
return $last_id;
}
No extra module or subroutines required. Keep it simple. (Updated to structure these last two examples as complete subroutines.) | [reply] [d/l] [select] |
|
| [reply] |
Re: Why isn't this subroutine working?
by eric256 (Parson) on May 20, 2009 at 17:57 UTC
|
if (!$Ok) {
print "x";
} elsif ($some_other <> $condition) {
print "y";
}
Not including the condition does indeed make the elsif into a syntax error, and elsf does indeed make it run, but it doesn't make it work.
Oh and please please please don't embed variables in SQL unless you properly untaint them first, and even then only do it if someone is holding a gun to your head ;)
| [reply] [d/l] [select] |
Re: Why isn't this subroutine working?
by dHarry (Abbot) on May 20, 2009 at 11:44 UTC
|
Is dbTools::MySQLqry a copy/paste error?
PS: the O'Reilly Perl book mentions an elsif operator, but when I try it, I get a syntax error (using Eclipse as an IDE), and only elsf works.
I use Eclipse myself and the elsif works fine;) Besides that, Perl would complain about elsf since it's not Perl?!
| [reply] [d/l] |
|
No, it's not a copy/paste error. It's a module I wrote as a MySQL helper. It works just fine. Also, if I take the subroutine code, and place it directly in a module, it works fine. It's only a problem when I call it as a subroutine.
| [reply] |
Re: Why isn't this subroutine working?
by Anonymous Monk on May 20, 2009 at 11:37 UTC
|
Impossible to say, "elsf" is not perl | [reply] |
|
|