OMG it works like charm. I still dont know why I was complicating the things. This code block works perfectly for me. Thanks you so much @poj I got stuck for like couple of days on this issue. Now I am good to go ahead its part of CPAN module I am creating. once again thanks for your help.