Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Source of warning message: "Use of uninitialized value in subroutine entry"

by BrowserUk (Patriarch)
on Dec 15, 2004 at 18:30 UTC ( [id://415141]=perlquestion: print w/replies, xml ) Need Help??

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

Does anyone know where (which source file(s)) the warning text "Use of uninitialized value in subroutine entry at ..." is issued from?

I've encountered this in a few occasions where checking the arguments to the sub being called, both before the call and immediately insde the sub, show no signs of undefs, and the subs function perfectly well.

I've usually just disabled that warning around the call in question. The weird thing is, that the warning seems to just suddenly appear, where it previously did not, and I haven't yet tracked down why. It appears as though it is related to source code formatting--which is strange in itself--but that's just a guess.

I'd post code to demonstrate the problem, but as has happened before, it has a habit of disappearing once I remove the no warnings 'Unitialized'; code. So I don't currently have a good demonstration of it.

One place it has cropped up several times is in code that calls Win32::API. Indeed, the only common theme to the problems are that it generally seems to involve code that drops into XS at some point. However, the error is always attributed to a line in my Perl source rather than the XS call.

A vague and wishy-washy description I know, which is why I'd like to track down where the message can be issued and under what circumstances, but my attempts to grep for the text of the message have turned nothing up. Presumably the message is composed at runtime and doesn't appear in the displayed form in the sources at all.


Examine what is said, not who speaks.        The end of an era!
"But you should never overestimate the ingenuity of the sceptics to come up with a counter-argument." -Myles Allen
"Think for yourself!" - Abigail        "Time is a poor substitute for thought"--theorbtwo         "Efficiency is intelligent laziness." -David Dunham
"Memory, processor, disk in that order on the hardware side. Algorithm, algorithm, algorithm on the code side." - tachyon
  • Comment on Source of warning message: "Use of uninitialized value in subroutine entry"
  • Download Code

Replies are listed 'Best First'.
Re: Source of warning message: "Use of uninitialized value in subroutine entry"
by Stevie-O (Friar) on Dec 15, 2004 at 20:18 UTC
    The fact that you're using XS explains a lot of this.

    To understand what's going on here, you need to better understand what the warning message actually means.

    The "Use of unitialized value" part is, as you surmised, referring to the usage of an undefined scalar as part of some operation. However, your assumption that 'in subroutine entry' means 'as part of the sub's arguments' is wrong. (If it did, then you'd get a warning every time you passed undef to a subroutine!) It actually means 'and the last Perl opcode that was run was the "entersub" opcode'. Similarly, the file/line number data you have came from the last "nextstate" opcode Perl processed.

    Because XS code is plain C, and not Perl bytecode, Perl can't know to update its file/line number information, or its last-opcode information. I was able to reproduce your problem doing this:

    # XS code SV* useofuninit(SV* arg) INIT: char *p; STRLEN plen; PPCODE: p = SvPVx(p, plen); XSRETURN_UNDEF; # Perl code my $foo; useofuninit(undef)
    SvPVx() gets the string value from a scalar. In this case, it sees that the scalar is undef, and before upgrading it to an empty string scalar, it calls report_uninit(). report_uninit() checks the last opcode data and reports the warning to you as 'in subroutine entry'.
    --Stevie-O
    $"=$,,$_=q>|\p4<6 8p<M/_|<('=> .q>.<4-KI<l|2$<6%s!<qn#F<>;$, .=pack'N*',"@{[unpack'C*',$_] }"for split/</;$_=$,,y[A-Z a-z] {}cd;print lc

      Many thanks for the clear and cogent explaination.

      I don't actually do any XS, but I do use modules which do, and that would appear to be the source of the problem. I still remains somewhat mysterious that on all the occasions where this has come up, the code in question has worked fine and I have just ignored the warning.

      Slightly more troubling is the I am making a gut-feel association between the appearance and disappearance of the warning, with changes to the formatting of the source code, rather than the logic. But that's only a feeling have.

      I've encountered it enough time that my first instinct is to disable the warning and get on with what I am coding, rather than trying to debug the source of the problem itself.

      I'll know better what to be looking for the next time it comes up. Now, armed with better info, I'll try to be more dilligent next time.

      Again, my thanks for the education.


      Examine what is said, not who speaks.        The end of an era!
      "But you should never overestimate the ingenuity of the sceptics to come up with a counter-argument." -Myles Allen
      "Think for yourself!" - Abigail        "Time is a poor substitute for thought"--theorbtwo         "Efficiency is intelligent laziness." -David Dunham
      "Memory, processor, disk in that order on the hardware side. Algorithm, algorithm, algorithm on the code side." - tachyon
Re: Source of warning message: "Use of uninitialized value in subroutine entry"
by ikegami (Patriarch) on Dec 15, 2004 at 19:36 UTC

    Update: Oops, this isn't the message you're talking about. Disregard. Try searching for no more than "subroutine entry". I think this error message is assembled.

    Win32::API::Struct expects certain variables to be initialized where it doesn't really matter. Specifically, you must call ->align('auto') on a Win32::API::Struct instance after creating it. Also, you need to initialize all the fields of the struct. If you don't, passing this structure to an API function will cause numerous warnings by Win32::API::Struct::getPack as it tries to create a C version of the structure.

    An example from my replies to 391557:

    Win32::API::Struct->typedef(MEMORYSTATUS => qw{ DWORD dwLength; DWORD MemLoad; DWORD TotalPhys; DWORD AvailPhys; DWORD TotalPage; DWORD AvailPage; DWORD TotalVirtual; DWORD AvailVirtual; }); my $MEMORYSTATUS = Win32::API::Struct->new('MEMORYSTATUS'); # Avoid warning at Struct.pm line 214 $MEMORYSTATUS->align('auto'); # Avoid all other warnings in Struct.pm $MEMORYSTATUS->{'dwLength' } = 0; $MEMORYSTATUS->{'MemLoad' } = 0; $MEMORYSTATUS->{'TotalPhys' } = 0; $MEMORYSTATUS->{'AvailPhys' } = 0; $MEMORYSTATUS->{'TotalPage' } = 0; $MEMORYSTATUS->{'AvailPage' } = 0; $MEMORYSTATUS->{'TotalVirtual'} = 0; $MEMORYSTATUS->{'AvailVirtual'} = 0;

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://415141]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (2)
As of 2024-04-20 06:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found