The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

MySQL::Compress - MySQL COMPRESS() and UNCOMPRESS() compatible Perl functions

DESCRIPTION

This module provides functions compatible with MySQL COMPRESS() and UNCOMPRESS(). One reason you may want to use these functions is because MySQL COMPRESS() does not offer the possibilty to specify the compression level, whereas the mysql_compress() function in this module does.

SYNOPSIS

        # Import all functions:
        use MySQL::Compress qw(:all)

        # Or list the functions you want to import:
        use MySQL::Compress qw(
                mysql_compress
                mysql_uncompress
                mysql_uncompressed_length
        );

        # Emulate COMPRESS():
        my $compressed_string = mysql_compress('Hello world!');

        # Emulate UNCOMPRESS():
        print mysql_uncompress($compressed_string) . "\n"; # prints "Hello world!\n"

        # Emulate UNCOMPRESSED_LENGTH():
        my $len = mysql_uncompressed_length($compressed_string);        # $len equals byte length of "Hello world!"

EXPORTS

The following functions can be imported into the calling namespace by request:

        mysql_compress
        mysql_uncompress
        mysql_uncompressed_length
        :all    - what it says

FUNCTIONS

$dest = mysql_compress($source [, $level])

MySQL COMPRESS() compatible compression function. $level is the optional compression level (valid values are 0 through 9; default = 6). See Compress::Zlib documentation for details. Returns the compressed data on success, else undef.

From the MySQL COMPRESS() documentation: The compressed string contents are stored the following way: - Empty strings are stored as empty strings. - Nonempty strings are stored as a 4-byte length of the uncompressed string (low byte first), followed by the compressed string. If the string ends with space, an extra "." character is added to avoid problems with endspace trimming should the result be stored in a CHAR or VARCHAR column. (However, use of nonbinary string data types such as CHAR or VARCHAR to store compressed strings is not recommended anyway because character set conversion may occur. Use a VARBINARY or BLOB binary string column instead.)

$dest = mysql_uncompress($source)

MySQL UNCOMPRESS() compatible function. Uncompresses data that has been compressed with MySQL's COMPRESS() function. $source can be either a scalar or a scalar reference. Returns the uncompressed data on success, else undef.

$length = mysql_uncompressed_length($source)

Returns the expected uncompressed length of the given string that has been compressed with MySQL's COMPRESS() function. This is done without actually decompressing since COMPRESS() prepends the length to the compressed string. $source can be either a scalar or a scalar reference. Returns the expected uncompressed length on success, else undef.

SEE ALSO

Compress::Zlib

COPYRIGHT

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

AUTHOR

Craig Manley (craigmanley.com)