CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Unknown warning when using -w

 



mmcw
User

Jul 11, 2003, 1:52 AM

Post #1 of 18 (3416 views)
Unknown warning when using -w Can't Post

I am using the follwoing perl code.
It does work but when I add the -w command to the top command of the script
the following alert is added to my error log file:


Quote
"Use of uninitialized value in hash element at line 297"



The line with the code "$record{$fields[$_]} = $row[$_];" is meant!

What is wrong or hwo to make it even better?



Code
   

#!/usr/local/bin/perl -w

# some code!!
# some code!!
# some code!!

my $r_data = readdata($r_in->{'currency_database'});
my $fields = shift @{$r_data};
chomp($fields);
my @fields = split(/\|/,$fields);

my $j;
foreach $j ($[ . $#{$r_data}) {
chomp($r_data->[$j]);
my @row = split (/\|/,$r_data->[$j]);
my %record;
foreach ($[ . $#row) {
$record{$fields[$_]} = $row[$_];
}
# some extra code!!
# some extra code!!
# some extra code!!
}

##################################

# subroutine readdata #
# #
##################################

sub readdata {

my ($datafile) = shift;
my $r_data = [];

use strict;

sysopen(DATA, $datafile, O_RDONLY) or die "Error in subroutine readdata: Can't open $datafile: $!";
flock(DATA, LOCK_SH) or die "Error in subroutine readdata: Can't read-lock $datafile: $!";

@{$r_data} = <DATA>;

close(DATA);

return $r_data;
}



davorg
Thaumaturge / Moderator

Jul 11, 2003, 2:16 AM

Post #2 of 18 (3414 views)
Re: [mmcw] Unknown warning when using -w [In reply to] Can't Post

When you get an error that you don't understand, it's always a good idea to add "use diagnostics" to your program so you get a more detailed error message.

In your case it will say something like this:

Use of uninitialized value in hash element at ./foo.pl line xx (#1)

(W uninitialized) An undefined value was used as if it were already defined. It was interpreted as a "" or a 0, but maybe it was a mistake. To suppress this warning assign a defined value to your variables.

To help you figure out what was undefined, perl tells you what operation you used the undefined value in. Note, however, that perl optimizes your program and the operation displayed in the warning may not necessarily appear literally in your program. For example, "that $foo" is usually optimized into "that " . $foo, and the warning will refer to the concatenation (.) operator, even though there is no . in your program.


So what it means is that the value $fields[$_] is returning "undef" which is being translated to "" when used as a hash key. This probably means that the @fields array doesn't have the data in it that you think there is.

--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


mmcw
User

Jul 11, 2003, 4:13 AM

Post #3 of 18 (3411 views)
Re: [davorg] Unknown warning when using -w [In reply to] Can't Post

Thanks there was an error in the flat database! I updated the script to check if there is really a field value!

I also get this warnings:

[Fri Jul 11 04:04:53 2003] shop_art.cgi: Use of uninitialized value in concatenation (.) or string at /home/cgi-bin/Shop/Lib/init.pl line 49.
[Fri Jul 11 04:04:53 2003] shop_art.cgi: Use of uninitialized value in concatenation (.) or string at /home/cgi-bin/Shop/Lib/init.pl line 50.

I looked the perl documentation but do not understand what it means. (Sorry my englsh is very well)!

It is revering to the following code part:

sub init_main {

Code


Code
   

sub init_main {

my $r_in = shift;

my $output;

# Set right to main dir
chmod(0755,$cgidir) unless ($ENV{'SERVER_SOFTWARE'} =~ /IIS/);

# Set path array
my @path = (
"auth_data",
"datadir",
"cart_dir",
"order_dir",
"orderold_dir",
"datadiv",
"databackup"
);

# Get/ set/ make other settings
unless (-e $r_in->{'settings'} &! -e "$cgidir/run.setup") {
$output .= check_settings_datafile($r_in,\@path);
}
else {
# Read & set new settings
my $r_setup = setup_variables($r_in->{'settings'});

# create and build extra data files
$output = check_dirs(\@path,$r_setup);

# Check/ correction for old version
$output .= check_cat_datafile(); # line 49
$output .= check_basis_datafile($r_setup); # line 50
}

return $output;
}




How to fix this warning?


(This post was edited by mmcw on Jul 11, 2003, 4:14 AM)


davorg
Thaumaturge / Moderator

Jul 11, 2003, 4:22 AM

Post #4 of 18 (3407 views)
Re: [mmcw] Unknown warning when using -w [In reply to] Can't Post

Er... this error is almost identical to the previous one, so my explaination would be almost exactly the same.

The concaternation operator is the dot (.). Perl will warn you if either of the expressions on either site of the concaternation operator is undef.

--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


mmcw
User

Jul 11, 2003, 4:26 AM

Post #5 of 18 (3406 views)
Re: [davorg] Unknown warning when using -w [In reply to] Can't Post

But I did define $output using "my $output;"


(This post was edited by mmcw on Jul 11, 2003, 4:36 AM)


mmcw
User

Jul 11, 2003, 4:53 AM

Post #6 of 18 (3400 views)
Re: [davorg] Unknown warning when using -w [In reply to] Can't Post

This is one of the subs:


Code
   

$output .= check_cat_datafile();

####################
#

# subroutine check_cat_datafile
####################



sub check_cat_datafile {

my $text;
my $cat_file = "$cgidir/cat.setup";

unless (-e $cat_file) {
# Make cat.setup file if not exist
$text = "<li>Start making $cat_file ... ";

my ($output,@output);
$output = "\%super_cat = (\n";
$output .= ");\n\n";
$output .= "\%super_cat_desc = (\n";
$output .= ");\n\n";
$output .= "1;\n";
# Make cat.setup file if not exist end

push(@output,$output);
writedata($cat_file,\@output);

$text .= "$cat_file made!<br>";
}

return $text;
}



When I change:

my $text;

to:

my $text = "";

the warning is gone? Am I doing that the right way?

(Am trying to learn perl better and better but need help!)


davorg
Thaumaturge / Moderator

Jul 11, 2003, 4:53 AM

Post #7 of 18 (3400 views)
Re: [mmcw] Unknown warning when using -w [In reply to] Can't Post

No. You "declared" $output using "my". When I said it was undef, I meant that it contained the special value "undef", which is what Perl variables hold if you haven't given them any other value.

So, one of the expressions on either side of your concaternation operators is evaluating to "undef.

--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


mmcw
User

Jul 11, 2003, 5:03 AM

Post #8 of 18 (3399 views)
Re: [davorg] Unknown warning when using -w [In reply to] Can't Post

By changing

my $text;

to

my $text = "";

the warning will be gone! If that isn't OK how to do it the right way!

I think I am programming basically the wrong way thats why I like to know how to fix this so I can change it on the other places where I do have simalar warnings!


davorg
Thaumaturge / Moderator

Jul 11, 2003, 5:50 AM

Post #9 of 18 (3393 views)
Re: [mmcw] Unknown warning when using -w [In reply to] Can't Post

You're only looking at half of the problem. Let's look at the problem logically. Here's a quick lesson in debugging code.

You have the following code:

Code
my $output; 

# stuff missing that doesn't effect $output

$output = check_dirs(\@path,$r_setup);

$output .= check_cat_datafile(); # "undef" error here.


So, when the line that gives the error is executed, either $output is "undef" or check_cat_datafile() returns "undef". you need to investigate both of these possibilities.

The only way that $output could be "undef" is if check_dir(\@path, $r_setup) returns "undef". You need to see what circumstances would allow that subroutine to return "undef" and see how you can prevent that. It might be enough to check the value returned like this:


Code
$output = check_dirs(\@path,$r_setup);  
die 'check_dirs returned undef' unless defined $output;


The other option is that check_cat_datafile() returns "undef". You've shown use the code for that subroutine so we can see what might be the problem. The code basically looks like this:


Code
sub check_cat_datafile {   

my $text;
my $cat_file = "$cgidir/cat.setup";

unless (-e $cat_file) {
# give $text a value
}

return $text;
}


So can this subroutine return "undef"? Yes, of course it can. If the cat.setup file is not in the right place, then $text will never be given a value and the subroutine will return "undef". You need to work out how you're going to deal with that. Should your program die if the setup file isn't found? Or should $text be set to the empty string?

Basically, your code is making far too many assumptions about things going right and it's not handling exceptions well. You either need to tighten up your programming style or pay a programmer to do the job for you.

--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


mmcw
User

Jul 11, 2003, 7:05 AM

Post #10 of 18 (3389 views)
Re: [davorg] Unknown warning when using -w [In reply to] Can't Post

Thank you for your answer!

But if I ask a programmer I still do not know myself how to do it the right way!

I am trying and learning to do it always better!

In my error log there is also the following error:


Quote


Use of uninitialized value in index at (eval 14) line 5






Code
   

eval {
use CGI qw(:cgi-lib);
use CGI::Carp qw(fatalsToBrowser set_message);
require "$path/shop.setup";
require "$datalib/lib.pl";
# require "$datalib/check_modules.pl";
require "$datalib/check_images.pl";
require "$datalib/language.pl";
require "$datalib/translate_date_time.pl";
require "$datalib/protect.pl";
require "$datalib/item_display_menu.pl";
require "$datalib/css.pl";
};

if ($@ && $debug) {
print "Content-type: text/html\r\n\r\n";
print "ERROR: $@\n";

exit(0);
}



(This post was edited by mmcw on Jul 11, 2003, 7:20 AM)


davorg
Thaumaturge / Moderator

Jul 11, 2003, 7:10 AM

Post #11 of 18 (3385 views)
Re: [mmcw] Unknown warning when using -w [In reply to] Can't Post


In Reply To

Code
Use of uninitialized value in index at (eval 14) line 5



And after the discussions we've had today, I hope you know exactly what to do to track the problem down and fix it.

--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


mmcw
User

Jul 11, 2003, 7:23 AM

Post #12 of 18 (3384 views)
Re: [davorg] Unknown warning when using -w [In reply to] Can't Post

No I do not because it isn't reffering me to the place where to find something about it!

I hope you understand that I am trying to learn! This forum is called Perl Programming Help: Beginner:.

Thats why I am asking the experts!


davorg
Thaumaturge / Moderator

Jul 11, 2003, 7:47 AM

Post #13 of 18 (3383 views)
Re: [mmcw] Unknown warning when using -w [In reply to] Can't Post

Maybe a good place to start would be to look for all places where you call the "index" function.

--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


mmcw
User

Jul 12, 2003, 7:47 AM

Post #14 of 18 (3377 views)
Re: [davorg] Unknown warning when using -w [In reply to] Can't Post

I did look for that place but couldn|t find it!

This is the actual message//Warning I get:

[Sat Jul 12 07:42:32 2003] shop.cgi: Use of uninitialized value in index at (eval 13) line 5.


But on the 5 line there is the following code:

#!/usr/local/bin/perl -w
# 3.30.00
#
# Script Version 3.30 multilingual (Februari 2003)
# An perl script
# Copyright 2000-2003 MMCW All Rights Reserved

I do not understand this message


Jasmine
Administrator / Moderator

Jul 12, 2003, 9:15 PM

Post #15 of 18 (3368 views)
Re: [mmcw] Unknown warning when using -w [In reply to] Can't Post

Please re-read all of the replies you've received. If you follow Dave's detailed suggestions, you will determine the cause of the warning.

Aside from the generous amount of patient and informative replies that Dave has provided, there's nothing else that anyone here can do to troubleshoot your issue.

All of the information you need is right there in his replies if you choose to carefully read and understand it.


mmcw
User

Jul 13, 2003, 1:30 AM

Post #16 of 18 (3366 views)
Re: [Jasmine] Unknown warning when using -w [In reply to] Can't Post

The information was OK but not enough!

At google I found the information I needed. Simple add the following lines and It will give zou the information needed for my last question:

use Carp ();
local $SIG{__WARN__} = \&Carp::cluck;

I didn't use something like index! The warning code was somewhere else and yes it was a undef hash.

But thank you anyway!


mmcw
User

Jul 14, 2003, 10:53 AM

Post #17 of 18 (3361 views)
Re: [davorg] Unknown warning when using -w [In reply to] Can't Post

Will you help me once more with an other sort of warning?


Code
   

if ($c3) {
$code .= qq~
// Number format
function ValidNumber(item, len) {
var anum=/(^\d+\$)|(^\d+\.\d+\$)/;
if (!anum.len(item)) return false;
if (!ValidLength(item, len)) return false;
return true;
}~;
}



The line: $code .= qq~ is line 58!



Would be very helpfull if you could tell me what the warning means?

The following code part will give the following warning:


Code
   

Unrecognized escape \d passed through at /home/cgi-bin/Shop/Lib/js_check.pl line 58.



mmcw
User

Jul 14, 2003, 11:06 AM

Post #18 of 18 (3360 views)
Re: [mmcw] Unknown warning when using -w [In reply to] Can't Post

I think I know what was wrong with that code (I am learning):

Changed it to:


Code
   if ($c3) { 
$code .= qq~
// Number format
function ValidNumber(item, len) {
var anum=/(^\\d+\\\$)|(^\\d+\\.\\d+\\\$)/;
if (!anum.len(item)) return false;
if (!ValidLength(item, len)) return false;
return true;
}~;
}



and the warning was gone! Am I doing that the right way?

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives