It is hard to answer without knowing what's the error message. Did you have a look at the logs? From the code you're showing, it doesn't look like a problem with XML::Twig, but who knows.
And yes, if several instances of your script are likely to be active at the same time and write the same file, then you need to lock it.
What do the logs tell you? As long as you don't look at the error file, your debugging is going to be just taking shots in the dark.
BTW, does flock work on Windows?
The last time I used locking (a while ago), I did not lock the file itself, but used a dedicated lock file:
Does this help?