david.jackson has asked for the wisdom of the Perl Monks concerning the following question:

How do I thaw and retrieve data from a "Storable file". Here's is the code I'm using for the CGI backend, it seem to work? Thanks for you time David
CGI backend (and runtime errors):
Use of uninitialized value in read at ./hashbrowns.cgi line 10 and 27 (#1)
#!/usr/bin/perl -w use strict; use diagnostics; use Storable qw( nfreeze thaw ); my ($server,$ipaddr,$funct); my ($FORM,$value,$pair,@pairs,$buffer,$name,%FORM); ###################################################### print "Content-type:text/html\n\n"; read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'}); @pairs = split(/&/, $buffer); foreach $pair (@pairs) { ($name, $value) = split(/=/, $pair); $value =~ tr/+/ /; $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg; $value =~ s/\n/ /g; # replace newlines with spaces $value =~ s/\r//g; # remove hard returns $value =~ s/\cM//g; # delete ^M's $FORM{$name} = $value; } ######################################################### # open SERVERS, ">>/tmp/homefries.db" or die "Hold on there buckaroo: $!\n"; my $boxen = { 'host' => "$FORM{hostess}", 'address'=> "$FORM{ipaddr}", 'funct' => "$FORM{funct}", }; my $sirinfo = nfreeze( $boxen ); $_ = unpack("H*", $sirinfo) ."\n"; print SERVERS $_; print "$boxen->{host}\n"; print "$boxen->{address}\n"; print "$boxen->{funct}\n"; # close SERVERS; exit;
My attemp at thawing datafile
Runtime error: Not an ARRAY reference at ./hashbrowns.pl line 11, <SERVERS> line 1 (#1)
#!/usr/bin/perl -w use strict; use diagnostics; use Storable qw( nfreeze thaw ); my ($server,$ipaddr,$funct,$boxen,@fields); open SERVERS, "/tmp/homefries.db" or die "Hold on there buckaroo: $!\n"; while (<SERVERS>) { my $frozen = pack "H*", $_; my $fields = thaw($frozen); my ($server,$ipaddr,$funct) = @$fields; } close SERVERS; exit;

Replies are listed 'Best First'.
(Ovid) Re: Storable: freeze/unfreeze
by Ovid (Cardinal) on Oct 15, 2001 at 21:51 UTC

    See the node use CGI or die;. You should not parse CGI form data by hand.

    1. print "Content-type:text/html\n\n";

      There should be a space between the colon and the t.

    2. read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});

      You don't check to see if the read was successful. Does the length match CONTENT_LENGTH?

    3. @pairs = split(/&/, $buffer);

      The semicolon is an alternate delimeter that will be supported more in the future. This will break your code.

    4. $value =~ s/\r//g; # remove hard returns
    5. $value =~ s/\cM//g; # delete ^M's

      Do you realize that \cM is the same as \r?

    6. $FORM{$name} = $value;

      Whoops! If we have multiple values, you've just overwritten them.

    You can replace all of that broken code with the following three lines:

    use CGI qw/ :cgi-lib :standard /; print header; my %FORM = Vars;

    Cheers,
    Ovid

    Vote for paco!

    Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

Re: Storable: freeze/unfreeze
by lestrrat (Deacon) on Oct 15, 2001 at 21:29 UTC

    Hmmm, rtfm :-) Below is from perldoc Storable...

        use Storable;
        store \%table, 'file';
        $hashref = retrieve('file');
    
        use Storable qw(nstore store_fd nstore_fd freeze thaw dclone);
        # Network order
        nstore \%table, 'file';
        $hashref = retrieve('file');   # There is NO nretrieve()
    
Re: Storable: freeze/unfreeze
by Maclir (Curate) on Oct 15, 2001 at 21:39 UTC
    I won't comment on your trying to "roll your own" CGI processing, ignoring the CGI.pm module. There have only been 1,000,000 posts on PM advising peoplenot to do this, so why add another?

    Regarding the use of storable, my understanding is the module works on references to a data structure. You may want to check out some examples in the Perl Cookbook (pages 388 onwards. Now I have not used the "freeze / thaw" functions, but basically the process works like this:

    my %complex_hash = (); # now stick stuff in the hash store (\%complex_hash, '/some/file/name'); # notice the use of the backslash, meaning you pass a reference # later on . . . $complex_hash_ref = retrieve('/some/file/name'); # or even %complex_hash = %( retrieve('/some/file/name');
    As I said, I dont know about nfreeze / thaw, but it may also require you pass a reference to the data structure, and thaw may return a reference.
see Revised Storable:
by david.jackson (Novice) on Oct 16, 2001 at 00:27 UTC
    I've posted a revised messages, please referring it. Thanks David