in reply to Can scalar representation cause infinite loops?

length($a) lt 2 should be length($a) < 2. You are converting the returned length to a string and doing a lexical (not a numerical) compare.

$a = "abcdefghij"; length($a) lt 2 ==> "10" lt "2" ==> true $a = "0$a"; length($a) lt 2 ==> "11" lt "2" ==> true ... $a = "0$a"; length($a) lt 2 ==> "20" lt "2" ==> false

If length($a) starts in [0..1], the final length($a) will be 2.
If length($a) starts in [2..9], $a won't change.
If length($a) starts in [10..19], the final length($a) will be 20.
If length($a) starts in [20..99], $a won't change.
If length($a) starts in [100..199], the final length($a) will be 200.
If length($a) starts in [200..999], $a won't change.
If length($a) starts in [1000..1999], the final length($a) will be 2000.
If length($a) starts in [2000..9999], $a won't change.
If length($a) starts in [10000..19999], the final length($a) will be 20000.
etc.

This could cause thrashing with very long strings (which localtime doesn't return), but I don't see this ever creating an infinite loop.

Try using

my $min = (localtime)[1]; $min = sprintf('%02d', $min);

Replies are listed 'Best First'.
Re^2: Can scalar representation cause infinite loops?
by Anonymous Monk on Jul 19, 2007 at 22:20 UTC

    Aha! Thanks for pointing out the use of lt vs. <. I've noticed it was odd before, but didn't pause to think about what the consequences are. There is another similar loop earlier in the script that looks like this:

    while (length($extnum) lt $numlength) { $extnum = "0" . $extnum; }

    where $numlength can potentially be set to 4. Again, $extnum is unlikely to be a very long string, so this doesn't seem to be the culprit either, but I'll have to comb through the script again to see if other abuses of lt could have caused the bug.

    Thanks for the prompt reply!

Re^2: Can scalar representation cause infinite loops?
by Anonymous Monk on Jul 19, 2007 at 22:22 UTC

    Yeah, I told the author of the script to use sprintf instead, because it's more efficient, but also because it is actually correct. :-)