adamcrussell

Perl Weekly Challenge 028

Part 1

Sample Run

$ perl perl5/ch-1.pl /opt/local/bin/git
The file content is binary.

$ perl perl5/ch-1.pl perl5/ch-1.pl
The file content is ascii.

$ perl perl5/ch-1.pl `which perl`
The file content is binary.

What I Did

This is one of the rare occasions where I made heavy use of a CPAN module for a Perl Weekly Challenge. Here the various testing needed such as inspecting the so called magic numbers seemed particularly unappealing to write from scratch and would just be simple checks of known values anyway. So, in this case I used File::MMagic to determine the type of the file specified as a command line argument and then, based on the challenge specification, decided to call any text file "ascii" and any other file "binary". This isn't necessarily always the case since a text file might contain non-ascii characters but for this purpose finer grained labelling is not required it seems!

Part 2

Sample Run

What I Did

I used POE::Component::Schedule to call a function, update_clock(), every second. That function then gets the current time and prints it out. Straightforward! Well, not exactly. First of all, POE is a tremendously powerful module and I've always wanted to try doing more with it but have never really needed to. Without a particular need it was impossible to invest the necessary time. This problem gave a nice opportunity. Documentation of POE and its components is somewhat infamously impenetrable to new comers to the module, however. Advanced Perl Programming [1] has a full chapter covering POE which is excellent, I highly recommend the entire book. Also, I wanted to do more than simply print the numbers but had zero patience for fiddling around with the various animation and display options for the numbers. I kept it simple: I use ANSI escape codes to print the clock in yellow on a white background. Each call to update_clock() will first print enough backspace characters to move the cursor to the beginning of the line and then overwrite the old time with the new. The result is a small console animation of a constantly updating clock.

References

[1] http://shop.oreilly.com/product/9780596004569.do
POE is covered in Chapter 7

Comments for this post were locked by the author