Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Why isn't this subroutine working?

by northwestdev (Acolyte)
on May 20, 2009 at 11:17 UTC ( [id://765188]=perlquestion: print w/replies, xml ) Need Help??

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.

Replies are listed 'Best First'.
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.
      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

        It might have gone by you - JavaFan's question was more rhetorical, as the differences in output of the following code show you:

        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"; };

        Then you are misinterpreting the error message. For example,

        >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.

        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.)

      (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.)

      EPIC (the Eclipse module for Perl support) uses PPI to parse the program or module and identify warnings and errors before it is executed, so false positives and false negatives are possible. But it's unlikely.

Re: Why isn't this subroutine working?
by eric256 (Parson) on May 20, 2009 at 17:57 UTC

    elsif expects to get a condition as well. so your example should be

    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 ;)


    ___________
    Eric Hodges
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?!

      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.
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

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (3)
As of 2024-04-25 23:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found