Beside all the Perl solutions given above, you could also use the join util, however, this only works if you've got GNU's textutils installed.
This would make your code look something like this:
JaWi is absolutely right. You should NEVER rebuild a wheel (especially a GREAT wheel that's already written in C by the GNU fellas) in perl unless you're on a system where you absolutely CANNOT use them or are writing something that HAS to be VERY portable.
And in case you're wondering, you can even have the GNU utilities on a Win32 system. Check out http://gnuwin32.sourceforge.net/. It includes the join GNU utility. Excellent stuff, use it all the time.