"CPAN is your bestest friend." There you will find fast, thoroughly-tested modules for correctly handling all kinds of files and formats – JSON, XML, CSV, you name it. "There's more than one way to do it," but "the best way to do it" is always to "let somebody else do it for you." Just use their module, knowing that it is known to work correctly in all cases.