print ++($foo = "99"); # prints "100"
the parentheses are there only to ensure that the assignment to $foo occurs before the auto-increment.
But note that the auto-increment used here is in the prefix position. When you use an auto-increment in the postfix position, the increment occurs after the rest of the statement is executed. So when:
if ($sequence_number++ gt "99999") {
has been evaluated, the value in $sequence_number is one greater than the value it had in the comparison.
A bigger problem arises from the use of gt, which makes an alphabetic comparison. So the comparison "100000" gt "99999" will actually fail, because "1" is alphabetically “less than” "9". In a case like this, it’s better to use eq for an exact comparison.
Another thing to watch out for is that in your proposed code:
if (($sequence_number)++ ...) {
$sequence_number = "00001";
}
else {
($sequence_number)++;
}
when the if clause fails, $sequence_number will be incremented twice, once in the if and again in the else. That’s not what you want.
I gather that what you do want is a cycle of 5-digit numbers beginning with "00001", incrementing by one up to "99999", then reverting to "00001" and repeating as before. In other words, the number "00000" never appears. This can be done as follows:
my $sequence_number = "00001";
use_seq_no($sequence_number);
while (...)
{
if (++$sequence_number eq "99999")
{
$sequence_number = "00001";
}
use_seq_no($sequence_number);
}
sub use_seq_no
{
...
}
Hope that helps,
|