in reply to foreach problem

That's because in line two you're referring to using the package variable %lib when you mean to be dereferencing the hashref you've got: $lib->{'foo'}.

The cake is a lie.
The cake is a lie.
The cake is a lie.