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

Ok bare with me, ok.
I have a program that writes values created to two different files, "pass" and "userlog" as it happens. The program works perfectly apart from the this message that appears in "userlog", with the data appended to it. I'm aware that this error message appeared because I have warnings turned on. But what I want to no is how to fix the problem:
Use of uninitialized value in join or string at reg.cgi line 166 (#1) (W uninitialized) An undefined value was used as if it were already de +fined. 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 operati +on you used the undefined value in. Note, however, that perl optimizes your program and the operation disp +layed in the warning may not necessarily appear literally in your pro +gram. For example, "that $foo" is usually optimized into "that " . $foo, and + the warning will refer to the concatenation (.) operator, even thoug +h there is no . in your program.
Heres the code for the error:
open USRNF,">>$file" or die "Couldn't find user file $file, Perl s +ays $!\n"; print USRNF join "#", @info; #Line 166 Error = "#" print USRNF "\n"; close USRNF; open PASS,">>$pass" or die "Coundn't find user file $pass, Perl say +s $!\n"; print PASS $user_name; print PASS "\\"; print PASS &encryption($password); print PASS "\n"; close PASS;
Thanks, Eoin..

Replies are listed 'Best First'.
Re: Perl says $!what???
by gjb (Vicar) on Jan 03, 2003 at 21:08 UTC

    Looks like @info is undefined, not initialized or contains undef values, try to print those values with print "'$info[$i]'\n"; and see what happens.

    Hope this helps, -gjb-

Re: Perl says $!what???
by JamesNC (Chaplain) on Jan 03, 2003 at 21:25 UTC
    you get this when stuff is undef easy fix to just check it like so... try
    open USRNF,">>$file" or die "Couldn't find user file $file, Perl says +$!\n"; print USRNF join "$var", @info if($var); # if defined print USRNF "\n" if($var); #may want this too close USRNF; open PASS,">>$pass" or die "Coundn't find user file $pass, Perl say +s $!\n"; print PASS $user_name; print PASS "\\"; print PASS &encryption($password); print PASS "\n"; close PASS;
    :-)
Re: Perl says $!what???
by poj (Abbot) on Jan 03, 2003 at 23:46 UTC
    Looking back at your earlier code
    my $dateofbirth = ("$dd/$mm/$yy"); my @info = ($user_name, $password, $email, $gender, $real_name, $datof +birth, $city, $country);
    you had an typo error with dateofbirth <> datofbirth, did you fix it ?
    poj
Re: Perl says $!what???
by Anonymous Monk on Jan 03, 2003 at 21:43 UTC
    Hi ,

    The @info seems not to be defined first and not possesing a value when used here.You could my the array and use it or chech for if defined while you use it .

    Regards ,
    Padmaraj K.Shankar (Pad)
      The thing that stumps me is that @info is defined at the start of the sub, taking on the value of the argument. And that value is an array whose values are all defined. @info is defined because it prints to the file. Heres the whole sub excluding the html. Could it be the "#" that is out of place??
      I just don't know.
      Thanks, Eoin..
      sub good { my @info = @_; my $title = "Registration successful!"; my $revised = scalar localtime; my $body = <<HTML; <body> </body> HTML open USRNF,">>$file" or die "Couldn't find user file $file, Perl s +ays $!\n"; print USRNF join "#", @info; print USRNF "\n"; close USRNF; open PASS,">>$pass" or die "Coundn't find user file $pass, Perl say +s $!\n"; print PASS $user_name; print PASS "\\"; print PASS &encryption($password); print PASS "\n"; close PASS; return $title, $body; }
        The only way I could get the error back from your sub that you specify is when I pass a value in @info that is not initialized. For example (i took out the irrelevent code so use strict; would not complain about $user_name, $file, not being defined in my tests.)

        1. This code works

        my $house = 1; my $car = 1; my @passed = ($house, $car); my ($t1, $t2) = good(@passed);

        2. Whereas this code does not (well it works, but diagnositics, and warnings both warn you of a possible error.)

        my $house = 1; my $car; my @passed = ($house, $car); my ($t1, $t2) = good(@passed);

        So the conclusion is that somewhere you have a value being passed into the array being passed into your sub, without a value. I hope this makes sense, if not I can try to explain further.

        update: I forgot to mention that the following does not complain either:

        3.

        my $house = 1; my $car; my @passed; my ($t1, $t2) = good(@passed);

        -enlil