For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > May 2006 > Problem moving from Data::Dumper to YAML









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Problem moving from Data::Dumper to YAML
Rob Chanter

2006-05-30, 3:58 am

Hi List,

I'm having a little trouble with YAML. The current version of a script
uses Data::Dumper to keep running totals over multiple runs of the script
in a nested hash, as well as a couple of arrays to give other programs
hints about how to order the data (for, say, graphing). I'd very much like
to switch over to YAML for the serialising.

The quick question: some Googling and playing with YAML haven't shown me
how to export structures including their variable names. Do I need to know
in advance the order of the structures that I expect to find in the .yml
file when I Load them, and call it as

($aref1, $aref2, $hashref) = LoadFile(mydumpfile.yml)?

?

The gory details, using Data::Dumper as an example - the following code
does as I expect (real code, trimmed somewhat to fit):

#!/usr/bin/perl
use strict;
use warnings;
use Getopt::Std;
use Data::Dumper;

my ($logdate, $reject_text); # extracted from log line.
my (%log_counts); # accumulator
my (@dispositions, @reasons); # data series
our ($opt_v, $opt_f); # command line options

# v for verbose, f to specify an accumulator file
getopts('vf:');

if ($opt_f) {
open STATEFILE, "<$opt_f";
local $/; #slurp
eval <STATEFILE> or die "couldn't eval state file $opt_f: $@\n";
close STATEFILE;
}

# overwrite the sequence hints arrays
@dispositions = qw(total_rejects redirects viruses spam clean);
@reasons = qw(xbl-sbl-client dsbl-client bad-helo forged-sender
unknown-sender bad-recipient relay-denied rejected-other);

#
# Code goes here to populate %log_counts
#

# If we read from a statefile, then write back to it.
if ($opt_f) {
$Data::Dumper::Sortkeys = 1;
$Data::Dumper::Purity = 1;
open STATEFILE, ">$opt_f" or die "Couldn't open $opt_f for writing: $!\n";
print STATEFILE Data::Dumper->Dump([\@dispositions, \@reasons, \%log_counts],
[qw(*dispositions *reasons *log_counts)]);
close STATEFILE;
}

__END__

Now, according to the documentation, YAML::Dump() "works very much like
Data::Dumper::Dumper()", and should pretty much be a drop-in replacement.

When I run it using the YAML version of Dump, I get a yml file like this:

----snip--8<--
---
-
- total_rejects
- redirects
- viruses
- spam
- clean
-
- xbl-sbl-client
- dsbl-client
- bad-helo
- forged-sender
- unknown-sender
- bad-recipient
- relay-denied
- Mar 28:
clean: 69121
deferred: 4
redirects: 89
rejects:
bad-helo: 5572
bad-recipient: 8653
dsbl-client: 1050
forged-sender: 389
unknown-sender: 4146
xbl-sbl-client: 24118
spam: 13805
total_rejects: 44489
viruses: 154
---
- '*dispositions'
- '*reasons'
- '*log_counts'
----snip--8<--

When I try to read it back in using YAML::LoadFile(), none of the
structures are populated. What am I doing wrong?

TIA
rob
Brad Baxter

2006-05-31, 7:00 pm

Rob Chanter wrote:
> Hi List,
>
> I'm having a little trouble with YAML. The current version of a script
> uses Data::Dumper to keep running totals over multiple runs of the script
> in a nested hash, as well as a couple of arrays to give other programs
> hints about how to order the data (for, say, graphing). I'd very much like
> to switch over to YAML for the serialising.
>

[snip]
> Now, according to the documentation, YAML::Dump() "works very much like
> Data::Dumper::Dumper()", and should pretty much be a drop-in replacement.

[snip]

I guess the answer is that it "works very much like
Data::Dumper::Dumper()" as the YAML docs say,
but is not in fact a "drop-in replacement", which the
docs do not claim. It would appear that not all of the
parameters that Dumper() supports are supported
by Dump().

--
Brad

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com