Sorry, I was rushing to send the code without realizing that there were some mistakes. The problem was related to the nested sub which is referencing a is referencing a lexical variable defined in an outer subroutine. I resorted to using the sub {} syntax and it is working properly. Thank you guys for your advice. use warnings did the trick.