kleinbiker7 has asked for the wisdom of the Perl Monks concerning the following question:

Hey guys, I do not understand how Compress::LZV1 works, and the instructions on the CPAN page dont help me either.

Here is my code:

#! /usr/local/bin/perl use Compress::LZV1; $str = "When the dream is big enough, the facts dont count!"; print "START: $str\n"; $compressed = compress ($str); print "COMPRESSED: $compressed\n"; $decompressed = decompress ($compressed); print "DECOMPRESSED: $decompressed\n\n\n";
The results look like this:
START: When the dream is big enough, the facts dont count! COMPRESSED: UWhen the dream is big enough, the facts dont count! DECOMPRESSED: When the dream is big enough, the facts dont count!

Basically, I cannot get the script to print out the compressed string. I would appreciate it greatly if you guys can tell me what I am doing wrong here. Cheers!

Replies are listed 'Best First'.
Re: How to use Compress::LZV1
by tadamec (Beadle) on Jun 10, 2004 at 22:42 UTC

    According to the Compress::LZV1 docs:

    Try to compress the given string as quickly and as much as possible. In the worst case, the string can enlarge by at most a single byte. Empty strings yield empty strings. The uncompressed data string must be smaller than 16MB (1<<24).
    (my emphasis added)

    The module is indeed "compressing" your data, as can bee seen by the U at the start of the compressed string.

    From what I remember, LZV-type compression is a dictionary-based algorithm. Without getting too deep into compression, this means that a dictionary is used to map out "compressable" portions of the message. This dictionary takes up a certain amount of space (say, 100 bytes). If the size of the data to compress is already smaller than the size of the dictionary, actually compressing the data will give a net increase in the size; not what you wanted. The implimentation of the algorithm used in the Compress::LZV1 package is smart enough to realize this fact and stores the string with a flag denoting that the string is uncompressed. This gives a net increase of one byte to the data, which is definately better than adding 100 bytes to the message.

    You're not doing anything wrong, as far as I can tell, other than using too small of a bit of data. Try increasing the amount of data you're using and see if that helps.