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

How does one get the results of calling freeze to print properly to a web page? Properly in this case would mean that I could stick the data in a hidden field in a form. Then submit the serialized object back to the web server and thaw it out.

I believe this is really a question of printing 'strange' characters to the web browser since I can write $foo to a file. Also, I can tell the print is writing the entire object due to the amount of time it takes for the page to load...strange.

use Storable qw(nstore store_fd nstore_fd freeze thaw dclone);

$obj = Really::Big::thingy->new();
$foo = freeze $obj;
print $foo # I want to print this to the browser;

Replies are listed 'Best First'.
Re: Writing frozen objects to a browser?
by ColtsFoot (Chaplain) on Sep 02, 2000 at 12:44 UTC
    The following seems to work for me
    #!/usr/bin/perl -w # use strict; use CGI; use FreezeThaw qw(freeze thaw); my $page = CGI->new(); my $ice_cube = freeze $page; print $page->header(); print $page->start_html(); print $page->startform(); print qq(<INPUT TYPE='HIDDEN' NAME='FROZEN' VALUE='$ice_cube'>\n); print $page->endform(); print $page->end_html();
    When I call up this page in my browser the following HTML
    is output
    Content-Type: text/html <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN"> <HTML><HEAD><TITLE>Untitled Document</TITLE> </HEAD><BODY><FORM METHOD="POST" ENCTYPE="application/x-www-form-urle +ncoded"> <INPUT TYPE='HIDDEN' NAME='FROZEN' VALUE='FrT;@1|>>0|$3|CGI%4|$11|.fie +ldnames$11|.parameters%0|@0|'> </FORM></BODY></HTML>
    The only thing that I can suggest is that you escape any single or double quotes that appear in the
    frozen object depending on which you use to to enclose the
    value of the hidden field.
    Hope this helps
      My bad. I should have included the module I am using. In any case, I believe my trouble is caused by not dealing with non-printable characters properly.

      use Storable qw(nstore store_fd nstore_fd freeze thaw dclone);

      Thanks.

RE: Writing frozen objects to a browser?
by DrManhattan (Chaplain) on Sep 02, 2000 at 18:48 UTC
    You can ensure the serialized data only has printable characters in it by unpacking it into hex like so:
    my $foo = unpack('H*', freeze($obj));
    Then you can thaw it out like this:
    my $obj = thaw(pack('H*', $foo));

    -Matt

      Looks like the serialized object is getting horked during the pack/unpack. I think I shall need to dig into Storable to see what exactly is happening.
      Any thoughts?
      Mon Sep 4 12:25:35 2000 index_new.pcgi: Storable binary image v111.205 more recent
      than I am (v2.2) at ./blib/lib/Storable.pm (autosplit into
      ./blib/lib/auto/Storable/thaw.al) line 222, at
      /usr/SD/docs/SERVICE/ReArch/index_new.pcgi line 424