A Decade of Lexical Filehandles


Perl 5.6.0 is almost a decade old; perldoc perlhist gives a release date of 22 March 2000.

My favorite feature of Perl 5.6.0 is lexical filehandles. Instead of having to access the IO slot of package global typeglobs, I could use lexical variables to contain filehandles -- without having to muck about localizing symbol tables or worrying about action at a distance or lifetimes of global symbols.

Yet to this day, almost a decade later, I still see the old way with all of its disadvantages (Tell the truth; do you understand every word of "the IO slot of package global typeglobs"? Do you want to explain that to novice programmers?) in new code.

Perl 5.6.2 is long dead. Perl 5.8.9 is the last of its release series too. The argument for running new code on old installations of Perl 5 is awfully thin, in that light.

Likewise I can't make a simplicity argument for the old approach. Making old-style filehandles work like people might expect is anything but simple. Throw in a local here or there and the typeglob sigil and maybe a gensym() call for good measure. Fun!

Reasonable people differ on style and technique, but I wonder what makes a feature such as pseudohashes or 5.005-style threads so hated that it eventually gets deleted, while difficult-to-use-correctly features superseded by better replacements stick around far longer than necessary. My guess is that the Perl 5 world suffers here, as usual, from a questionable abundance of old code, old tutorials, old books, and copy and paste coding from ancient sources of dubious wisdom. (This probably means I should submit patches to perldoc perluniintro and other offenders in the core documentation.)

Perhaps it's time to consider a gradual, intentional, well-tested and well-reviewed campaign to update tutorials and example code with somewhat more modern examples of maintainable Perl.

(For fun, imagine a world where the canonical printed Perl 5 reference covered a version of Perl 5 released this millennium. Then again, Perl.com thinks that 5.6.2 is "the previous version of Perl" 5.)


Well 5005threads is "easy". It had only been around since 5.005 before it was deprecated in 5.6.0 (and even still the deprecation cycle took 10 years to complete -- -Duse5005threads was supported throughout Perl 5.8). The story is similar with pseudohashes. Sure, they weren't deprecated until 5.8.0, four years after they came out, but nobody used the damn things, so there were only about two people to cry when they were deprecated, and nobody cared when they were removed.

Moral of story: things move at glacial speed in the name of "not breaking old code" :)

Oh, well, that would be nice. Finding information on UTF-8 and DBI/DBD lets you run into all sorts of advice that's neither malicious nor correct.

When you look at the man page for the 'open' function (for the current Perl 5 version 10.1 release): http://perldoc.perl.org/functions/open.html - all the more advanced examples and code samples use the typeglobs filehandles. Honestly it goes into such areas that I don't know if it is just an omission in updating the docs or if it is left on purpose and that you can use these more advanced features only via typeglobs.

Modern Perl: The Book

cover image for Modern Perl: the book

The best Perl Programmers read Modern Perl: The Book.

sponsored by the How to Make a Smoothie guide



About this Entry

This page contains a single entry by chromatic published on February 15, 2010 6:24 PM.

Chunking, Subtlety, and Whitespace was the previous entry in this blog.

Why SDL Perl Matters is the next entry in this blog.

Find recent content on the main index or look in the archives to find all content.

Powered by the Perl programming language

what is programming?