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.
(6)
]
