Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

My Stupid Head, or why you should never stop thinking

by btrott (Parson)
on Jul 27, 2001 at 09:51 UTC ( #100231=perlmeditation: print w/replies, xml ) Need Help??

It's been said before and before again, but here is another example of why you should never let yourself get locked into a particular way of thinking about a problem.

I'm working on a pure-Perl OpenPGP implementation (will be on CPAN in the next couple of days, Crypt::OpenPGP; the link will work when it's up :). And so on this particular day today I am working on the compression code, using Compress::Zlib.

Decompression worked fine, but I had a problem with compression: every time I would compress a data packet, GnuPG and PGP5 would have strange and mysterious errors when decompressing it. The errors usually didn't prevent the programs from decompressing the data, but they would die ugly deaths soon afterwards.

It turns out that the reason I was having the problem is because I am very, very stupid.

For the compression code I am using the relatively low-level Compress::Zlib interface (deflateInit, etc.) and after deflating, I am calling flush. And I was giving flush the Z_PARTIAL_FLUSH parameter, and then I tried Z_SYNC_FLUSH, etc. etc. The reason I did this is because when I was writing the compression code for Net::SSH::Perl, it took me a while to realize that I needed to use Z_PARTIAL_FLUSH. So I figured, why not save myself some time and just start using the "correct" flush type in the beginning.

This is a problem when the "correct" flush type is not so correct.

So I spent hours debugging the code, printing out hex dumps of the compressed and decompressed data, etc.

All the time not realizing that *the default*, Z_FINISH (which you get if you don't supply any args at all--hence default), is what works for PGP.

  • Comment on My Stupid Head, or why you should never stop thinking

Replies are listed 'Best First'.
Re: My Stupid Head, or why you should never stop thinking
by jepri (Parson) on Jul 27, 2001 at 11:56 UTC
    Funny you should mention that. I just spent a day implementing a LZW compressor in Perl. It took me a long time to do, I suspect because I am qutie thick, but mainly I was trying to do it by looking at C source code.

    Kids, do not try this at home! The C program, which was meant to demonstrate clearly how it all worked was in fact horribly obfu. Linked lists everywhere, and ick. OTOH, it did run about 100 times faster than my Perl effort.

    My little bit of agony was that I wasn't implementing the linked lists quite right. The first few lines were decompresing correctly, but as the program went further random garbage would appear, eventually obscuring the text. I was so close.

    Many hours of shotgun debugging later, I threw out my pseudo-C code, and implemented a Perl version of a mathematical description of the LZW process. I nearly fell off my chair when it compiled and ran perfectly first time. I think that may be a lifetime first too.

    So there is a moral to this story - don't do C, it ruins your style ;P

    I would post the code, but Unisys are very clear that they will not tolerate even free versions to be distributed.

    I didn't believe in evil until I dated it.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlmeditation [id://100231]
Approved by root
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (4)
As of 2023-12-04 03:30 GMT
Find Nodes?
    Voting Booth?
    What's your preferred 'use VERSION' for new CPAN modules in 2023?

    Results (20 votes). Check out past polls.