Sorry I was not clear enough.
The test case, which is the Perl with this posting, does work for me as well.
However, I have the problem with a big application which is impossible for me to post. I have used the same methods.
Therefore I am hoping for some guidance about how to diagnose what is causing the problems in my main application.