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

Error: Use of uninitialized value in printf at perl_lib/areg_fparse.pm

package reg_spec.pm; sub new { my $self = {}; my $dummy = shift; my $name = shift; my $offset = shift; my $comment = shift; my @fields = @_; my $fref = \@fields; my $fc = @fields; $self->{name} = $name; $self->{field_ptr} = $fref; $self->{offset} = $offset; $self->{comment} = $comment; bless ($self); return $self; } package areg_fparse.pm; my $new_reg = reg_spec->new ($cur_reg_name,$cur_reg_offset,$cur_reg_co +mment,@cur_fields); printf $OFILE "<Descr Value=\"%s\"> </Descr>\n",$self->{comment};
I wonder why this error! Using perl after 2yrs!!

Replies are listed 'Best First'.
Re: Uninitialized value error
by moritz (Cardinal) on May 02, 2012 at 06:48 UTC

    package areg_fparse.pm; gives me a syntax error.

    Anyway, "Uninitialized value" is a warning, not an error. And it comes with a line number, which tells you where a value is uninitialized. And once you know the line, it should be pretty easy to figure out which value it is that is undef.

Re: Uninitialized value error
by davido (Cardinal) on May 02, 2012 at 06:53 UTC

    Where are you setting $cur_reg_comment? The code you presented never set a value in $cur_reg_name, $cur_reg_offset, $cur_reg_comment, or @cur_fields. If that's all the code, their values are undef (except for the array which is ()). You pass undefined values into the constructor, it passes them straight through to its underlying object stash, and then you print one of those undef values back out in line 23. Perl is kind enough to issue a warning that you might be doing something wrong.

    I'm sure there's more code than you're presenting, but it looks reasonably clear that if $cur_reg_comment is empty when you call the constructor, $obj->{comment} will be empty when you try to print it.


    Dave

Re: Uninitialized value error
by Jenda (Abbot) on May 02, 2012 at 09:14 UTC
    sub new { my ($class, $name, $offset, $comment, @fields) = @_; my $self = { name => $name, offset => $offset, comment => $comment, field_ptr => \@fields, }; return bless($self, $class); }

    Jenda
    Enoch was right!
    Enjoy the last years of Rome.

      Hi All.. I fixed this issue. Was actually trying to access it with a different pointer. Thanks!

        Perl doesn't have pointers except for pack and "((\$scalar)+0)".