Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Use of uninitialized value at?

by vivekvp (Acolyte)
on Oct 08, 2002 at 19:09 UTC ( [id://203724]=perlquestion: print w/replies, xml ) Need Help??

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

Hello

I am using the DBI to grab some data from a database. Some of the database table fields are NULL. I am receiving this error

Use of uninitialized value at /test/getform.cgi line 47


Here is the code
43 $sth=$dbh->prepare("select * from names where id=?"); 44 $sth->execute($poreq); 45 my @ar = $sth->fetchrow_array(); 46 foreach $ar (@ar) { 47 print "$ar \n"; 48 } 49 $sth->finish;
How do I prevent the error without dropping the perl -w option? Or do I need to put in an code to check the value of $ar in the foreach loop?


Many thank yous...

He who laughs last, doesn't get the joke.

Replies are listed 'Best First'.
Re: Use of uninitialized value at?
by chromatic (Archbishop) on Oct 08, 2002 at 19:15 UTC

    In versions 5.6 and later, you can use no warnings 'uninitialized' to suppress that error. In prior versions, you can localize a warnings handler ($SIG{__WARN__}) to suppress that error. Otherwise, you have several options.

    • Use a map block to set each element in the array to a null string if it's not defined.
    • Skip undefined elements in the loop.
    • Turn undefined elements into blank strings within the loop.

      And in versions before 5.6 you can use the $^W global. eg:

      { local($^W) = 0; # suppress warnings ... your code here ... }
Re: Use of uninitialized value at?
by kabel (Chaplain) on Oct 08, 2002 at 19:15 UTC
    one common idiom: defined $val or $val = ""; then $val is at least defined, and no warning appears.

    HTH
      Or, since we want to apply this rule to a list:
      $_ = '' for grep !defined, @arr;

      -Blake

Re: Use of uninitialized value at?
by dpuu (Chaplain) on Oct 08, 2002 at 20:42 UTC
    Another alternative is to "catch" the warning:
    local $SIG{__WARN__} = sub { warn @_ unless $_[0] =~ /uninitialised/; };
    --Dave
Re: Use of uninitialized value at?
by BrowserUk (Patriarch) on Oct 08, 2002 at 19:17 UTC

    Done better (and correctly) above.

      ..as well as prevent seeing any columns that are zero, the empty string, and so on. print "$ar \n" if defined $ar; is probably the way to go in this case.

      perl -pe '"I lo*`+$^X$\"$]!$/"=~m%(.*)%s;$_=$1;y^`+*^e v^#$&V"+@( NO CARRIER'

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (3)
As of 2024-12-07 13:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Which IDE have you been most impressed by?













    Results (50 votes). Check out past polls.