SPUG: Data::Dumper for an entire package?

rick.croote at philips.com rick.croote at philips.com
Thu Jul 12 13:37:20 CDT 2001


Look at XML::Dumper.  It allows you to dump a data type to a file of xml format and restore the data back to the original form.  I've done this with very large hashes, and it sure makes the job very easy, but as far as a whole package, I don't know.
First, I never use a package other than an object orientated fashion, and now days limit that to a hash, which should dump nicely, but you may have problems with the fact that the hash is blessed.  However, one could copy the blessed hash to a non-blessed
hash, save it to the xml file, then allow the object to initialize itself from the xml file, or something like that.

Have Fun,
Rick Croote

Here is a working example I threw together:

#!/usr/bin/perl -w

use strict;
use FileHandle;
use XML::DUMPER;
use Data::Dumper;

use constant objectFile => 'myObject.xml';

{
    #
    # Just some bogus data for now.
    #
    my $filedata = {
        aScalar => 1,
        aArray  => ['a', 'b'],
        aHash   => {key => 'value'}
        };

    print Dumper("Before", $filedata);
    #
    # Save it.
    #
    saveObject($filedata);

    #
    # Restore it.
    #
    $filedata = restoreObject();
    print Dumper("After", $filedata);
}

sub saveObject
{
    my ($filedata) = @_;

    my $fh = new FileHandle '>' . objectFile;
    if (defined $fh)
    {
        my $xmldump = new XML::Dumper;
        print $fh $xmldump->pl2xml($filedata);
    }
}

sub restoreObject
{
    my $parser = new XML::Parser(Style => 'Tree');
    my $xmldump = new XML::Dumper;
    my $doc = $parser->parsefile(objectFile);
    return $xmldump->xml2pl($doc);
}



 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     POST TO: spug-list at pm.org       PROBLEMS: owner-spug-list at pm.org
      Subscriptions; Email to majordomo at pm.org:  ACTION  LIST  EMAIL
  Replace ACTION by subscribe or unsubscribe, EMAIL by your Email-address
 For daily traffic, use spug-list for LIST ;  for weekly, spug-list-digest
  Seattle Perl Users Group (SPUG) Home Page: http://www.halcyon.com/spug/





More information about the spug-list mailing list