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

Thanks all, you point about use of CGI is well taken.

Now can we get back to the *central* question storing and retrieving data using Storable module. Attached is my revised code (less GGI ) and the example (as a starting point) from,"Programming the Perl DBI" Chapter 2, pages 25-28.

Keeping in mind that there is more than one path to "Perl enlightenment", but only one path to true englightenment.

Objective: Use Perl write; to verify data is being stored correctly.

First my code:

#!/usr/bin/perl -w use strict; use diagnostics; use Storable qw( nfreeze thaw ); die "USAGE: hashbrowns.pl <server></server> <ipaddress> <function>\n" unless @ARGV == 3; my $server = $ARGV[0]; my $ipaddr = $ARGV[1]; my $funct = $ARGV[2]; # open SERVERS, ">>homefries.db" or die "Hold on there buckaroo: $!\n"; my $boxen = { 'host' => "$server", 'address'=> "$ipaddr", 'funct' => "$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;
=========================

From Perl DBI book:

#!/usr/bin/perl -w # # ch02/marshal/update_storable: Updates the given megalith data file # for a given site. Uses Storable data # and updates the map reference field. use Storable qw( nfreeze thaw ); die "Usage: updatemegadata <data file> <site name> <new map reference> +\n" unless @ARGV == 3; my $megalithFile = $ARGV[0]; my $siteName = $ARGV[1]; my $siteMapRef = $ARGV[2]; my $tempFile = "tmp.$$"; open MEGADATA, "<$megalithFile" or die "Can't open $megalithFile: $!\n"; open TMPMEGADATA, ">$tempFile" or die "Can't open temporary file $tempFile: $!\n"; while ( <MEGADATA> ) { my $frozen = pack "H*", $_; my $fields = thaw( $frozen ); my ( $name, $location, $mapref, $type, $description ) = @$fields; next unless $siteName eq $name; $fields = [ $name, $location, $siteMapRef, $type, $description ]; $frozen = nfreeze( $fields ); $_ = unpack( "H*", $frozen ) . "\n"; } continue { print TMPMEGADATA $_ or die "Error writing $tempFile: $!\n"; } close MEGADATA; close TMPMEGADATA or die "Error closing $tempFile: $!\n"; unlink $megalithFile or die "Can't delete old $megalithFile: $!\n"; rename $tempFile, $megalithFile or die "Can't rename '$tempFile' to '$megalithFile': $!\n"; exit 0;

Edit Petruchio Tue Oct 16 10:56:13 UTC 2001 - removed PRE tags, adjusted markup slightly.

Replies are listed 'Best First'.
Re: Revised: Storable data retrieval
by blakem (Monsignor) on Oct 16, 2001 at 00:33 UTC
      Blake -- I didn't link back to previous discussion, as is pretty clear if you read my original post it's about using the "Storeable" module, NOT about CGI. Yet 3/4 of the people who repsonded BITCHED about my "home rolled" CGI. I posted a news "node", in order to allow us to refocus on the central issue which as you know is using the "Storeable Module" as it applies. to the code attached above.
        Calm down, man... I was trying to help you get an answer to your question. Double posted questions (whether exact copies, or simple restatements) show up often enough that a lot of us simply skip over them the second time around.... This is especially tempting to do if there isn't an easy way jump back to the old question for comparison.

        In otherwords, when I read your question the second time around, the overwhelming temptation was to move on as soon as I determined it was a.) a continuation of a previous question, and b.) lacking a link to said question.

        The other option was for me to track down the link you should have provided and politely point out its absence. This helps keep others from skipping the node simply because following the thread is too much work.

        That said... perhaps the reason CGI was such a focus in your initial node was because you hadn't narrowed your code down to a base case. If you want an answer about X, winnow your code down to a tiny snippet that illistrates the X issue. You'll get far more focused answers that way, and it keeps us from wasting our time offering you advice you don't seem to want.

        Finally, complaning in your own thread does nothing but divert time and energy away from helping you find the answer you want. You have now successfully used up the time I would have spent seeking an answer for you.

        Time spent framing good questions is time very well spent.

        -Blake

Re: Revised: Storable data retrieval
by lestrrat (Deacon) on Oct 16, 2001 at 07:05 UTC

    Oh I think I missed the problem in your earlier post... I was confused as to why on earth you would want to unpack() the data you just froze...

    In any case your original question stated: "How do I thaw and retrieve data from a 'Sorable file'". Well, you use store() and retrieve(). That's why I replied to rtfm, cause it looked like you were trying to thaw it with some pack()/unpack() magic...

    But upon examining your code, I see that it has nothing to do with Storable, but a simple error...

    In your original post, you freeze a ref to a hash, and then when you thaw, you try to dereference it to an array.... that's all there is to it.

    In any case, I don't think you really should be doing this pack unpack stuff unless there's some reason you just cannot use store/nstore and retrieve


    And you might want to lose the atitude when somebody is pointing an obvious flaw in your code, even if that doesn't solve your problems immediately. In the long run, it will be better if you listen to these details.