Hi, I just want to confirm if the following scenario is in fact possible:

$a = (localtime)[1]; while (length($a) lt 2) { $a = "0$a"; }

Can this ever, under any circumstances, cause an infinite loop? Is there any circumstance under which Perl might be convinced that it should convert $a to an integer after the assignment, thereby causing the test to always fail?

Yes, I know this is horribly bad code... I didn't write this, but I'm trying to determine if this is the cause of the problem I'm seeing. This is in a CGI script which has been running all night at 100% CPU. Unfortunately I can't reproduce this problem, and the script wasn't running with debugging on, so all I could do was to use gdb to try to find out what happened.

I attached strace to the process, but see nothing: it doesn't seem to be making any syscalls. Attaching gdb to the process (and tediously interrupting/resuming it many times and collating the backtraces), I see that it's making the following calls (in unknown order; there are no debugging symbols so I don't know what order these functions are called in, and I don't know how complete this picture is):

(X -> Y in the above list means that a backtrace shows that function X called function Y at some point. Chained arrows indicate that at some point I saw that nested sequence of calls in the backtrace.)

From the (very) little I know of Perl internals, this seems to correspond with the code fragment listed above. I don't see any other place in the code that has a tight loop involving these operations. There are several places in the script with similar-looking code; I suggested to the writer of the script to replace them with sprintf, but I wanted to check here to make sure that I'm not totally off-base with my guess that these loops are probably the cause of the problem.


In reply to Can scalar representation cause infinite loops? by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.