Ahh.. that looks like it makes sense. Was forgetting for a minute that declaring $b there would actually scope it inside the loop.
All code is untested unless otherwise stated.
All opinions expressed are my own and are intended as guidance, not gospel; please treat what I say as such and as Abigail said Think for yourself.
If in doubt ask.