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

Monks,

This is driving me crazy. I keep getting the error message Use of uninitialized value in concatenation (.) at /usr/local/apache/cgi-bin/temp line 196 when I run my script with -w. Here is the part of the script where the error is happening:
188 open (IN,"$header") || die "can't open file $header: $!"; 189 open (OUT,">$output") || die "can't open file $output: $!"; 190 while (<IN>) { 191 print OUT $_; 192 } # close while 193 close(IN) || warn "can't close $header: $!"; 194 close(OUT) || warn "can't close $output: $!"; 195 196 my $string = "$month$year1"; 197 my @days = ($day1 .. $day2); 198 @days = map {$_ .= "$string"} @days;
Can somebody please tell me what I am doing wrong?

Thanks
<br -Dru

Replies are listed 'Best First'.
Re: Why do I keep getting this error message?
by dvergin (Monsignor) on Dec 05, 2001 at 22:33 UTC
    Either $month or $year1 has not been assigned a value.

    When an error like this occurs with a variable in a double-quoted string (or its ilk) Perl refers to it as concatenation. It's as if you had typed

         "My name is " . $myname
    
    instead of
         "My name is $myname"
    
    If you want to know which one it is, simply print out each one on lines just before the error line. Or sometimes if I think this may happen again some day. I alter the code (with a reminder of why I left it that way) and just leave it as:
    # my $string = "$month$year1"; my $string = $month; # separate vars for easier debug $string .= $year1; # separate vars for easier debug
    This way I don't need to come back to remove the print trace statements. And if the problem happens again, I can zero in on it immediately.
Re: Why do I keep getting this error message?
by George_Sherston (Vicar) on Dec 05, 2001 at 23:04 UTC
    dvergin is spot on. The only thing I'd add is that if for some reason you want to be able to deal with the occasional appearance of an unititialised value then this wd do the trick (or whatever defaults you favour): 196   my $string = ($month or 'no month') . ($year1 or 'no year');

    § George Sherston
Re: Why do I keep getting this error message?
by vek (Prior) on Dec 06, 2001 at 03:20 UTC
    Or if you have some reason to suspect that there are valid circumstances where $month or $year1 are not getting assigned, you could always test them:
    unless ( $month && $year1 ) { # your error code here... }