ChapterÂ 10.Â I/O
OnÂ twoÂ occasionsÂ IÂ haveÂ beenÂ askedÂ [byÂ membersÂ ofÂ Parliament],Â "Pray,Â Mr.Â Babbage,Â ifÂ youÂ putÂ intoÂ theÂ machineÂ wrongÂ figures,Â willÂ theÂ rightÂ answersÂ comeÂ out?." IÂ amÂ notÂ ableÂ rightlyÂ toÂ apprehendÂ theÂ kindÂ ofÂ confusionÂ ofÂ ideasÂ thatÂ couldÂ provokeÂ suchÂ aÂ question
Input and output are critical in any design, because they mediate the interface of an application or library. To most users of your software, what your I/O components do is their entire experience of what the software is. So good I/O practices are essential to usability.
I/O operations are also particularly susceptible to inefficiencies, especially on large data sets. I/O is frequently the bottleneck in a system, and usually doesn't scale well. So good I/O practices are essential to performance too.
Yet another concern is that I/O deals with the software's external environment, which is typically less reliable than its own internals. Dealing successfully with the multiple failure modes of operating systems, filesystems, network connections, and human beings requires careful and conservative programming. So good I/O practices are essential to robustness as well.
Don't use bareword filehandles.
One of the most efficient ways for Perl programmers to bring misery and suffering upon themselves and their colleagues is to write this:
open FILE, '<', $filename or croak "Can't open '$filename': $OS_ERROR";
Using a bareword like that as a filehandle ...
Get Perl Best Practices now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.