For those with time on their hands,

Try your hand at something relatively un-perl-ish with Perl like, for example, reading and writing bitmap images directly in Perl, without the aid of modules such as Image::Magick or GD.

For example, say you wanted to read the header data from a Windows Bitmap file:

(see http://msdn2.microsoft.com/en-us/library/ms532301%28VS.85%29.aspx)

use strict; use Fcntl; my %headers = ( 0 => "file-type", 1 => "file-size-in-bytes", 2 => "must-be-0", 3 => "must-be-0", 4 => "offset-to-raster-in-bytes", 5 => "size-of-following-header-in-bytes", 6 => "width-in-pixels", 7 => "height-in-pixels", 8 => "device-planes", 9 => "bits-per-pixel", 10 => "type-of-compression", 11 => "raster-size-in-bytes", 12 => "horizontal-pixels-per-meter", 13 => "vertical-pixels-per-meter", 14 => "number-of-colors-used", 15 => "number-of-important-colors", ); my $x = binary_contents("path/to/bitmap.bmp"); my @x = bmp_headers($x); for my $i(0..$#x){ printf "%-5s %-10s %-20s\n",$i,$x[$i],$headers{$i} } sub bmp_headers { # careful - 's' is endian sensitive my $t = 'a2Vn2V4s2V6'; my $x = shift; unpack($t,$x); } sub binary_contents { my $g = shift; sysopen(my $F, $g, O_RDONLY) or die "cant open: $g\n$!\n"; sysread($F, my $x, -s($g)); close $F; return $x; }
If you read up on Bitmap Structs, you can read in and parse any MCSF Bitmaps (through Version 5). From there, try writing your own bitmaps with pack and syswrite or
open(my $FH, '>:raw', 'path/to/my/new/cool/bitmap.bmp'); print $FH $my_carefully_packed_binary_data
If you want a real challenge, try implementing your own graphics processing algorithms, like applying filters, contrasting, histogram normalization, etc... Try making it As Fast As C. (You can get very, very, close in Pure Perl).

Replies are listed 'Best First'.
Re: On Challenging Ways to Learn Perl
by merlyn (Sage) on Mar 27, 2008 at 16:23 UTC
    my %headers = ( 0 => "file-type", 1 => "file-size-in-bytes", 2 => "must-be-0", 3 => "must-be-0", 4 => "offset-to-raster-in-bytes", 5 => "size-of-following-header-in-bytes", 6 => "width-in-pixels", 7 => "height-in-pixels", 8 => "device-planes", 9 => "bits-per-pixel", 10 => "type-of-compression", 11 => "raster-size-in-bytes", 12 => "horizontal-pixels-per-meter", 13 => "vertical-pixels-per-meter", 14 => "number-of-colors-used", 15 => "number-of-important-colors", );
    And this would be this week's winner for "most egregious use of a hash". Why isn't that just an array?
      Wait, you mean there is actually a weekly contest for hash abuse!!! OMG, I would love that! Where is the node???
Re: On Challenging Ways to Learn Perl
by dragonchild (Archbishop) on Mar 27, 2008 at 12:55 UTC
    Speaking of directly writing image formats in Perl, I once wrote to PNG directly from Perl. It ended up being 10x faster for my limited needs than going through either Image::Magick or GD.

    My criteria for good software:
    1. Does it work?
    2. Can someone else come in, make a change, and be reasonably certain no bugs were introduced?
      Did you use a Gzip module?
        Nope. I served up through Apache and it handled the gzipping for me. :-)

        My criteria for good software:
        1. Does it work?
        2. Can someone else come in, make a change, and be reasonably certain no bugs were introduced?
Re: On Challenging Ways to Learn Perl
by ambrus (Abbot) on Mar 27, 2008 at 09:56 UTC
      Ha! I actually find that to be clever. The bmp contents might be trivial but I can safely say I've never seen that before. I can imagine awesome extensions to that. Like obfuscated code inside of a more elaborate, even colorful bmp. A png or jpeg would be fabulous at that.

      Seems to me that finding code whose bytes also rendered a nontrivial image, or making and image that incidentally yielded nontrivial code would be tantamount to a very hard fixed point problem...

        A PNG can't work, because it starts with a header that raises a syntax error in perl (unless you use some command-line switches). A GIF may, but I was lazy to do that. I don't know about JPEG, TIFF, or other image formats.

Re: On Challenging Ways to Learn Perl
by Anonymous Monk on Mar 27, 2008 at 04:17 UTC
    No, not a very challenging way to learn perl :)
      The topic was *learning*.

      But lets suppose it wasn't. A better question is: what then do you find challenging anonymous monk? Give us a good one!