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

what does this error means in perl? Use of uninitialized value $dna in length at cpg8.pl line 4.

Replies are listed 'Best First'.
Re: Error meaning
by marto (Cardinal) on May 19, 2014 at 06:54 UTC

    It means that either $dna has no value assigned to it, that you've assigned undef to $dna at some point or problems with scope. This is a common warning. If you actually want someone to tell you what exactly is wrong you're going to have to show us the code you are running, but you know that already.

Re: Error meaning
by boftx (Deacon) on May 19, 2014 at 07:09 UTC

    As was pointed out above, it basically means that $dna has a value of undef.

    The usual "fix" to this is to initialize the variable in question to an empty string (''). If, however, that is not reasonable because the variable is initialized by a function call that could return undef, then the following should be viable:

    # using a "modern" perl (5.10 or newer) my $dna = f(x) // ''; # using an older perl my $dna = f(x) || '';
    If worse comes to worst, you can always place the variable inside double quotes to force interpolation into a string for length to work with:
    if ( length("$dna") ) { ... }
    Edit: Had a brain fart there. This will work:
    if ( length( ($dna || '') ) { ... }

    It helps to remember that the primary goal is to drain the swamp even when you are hip-deep in alligators.
Re: Error meaning
by AppleFritter (Vicar) on May 19, 2014 at 08:56 UTC

    BTW, You can use diagnostics; in your code to get long-form explanations for such error messages:

    Use of uninitialized value in length at cpg8.pl line 4 (#1) (W uninitialized) An undefined value was used as if it were alread +y defined. It was interpreted as a "" or a 0, but maybe it was a mi +stake. To suppress this warning assign a defined value to your variables. To help you figure out what was undefined, perl will try to tell y +ou the name of the variable (if any) that was undefined. In some cases it + cannot do this, so it also tells you what operation you used the undefine +d value in. Note, however, that perl optimizes your program and the opera +tion displayed in the warning may not necessarily appear literally in y +our program. For example, "that $foo" is usually optimized into "that + " . $foo, and the warning will refer to the concatenation (.) operat +or, even though there is no . in your program.

    In order to say more, it'd be useful to see just what line 4 of your script actually says.

Re: Error meaning
by BillKSmith (Monsignor) on May 19, 2014 at 12:29 UTC
    I frequently find that blank line(s) at the end of a data file are the root cause of this error. In that case, the only error in the perl code is a failure to anticipate them.
    Bill
Re: Error meaning
by LanX (Saint) on May 19, 2014 at 13:14 UTC
    Hint:

    Take a look at line 4 and show us the code! :)

    Cheers Rolf

    ( addicted to the Perl Programming Language)