During today's Perl course, I was asked to provide an example of the
unpack function for extracting multiple values from a piece of data - typically binary data extracted from a file into a scalar variable.
Dorothy-2:de$ perl imgsize sd*.gif
sd1.gif is GIF, 89a, 450 by 250
sd2.gif is GIF, 89a, 250 by 300
sd3.gif is GIF, 89a, 300 by 250
sd4.gif is GIF, 89a, 333 by 333
Dorothy-2:de$
And what better images to illustrate the article that ones of the delegates on the course?
Here's the program:
for $file(@ARGV) {
open (FH,$file);
read (FH,$header,10);
($type,$st,$wid,$hei) = unpack("a3a3vv",$header);
print ("$file is $type, $st, $wid by $hei\n");
}
From memory (!) I recalled that a .gif file starts with 3 bytes to confirm that it's a GIF file, 3 bytes to tell us which subformat has been used, and 2 2-byte 16 bit unsigned integers, bigendian, which tell the width and height of the image in pixels.
You'll notice that the course had a
lot of delegate on it. Where there's a high number of delegates, all at a similar level, all from the same company, and all doing similar applications, I will let the customer place up to 20 delegates on a course. Where the delegates background differs, I request a reduced top number. And on a public course, where people from differnt companies book, we restrict delegates to just 8 so that there's a good opportunity for the tutor to talk to each and every delegate about their own individual use of the subject being taught.
P.S. There's another example of unpack on a .gif file -
[here] - that I wrote a wile back; the techniques used are slightly different, and the program is one that came from a series named after British Rivers - in this case the Ystwyth from Aberystwyth fame.
(written 2010-06-10)
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
P212 - Perl - More on Character Strings [453] Commenting Perl regular expressions - (2005-09-30)
[583] Remember to process blank lines - (2006-01-31)
[586] Perl Regular Expressions - finding the position and length of the match - (2006-02-02)
[597] Storing a regular expression in a perl variable - (2006-02-09)
[608] Don't expose your regular expressions - (2006-02-15)
[737] Coloured text in a terminal from Perl - (2006-05-29)
[928] C++ and Perl - why did they do it THAT way? - (2006-11-16)
[943] Matching within multiline strings, and ignoring case in regular expressions - (2006-11-25)
[1222] Perl, the substitute operator s - (2007-06-08)
[1230] Commenting a Perl Regular Expression - (2007-06-12)
[1251] Substitute operator / modifiers in Perl - (2007-06-28)
[1305] Regular expressions made easy - building from components - (2007-08-16)
[1336] Ignore case in Regular Expression - (2007-09-08)
[1510] Handling Binary data (.gif file example) in Perl - (2008-01-17)
[1727] Equality and looks like tests - Perl - (2008-07-29)
[1735] Finding words and work boundaries (MySQL, Perl, PHP) - (2008-08-03)
[1947] Perl substitute - the e modifier - (2008-12-16)
[2230] Running a piece of code is like drinking a pint of beer - (2009-06-11)
[2379] Making variables persistant, pretending a database is a variable and other Perl tricks - (2009-08-27)
[2657] Want to do a big batch edit? Nothing beats Perl! - (2010-03-01)
[2834] Teaching examples in Perl - third and final part - (2010-06-27)
[2874] Unpacking a Perl string into a list - (2010-07-16)
[2877] Further more advanced Perl examples - (2010-07-19)
[2993] Arrays v Lists - what is the difference, why use one or the other - (2010-10-10)
[3059] Object Orientation in an hour and other Perl Lectures - (2010-11-18)
[3100] Looking ahead and behind in Regular Expressions - double matching - (2010-12-23)
[3322] How much has Perl (and other languages) changed? - (2011-06-10)
[3332] DNA to Amino Acid - a sample Perl script - (2011-06-24)
[3411] Single and double quotes strings in Perl - what is the difference? - (2011-08-30)
[3546] The difference between dot (a.k.a. full stop, period) and comma in Perl - (2011-12-09)
[3630] Serialsing and unserialising data for storage and transfer in Perl - (2012-02-28)
[3650] Possessive Regular Expression Matching - Perl, Objective C and some other languages - (2012-03-12)
[3707] Converting codons via Amino Acids to Proteins in Perl - (2012-04-25)
[3927] First match or all matches? Perl Regular Expressions - (2012-11-19)
[4452] Binary data handling - Python and Perl - (2015-03-09)
G401 - Well House Consultants - On Site courses [139] Just provide a room and the students - (2004-12-03)
[191] Setting up a training room for a course - (2005-01-27)
[213] Off to Munich - (2005-02-13)
[241] Dress for training - (2005-03-10)
[382] Central London Courses - Perl, PHP, Python, Tcl, MySQL - (2005-07-18)
[438] Fair system for travel and accommodation expenses - (2005-09-07)
[439] You cant - (2005-09-08)
[1027] Cue the music, I'm happy. - (2007-01-09)
[1465] Perl, PHP, Python, Tcl, Linux, MySQL, Ruby courses ... - (2007-12-09)
[1600] Cambidge - Tcl, Expect and Perl courses - (2008-04-04)
[1781] Reception - (2008-09-01)
[2041] httpd, Tomcat and PHP course enhancements - (2009-02-14)
[2309] Lua training class in Spanish - (2009-07-29)
[2311] Floor to ceiling - (2009-07-30)
[2810] A course review - for the tutor to complete - (2010-06-14)
[2928] Public (scheduled) or private course? Book direct, or through a third party? - (2010-08-19)
[2956] On site course - travel and accommodation expenses - (2010-09-14)
[3131] Lua, Tcl, Python; Worldwide training classes - (2011-01-14)
[3137] Training Classes - should the training company provide a system for each delegate to use? - (2011-01-18)
[3691] Back in Cambridge to give a Lua course - (2012-04-09)
[3715] Changing face - Filton - (2012-05-01)
[3898] The course must go on - improvements to tutor travel plans, with immediate effect - (2012-10-17)
[3950] 10 reasons why I travel the night before a course starts - (2012-12-10)
[4132] Using your own laptop on our courses - now even easier! - (2013-07-05)
[4230] Well House Consultants - course prices for 2014 - (2013-12-26)
[4602] Training course and hotel room prices - 2016 - (2015-12-31)
[4739] A year of changes for Lisa and Graham Ellis, and Well House - (2017-05-27)
[4769] Python, Tcl, Lua and other Open Source programming courses for 2018 - (2017-12-31)
Some other Articles
How are you getting on?Regular Expression MythsTravelling across EuropeAfter the Perl course in NurnbergBinary data handling with unpack in PerlThe Merchants AndNuremberg - some picturesPerl - skip the classics and use regular expressionsSur la T.G.V.Sunday - waiting for the ferry.