How much has Perl (and other languages) changed?
Archive - Originally posted on "The Horse's Mouth" - 2011-06-10 20:13:13 - Graham EllisHow much has Perl code mover forward over the years? A lot, and not a lot. To some extend, programming languages are the eye of the storm of technology - and that's because people who invest in code want it to be good for many years, so the same tools are used for several generations of products, with perhaps incremental rather than complete changes.
I was minded of this when asked for a binary data handling demo on today's course, and I pulled up an ancient example called ystwth.pl from the days we named examples after a theme, and the theme of the Perl course was rivers. You'll see what a big language Perl is when you realise I was getting pretty desparate to choose the Ystwyth at Aberystwyth (the main river there is the Rheidol, of course, but I digress).
Anyways - here's the new code ... a better Usage line, using the newer 3 parameter form of the open function, and frankly much of the other change is me tidying up my own code. I have decided NOT to use say because not everyone has upgraded to a Perl that supports in yet or at least hadn't in my group of trainees, and I'm not talking Perl 6 ... yet. But there's another story I have there, to write up in the morning.
# Checking whether files are in GIF format and if they are reporting image size
# Nice demo of a little bit of binary data handling
die ("Usage: %body% filename [filename [filename ....]]\n") unless @ARGV;
foreach $fn (@ARGV) {
if (open FH,"<",$fn) {
read (FH,$buffer,10);
($gifword,$giflevel,$x,$y)= unpack("a3a3vv",$buffer);
if ($gifword ne "GIF") {
print ("$fn is NOT a valid GIF file\n\n");
} else {
print ("$fn - GIF file - version $giflevel\n");
print ($x," pixels wide by ", $y," pixels high\n\n");
}
} else {
print "Failed to open file $fn - $!\n\n";
}
}
__END__
Sample output:
20-dynamic:bperl grahamellis$ perl bincopi
Usage: bincopi filename [filename [filename ....]]
20-dynamic:bperl grahamellis$ perl bincopi *.gif
copy.gif - GIF file - version 89a
72 pixels wide by 73 pixels high
20-dynamic:bperl grahamellis$ perl bincopi *.gif dfjsjkfdfs bincopi
copy.gif - GIF file - version 89a
72 pixels wide by 73 pixels high
Failed to open file dfjsjkfdfs - No such file or directory
bincopi is NOT a valid GIF file
20-dynamic:bperl grahamellis$
The new example is also [here] on the web site.