List a directory with millions files on linux

I needed to list all the files in a directory which had a lot of files, actually 4060276 to be exact. Usually I do this with ls or find, but with ls it used over 600 MB memory, and a lot of time. So I set out to find a better way of doing this. I found a post “You can list a directory containing 8 million files! But not with ls..” which explained the problem. But I wasn’t happy with compiling a C program to do this. That’s not very easy to do on all servers, so, as the Perl guy I am, I resorted to Perl to find an easier way. And as usually, Perl ftw 🙂