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

Monks,

Can someone kindly instruct me on what I'm doing wrong here?
my $today = getMaxDate(); sub getMaxDate { my $sqltoday; my $SQLDT = qq(select max(convert(char(12), Modified_Date, 101)) from +Metrics); my $dbhdt=Sybase::DBlib->dblogin($user, $pw, $server); $dbhdt->dbcmd($SQLDT); $dbhdt->dbsqlexec; $dbhdt->dbresults; while (@data = $dbhdt->dbnextrow) { $sqltoday = $data[0]; } return $sqltoday; }
The value for $today is always empty!

Thanks!

Replies are listed 'Best First'.
Re: Returning a value from a sub routine
by dragonchild (Archbishop) on Nov 12, 2004 at 15:07 UTC
    Some further suggestions:
    • Don't use global variables. It would be better to create your $dbh outside the function and pass it in.
    • Add 'use strict' and 'use warnings', then fix the stuff that pops up.
    • I'd recommend shifting from Sybase::DBlib to DBI and DBD::Sybase. DBI is the standard for Perl database connections and is more portable.

    Being right, does not endow the right to be rude; politeness costs nothing.
    Being unknowing, is not the same as being stupid.
    Expressing a contrary opinion, whether to the individual or the group, is more often a sign of deeper thought than of cantankerous belligerence.
    Do not mistake your goals as the only goals; your opinion as the only opinion; your confidence as correctness. Saying you know better is not the same as explaining you know better.

Re: Returning a value from a sub routine
by Roger (Parson) on Nov 12, 2004 at 15:02 UTC
    Your code does not specify which database to use on the sybase server. Most likely is that your connection succeeded, but defaults to the master database. A quick fix is to specify the name of the database in the $SQLDT like below:

    select max(convert(char(12), Modified_Date, 101)) from databasename..Metrics

    Assumes that you are using ASE of course.

Re: Returning a value from a sub routine
by ikegami (Patriarch) on Nov 12, 2004 at 14:53 UTC
    I'd start by checking if the database query worked. Add some error checks.
Re: Returning a value from a sub routine
by gnu@perl (Pilgrim) on Nov 12, 2004 at 15:52 UTC
    Also of note, the 'return $sqltoday' line will return the first element of the final line of your '$dbhdt->dbnextrow' query. For example if the data were like this:
    Field1 Field2 Field3 bill smith 32 bob jones 43
    your return would be 'bob' If that final line had no value in the first position you would return nothing, like this:
    Field1 Field2 Field3 bill smith 32 jones 43
    this would return '' in '$sqltoday'; Note, this is just a guess since I do not know your data setup. -Chad