Parentheses aren’t needed for auto-increment. In the documentation example:

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,

Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,


In reply to Re^4: POE - can't increment within sub by Athanasius
in thread POE - can't increment within sub by ljamison

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.