in reply to Re^6: Can not use JSON module
in thread Can not use JSON module

As I have multiple cgi files, do I need to copy-paste that function in each file or is there any way to keep that function in separate file and use it, if possible how?

This sort of code re-use is performed in Perl using modules. See Simple Module Tutorial for how to create and use a module for such a purpose. There are other, similar guides for Creating and Distributing Modules in our Tutorials section.