faster way in writing Perl script