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

Hey, I have a variable that holds a compressed body of HTML.
The compression is gzip, so I wanted to uncompress it to another variable, but the only way I saw to uncompress it is using files, and a file handlers.

Is there any way that I can uncompress the content of my variable to another variable?

Replies are listed 'Best First'.
Re: Uncompress gzip from variable
by pmqs (Friar) on Jan 01, 2012 at 13:49 UTC
    Try this
    use IO::Uncompress::Gunzip qw(:all); # assume compressed HTML is in $compressedHTML my $uncompresedHTML; gunzip \$compressedHTML => \$uncompresedHTML;
Re: Uncompress gzip from variable
by flexvault (Monsignor) on Jan 01, 2012 at 14:23 UTC

    To expand on the above post from pmqs. If you are generating the HTML, you can use the code below to compress/uncompress the HTML. I find I can compress a 100K HTML to about 9K with these tools, and then save the variable in a DB for later reuse. Excellent!

    use IO::Compress::Gzip qw( gzip $GzipError ); use IO::Uncompress::Gunzip qw(gunzip $GunzipError ); my ( $uncompresedHTML, $compressedHTML ); . . . gzip \$uncompressedHTML => \$compresedHTML; . . . gunzip \$compressedHTML => \$uncompresedHTML;

    If your getting the zipped variable from a file, there are many ways to compress/uncompress data, so you may have to use 'qx/ /;' to use a system application and get the results back.

    Good Luck

    "Well done is better than well said." - Benjamin Franklin

Re: Uncompress gzip from variable
by locked_user sundialsvc4 (Abbot) on Jan 03, 2012 at 14:31 UTC

    As an aside, it can be quite useful to pre-compress even “ordinary” HTML (of any significant size) into gzip files so that the web server does not have to fully compress the stream each time.   You can do the work ahead of time (or “on the fly, but once”) and then simply send the stream with the appropriate HTML headers.   Natcherly, this qualifies as “something that has already been well-done,” such that there are a plethora of plug-ins for various platforms that will do it (more, or less) automagically for you.   I didn't fully realize just how much time a web-server could be spending on the repetitious task of compressing things for transmission.