in reply to Localization gotcha?

local $. in the <DATA>-block has no usage. What will perl do? it will copy the current value of $. to a temp variable, and reset it after the block.

It is not this block that it is messing up the results of $., it is the seeky-sub. What you could do is add a local $. in sub seeky, then you get the results you expect.

Since this means that at the start of the block it will copy $. to another variable, seek will set $. to another alias, and at the end of the block the value of $. is restored (by using the copy it made).

Update: another solution with which you would get the same result is to create a new block in your code, and localising $. in there. Something like:

while (<DATA>) { chomp; my $line = $.; warn "Pre match: Line $. == $line\n"; next if /match/; { local $.; seeky(); } warn "Postmatch: Line $. != $line\n"; }

Replies are listed 'Best First'.
Re: Localization gotcha?
by benizi (Hermit) on May 19, 2005 at 17:51 UTC

    You're quite right. And even though I understood what was going wrong, my interpretation of localization was backwards in this case. I was thinking that I was trying to make $.'s behavior local to the <DATA>-block, when I should have been attempting to localize seek()'s effects on $. to seeky().

    (It didn't help matters that my original problem was significantly more complicated. I tried to fix it by localizing $. wherever I had a "local $_;" in a module, but forgot to search for seek()s.)

    FWIW, I would add the "local $.;" statement to seeky itself, analagous to adding "local $_;" in subroutines.

    Thanks for the response, Animator.