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

I'm trying to eliminate an error I get for using an undefined array. Basically if the array comes up with know value it executes the sub clock_in_form. Any suggestions?
$main::SQL="SELECT oid,* FROM timeclock WHERE end_stamp = NULL AND use +rname = '$username'"; common::sql::Do_SQL(); @clocked_in = $common::sql::sth->fetchrow(); #unless ($clocked_in[1] eq "$username"){ if (@clocked_in == undef){ clock_in_form($username, $date, $time);

Replies are listed 'Best First'.
Re: undefined array use
by japhy (Canon) on May 03, 2002 at 21:48 UTC
    Never test for equality with undef, ever. You want to do:
    if (@clocked_in == 0) { ... } # empty # or unless (@clocked_in) { ... } # empty

    _____________________________________________________
    Jeff[japhy]Pinyan: Perl, regex, and perl hacker, who'd like a (from-home) job
    s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;

Re: undefined array use
by sparkyichi (Deacon) on May 03, 2002 at 21:49 UTC
    You should set the array for a default value and check for that value.
    $main::SQL=" SELECT oid,* FROM timeclock WHERE end_stamp = NULL AND use rname = '$username'"; common::sql::Do_SQL(); @clocked_in = $common::sql::sth->fetchrow(); if (! @clocked_in ){$checked = 0} #unless ($clocked_in[1] eq "$username"){ if ($checked == 0){ clock_in_form($username, $date, $time); }
    or something like that.

    Sparky
    FMTEYEWTK
Re: undefined array use
by Fastolfe (Vicar) on May 04, 2002 at 15:49 UTC
    A side note: Since you're not making use of bind variables and are putting a variable directly into your SQL, be sure that you've sanitized $username. Don't let me put a single-quote in there and let me add arbitrary SQL to make your script do whatever I want. If you're basing this on DBI, a better approach might be something similar to this:
    $sth = $dbh->prepare('select oid,* from timeclock where end_stamp is n +ull and username=?') or die "prepare: $DBI::errstr"; # for each username { $sth->execute($username) or die "execute: $DBI::errstr"; # now do whatever with $sth->fetchrow # }
Re: undefined array use
by nlafferty (Scribe) on May 03, 2002 at 22:04 UTC
    Thanks :)
      That's graditude I tells ya. A -1 for a "thanks :)". Oh what a world we live in.