I feel like I'm debugging quantum entanglement in my code. A string I'm reading from a file in one part of the code ended up in the *internal state* of the event loop.

...that's not supposed to happen.

Turns out that AnyEvent was leaking a reference to its internal state through $_ and my attempt to read a line from a file would overwrite that variable instead of creating a newly scoped $_.

Mystery solved.
Global variables, not even once.

Show thread

why would a while use global variable by default?
Why would $_ ever be not lexically scoped?

@wolf480pl I don't know the file details, this is pre-5.8 stuff, but I'm guessing it went something like this: Historically, $_ was a global variable (still is, I think. Perl is built on globals...), then at some point it was made possible to temporarily overwrite a global variable within a certain scope, functions called from that scope would also observe that change but functions outside wouldn't (this is what 'local' does, real handy feature).

Why does a while loop not localize $_? Not sure, I'm guessing its so that you can still access the value from the last iteration, maybe a backwards compat thing. for/foreach loops do localize $_.

Sign in to participate in the conversation

A lonely little town in the wider world of the fediverse.