Error uninitialized value

W

Wijnand Nijs

Hello,

I get a lot of "uninitialized value" errors in my logfile. The script is working fine but I don't like errors I don't understand. Ok, if there are no form params, I understand that $Year en $Month are empty. Is there a nice way to give $Year and $Month a value if there are no form params without an error message in the log fiale?

Thanks and regards...
Wijnand


Use of uninitialized value in string eq at G:/cgi-bin/calendar/calendar.pl line 35.

14 # get form params
15 #-----------------
16
17 $Year = param("Year");
18 $Month = param("Month");
19
20
21 # convert localtime
22 #-------------------
23
24 $HourOffset = 0;
25
26 $time = time;
27 ($local_monthday,$local_month,$local_year) = (localtime($time+($HourOffset*3600)))[3,4,5];
28 $local_month = $local_month + 1;
29 $local_year = $local_year + 1900;
30
31
32 # check form params
33 #-------------------
34
35 if ($Year eq "") {
36 $Year = $local_year;
37 };
 
A

Andrew DeFaria

body { font: Helvetica, Arial, sans-serif; } p { font: Helvetica, Arial, sans-serif; } .standout { font-family: verdana, arial, sans-serif; font-size: 12px; color: #993333; line-height: 13px; font-weight: bold; margin-bottom: 10px; } .code { border-top: 1px solid #ddd; border-left: 1px solid #ddd; border-right: 2px solid #000; border-bottom: 2px solid #000; padding: 10px; margin-top: 5px; margin-left: 5%; margin-right: 5%; background: #ffffea; color: black; -moz-border-radius: 10px; } .codedark { border-top: 10px solid #03f; border-left: 1px solid #ddd; border-right: 2px solid grey; border-bottom: 2px solid grey; padding: 10px; margin-top: 5px; margin-left: 5%; margin-right: 5%; background: black; color: yellow; -moz-border-radius: 10px; } #code { color: black; font-size: 14px; font-family: courier; padding-left: 5px; } #line-number { color: #804000; font-family: Arial; font-size: 14px; padding-right: 5px; border-right: 1px dotted #804000; } blockquote[type=cite] { padding: 0em .5em .5em .5em !important; border-right: 2px solid blue !important; border-left: 2px solid blue !important; } blockquote[type=cite] blockquote[type=cite] { border-right: 2px solid maroon !important; border-left: 2px solid maroon !important; } blockquote[type=cite] blockquote[type=cite] blockquote[type=cite] { border-right: 2px solid teal !important; border-left: 2px solid teal !important; } blockquote[type=cite] blockquote[type=cite] blockquote[type=cite] blockquote[type=cite] { border-right: 2px solid purple !important; border-left: 2px solid purple !important; } blockquote[type=cite] blockquote[type=cite] blockquote[type=cite] blockquote[type=cite] blockquote[type=cite] { border-right: 2px solid green !important; border-left: 2px solid green !important; } Wijnand Nijs wrote: Hello,

I get a lot of "uninitialized value" errors in my logfile. The script is working fine but I don't like errors I don't understand. Ok, if there are no form params, I understand that $Year en $Month are empty. Is there a nice way to give $Year and $Month a value if there are no form params without an error message in the log fiale?

Thanks and regards...
Wijnand

Use of uninitialized value in string eq at G:/cgi-bin/calendar/calendar.pl line 35.

14 # get form params
15 #-----------------
16
17 $Year  = param("Year");
18 $Month = param("Month");
19
20
21 # convert localtime
22 #-------------------
23
24 $HourOffset = 0;
25
26 $time = time;
27 ($local_monthday,$local_month,$local_year) = (localtime($time+($HourOffset*3600)))[3,4,5];
28 $local_month = $local_month + 1;
29 $local_year  = $local_year  + 1900;
30
31
32 # check form params
33 #-------------------
34 35 if ($Year eq "") {
36   $Year    = $local_year;
37 };
$Year ||= $local_year;
 
J

Jens Thoms Toerring

Wijnand Nijs said:
I get a lot of "uninitialized value" errors in my logfile. The script is working fine but I don't like errors I don't understand. Ok, if there are no form params, I understand that $Year en $Month are empty. Is there a nice way to give $Year and $Month a value if there are no form params without an error message in the log fiale?
Use of uninitialized value in string eq at G:/cgi-bin/calendar/calendar.pl line 35.
14 # get form params
15 #-----------------
16
17 $Year = param("Year");
35 if ($Year eq "") {
36 $Year = $local_year;
37 };

IIRC if "Year" hasn't been filled in in the form you seem to be
getting this then param("Year') is an undefined value, not an
empty string. So all you have to do is to apply the 'defined'
operator instead of comparing to "". Or write it all in a single
line:

$Year = param("Year") || $local_year;

This deals with the case that param("Year") is not defined as well
as that it's an empty string.
Regards, Jens
 
W

Wijnand Nijs

Jens Thoms Toerring schreef:
IIRC if "Year" hasn't been filled in in the form you seem to be
getting this then param("Year') is an undefined value, not an
empty string. So all you have to do is to apply the 'defined'
operator instead of comparing to "". Or write it all in a single
line:

$Year = param("Year") || $local_year;

This deals with the case that param("Year") is not defined as well
as that it's an empty string.
Regards, Jens

Thanks!!!

But what to do with:

Use of uninitialized value in substitution (s///) at G:/cgi-bin/calendar/calendar.pl line 109.

in:

109 $SmallTable{int($dateday)} =~ s/^<BR><HR NOSHADE WIDTH=25%>//;

???

Wijnand
 
A

Andrew DeFaria

body { font: Helvetica, Arial, sans-serif; } p { font: Helvetica, Arial, sans-serif; } .standout { font-family: verdana, arial, sans-serif; font-size: 12px; color: #993333; line-height: 13px; font-weight: bold; margin-bottom: 10px; } .code { border-top: 1px solid #ddd; border-left: 1px solid #ddd; border-right: 2px solid #000; border-bottom: 2px solid #000; padding: 10px; margin-top: 5px; margin-left: 5%; margin-right: 5%; background: #ffffea; color: black; -moz-border-radius: 10px; } .codedark { border-top: 10px solid #03f; border-left: 1px solid #ddd; border-right: 2px solid grey; border-bottom: 2px solid grey; padding: 10px; margin-top: 5px; margin-left: 5%; margin-right: 5%; background: black; color: yellow; -moz-border-radius: 10px; } #code { color: black; font-size: 14px; font-family: courier; padding-left: 5px; } #line-number { color: #804000; font-family: Arial; font-size: 14px; padding-right: 5px; border-right: 1px dotted #804000; } blockquote[type=cite] { padding: 0em .5em .5em .5em !important; border-right: 2px solid blue !important; border-left: 2px solid blue !important; } blockquote[type=cite] blockquote[type=cite] { border-right: 2px solid maroon !important; border-left: 2px solid maroon !important; } blockquote[type=cite] blockquote[type=cite] blockquote[type=cite] { border-right: 2px solid teal !important; border-left: 2px solid teal !important; } blockquote[type=cite] blockquote[type=cite] blockquote[type=cite] blockquote[type=cite] { border-right: 2px solid purple !important; border-left: 2px solid purple !important; } blockquote[type=cite] blockquote[type=cite] blockquote[type=cite] blockquote[type=cite] blockquote[type=cite] { border-right: 2px solid green !important; border-left: 2px solid green !important; } Wijnand Nijs wrote: Thanks!!!

But what to do with:

Use of uninitialized value in substitution (s///) at G:/cgi-bin/calendar/calendar.pl line 109.

in:

109  $SmallTable{int($dateday)} =~ s/^&lt;BR&gt;&lt;HR NOSHADE WIDTH=25%&gt;//;

???
Is $dateday defined? Why would you use int for a hash key? Finally is $SmallTable defined with the int of $dateday as a hash key?
 
J

Jens Thoms Toerring

Wijnand Nijs said:
But what to do with:
Use of uninitialized value in substitution (s///) at G:/cgi-bin/calendar/calendar.pl line 109.

109 $SmallTable{int($dateday)} =~ s/^<BR><HR NOSHADE WIDTH=25%>//;

Don't know if Andrew came up with something useful (my newsreader
doesn't display HTML messages). But what are '$dateday' and
'%SmallTable'? Why do you need the int() on '$dateday'? Since
you don't show any code I can't say more than Perl already did,
i.e. that either '$dateday' or '$SmallTable{int($dateday)}' is
not defined (those are the only variables on that line). Why this
is the case can only be explained when you show how you set up
'%SmallTable' and '$dateday'.
Regards, Jens
 
N

Nathan Keel

Wijnand said:
Jens Thoms Toerring schreef:

Thanks!!!

But what to do with:

Use of uninitialized value in substitution (s///) at
G:/cgi-bin/calendar/calendar.pl line 109.

in:

109 $SmallTable{int($dateday)} =~ s/^<BR><HR NOSHADE WIDTH=25%>//;

???

Wijnand

Looks like you're trying to make a script someone else wrote work, or
you'd know what and why the above is set to do whatever you think it's
supposed to do. Going by the code you've posted, I'd recommend
dropping the idea of using this calendar script and to use a better
one.
 
W

Wijnand Nijs

Nathan Keel schreef:
Looks like you're trying to make a script someone else wrote work, or
you'd know what and why the above is set to do whatever you think it's
supposed to do. Going by the code you've posted, I'd recommend
dropping the idea of using this calendar script and to use a better
one.

Hello Andrew, Jens and Nathan

Yes it is a script of someone else. I can drop this script and look for another, but I am also trying to learn something (writing Perl scripts).

I have a few files with (birthday- and other)events with a line format:

00000209|Anita Bruls-Jokhorst|/roots/lines/jokhorst/jok-aaa/jok-jeu/anita.htm

after collecting and sorting the data the script filters de events for that month to a hash:

foreach $line (@sortedevents) {

($date,$event,$URL) = split (/\|/, $line);
($dateyear,$datemonth,$dateday) = $date =~ m#(\d\d\d\d)(\d\d)(\d\d)#o;

# collect the events for $dateday
if (((int($dateyear) == int($Year)) || (int($dateyear) < 1)) && (int($datemonth) == int($Month))) {
if ($URL) {
$SmallTable{int($dateday)} = "<BR><HR NOSHADE WIDTH=25%><A HREF=\"$URL\">$event</A>";
} else {
$SmallTable{int($dateday)} = "<BR><HR NOSHADE WIDTH=25%>$event";
}
}

# drop the first HR
$SmallTable{int($dateday)} =~ s/^<BR><HR NOSHADE WIDTH=25%>//;
}

with this hash the calendar for that month is built.

Thanks for your help, regards...
Wijnand
 
J

Jim Gibson

I have a few files with (birthday- and other)events with a line format:

00000209|Anita Bruls-Jokhorst|/roots/lines/jokhorst/jok-aaa/jok-jeu/anita.htm

after collecting and sorting the data the script filters de events for that
month to a hash:

foreach $line (@sortedevents) {

($date,$event,$URL) = split (/\|/, $line);
($dateyear,$datemonth,$dateday) = $date =~ m#(\d\d\d\d)(\d\d)(\d\d)#o;

You may have a line that does not match the above reqular expression,
resulting in invalid values for $dateyear, etc. You should always check
whether the RE has been successfully matched before using the values
from the match.
# collect the events for $dateday
if (((int($dateyear) == int($Year)) || (int($dateyear) < 1)) &&
(int($datemonth) == int($Month))) {

This if statement expression may evaluate to false, ...
if ($URL) {
$SmallTable{int($dateday)} = "<BR><HR NOSHADE WIDTH=25%><A
HREF=\"$URL\">$event</A>";
} else {
$SmallTable{int($dateday)} = "<BR><HR NOSHADE WIDTH=25%>$event";
}
}

# drop the first HR
$SmallTable{int($dateday)} =~ s/^<BR><HR NOSHADE WIDTH=25%>//;

.... but here you assume that it succeeded and put a value in
$SmallTable. You should put this line somewhere else, where you are
assured of having a value in $SmallTable{int($dateday)}, or at least
test for a value before trying to modify it:

if( $SmallTable{int($dateday)} ) {
...
}
}

with this hash the calendar for that month is built.

A possibly better approach would be to push each event into an array,
then use join to concatenate the events when you have finished parsing
the file.
 
N

Nathan Keel

Wijnand Nijs wrote:


That says it all.

# drop the first HR
$SmallTable{int($dateday)} =~ s/^<BR><HR NOSHADE WIDTH=25%>//;

That is trying to blindly substitute.

I would not recommend learning from a bad script, unless you just want
to learn what to avoid. Trying to fix a bad script is possibly
helpful, but more likely will confuse you.
 
K

Keith Thompson

IIRC if "Year" hasn't been filled in in the form you seem to be
getting this then param("Year') is an undefined value, not an
empty string. So all you have to do is to apply the 'defined'
operator instead of comparing to "". Or write it all in a single
line:

$Year = param("Year") || $local_year;

This deals with the case that param("Year") is not defined as well
as that it's an empty string.

But be careful; this will set $Year to $local_year if param("Year")
is either undef, the empty string, or 0. That shouldn't cause any
visible problems for a year number (unless you're dealing with very
old events or using 2-digit years), but it could bite you for values
that could legitimately be 0.

Perl 6 has a "defined-or" operator, spelled "//", that acts like
"||" except that it tests its first operand for definedness rather
than truth. So in Perl6 you could write:

$Year = param("Year") // $local_year;

Perl 5.10 has this as well, but I think it was introduced relatively
recently (5.8.8 doesn't have it), so there are still plenty of Perl
installations out there that don't support it.

If you can't assume that the // operator will be available, you
can do something like this:

$Year = param("Year");
$Year = $local_year if not defined $Year;

Or like this:

$Year = defined param("Year") ? param("Year") : $local_year;

But note that the latter calls param("Year") twice.
 
J

Jens Thoms Toerring

But be careful; this will set $Year to $local_year if param("Year")
is either undef, the empty string, or 0. That shouldn't cause any
visible problems for a year number (unless you're dealing with very
old events or using 2-digit years), but it could bite you for values
that could legitimately be 0.

Thanks, good point! Hadn't considered it to be a possiblilty.

Regards, Jens
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,528
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top