I believe it's due to the way Perl handles blocks. Each subroutine has a private pad, where it stores lexical variables -- variables you declare within the subroutine with the
my operator. When you refer to a variable within the subroutine, Perl first looks in the pad for a variable with the required name, then in the global symbol table for the current namespace. This allows you to say:
my $foo # a lexical
= $foo; # a package global
I think the problem is that you're running afoul of an optimization. Since it's expensive to allocate and deallocate memory, Perl often reuses and rarely clears the pads attached to subroutines. That means that names and values stick around. Normally, this isn't a problem.
What your code did was to assign the value of the global $s_off_no (in the package symbol table) to the lexical $s_off_no (in the pad attached to the sub) on the first invocation. On subsequent calls, $s_off_no on the right hand side resolves not to the global but to the lexical left over from the first call.
If you pass arguments to the sub, you'll always end up with fresh values in the pad. It could be considered a bug, but this is a really good corner case.
(note: this is speculation on my part, with the appropriate amount of hand-waving)
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.