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: [EMAIL 
PROTECTED]";
    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([EMAIL PROTECTED], [EMAIL PROTECTED], 
\%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

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to