Two tested simple subroutines which allow the simple capture and restoration of arbitrary complex data structures (especially multi-dimensional hashes) from/to file(s) in a single step. Initial concept by mikfire. &CaptureStructure($rComplexDataStructure, $FileToWrite); &RestoreStructure($rComplexDataStructure, $FileToRead);
# Usage example follows subroutines! # ------------------------------ # CaptureStructure -- subroutine STRICT # ------------------------------ # Author: rhardy@webcon.net # This function will capture a complex data structure (including # multi-dimensional hashes) and dump it to the specified file. # Function returns the datatype on success for easy error detection: # i.e. &CaptureStructure or die; # Syntax: &CaptureStructure($rComplexDataStructure, $FileToWrite); sub CaptureStructure { my $Donor = shift; my $File = shift; my ( $DNA, $Clone, $Length, $Offset, $Written, $RefType); $RefType=ref($Donor); local $Data::Dumper::Indent = 0; # It will be eval'd, we don't nee +d pretty local $Data::Dumper::Purity = 1; # make sure all code is present local $Data::Dumper::Terse = 1; # makes the eval work well local $Data::Dumper::Deepcopy = 1; # tell Dumper our intent local $Data::Dumper::QuoteKeys = 1; # Insist on having our keys quot +ed or else some multidimensional arrays fail. # Interestingly enough, Dumper will embed the call to bless in the D +NA. # Which makes this whole analogy really quite accurate and somehow s +trange # WARNING: As of V2.101 the Dumper function does NOT properly quote +hash # keys even when told to. DumperX works fine and is much faster. Aut +hor of # Dumper has been notified. $DNA = DumperX( $Donor ); # The data structure in $Donor has been stringified into $DNA. $Length = length($DNA); open(TO, "> $File"); $Offset = 0; while ($Length) { # Handle partial writes. $Written = syswrite TO, $DNA, $Length, $Offset; (! defined $Written ) and die "System write error: $!\n"; $Length -= $Written; $Offset += $Written; } close(TO); return $RefType; } # ------------------------------ # RestoreStructure -- subroutine STRICT # ------------------------------ # Author: rhardy@webcon.net # # This function will read a specified file containing a captured struc +ture, # restore it to a specified structure of the same type and return whet +her is # succeeded or not. This function works with almost any complex data # structure (including multi-dimensional hashes). # Function returns the data type on success for easy error detection: # i.e. &RestoreStructure or die; # Syntax: &RestoreStructure($rComplexDataStructure, $FileToRead); sub RestoreStructure { my $Clone = shift; my $File = shift; my ( $DNA ); local $_; # Must read in the file open(FROM, "< $File") or die "Error reading File $File: $!\n"; $DNA=<FROM>; close(FROM); # Find out what we are restoring and then use $DNA to clone original + structure $_=ref($Clone); if ($_ eq "HASH") { %{$Clone} = %{eval $DNA}; $@ and die("RestoreStructure Eval ERROR:$@\n"); return $_; } if ($_ eq "ARRAY") { @{$Clone} = @{eval $DNA}; $@ and die("RestoreStructure Eval ERROR:$@\n"); return $_; } if ($_ eq "SCALAR") { ${$Clone} = ${eval $DNA}; $@ and die("RestoreStructure Eval ERROR:$@\n"); return $_; } (not ref $Clone) and die("Error: Undefined Reference!\n"); # Unrecognized data we simply return an error. CODE and GLOB fall he +re for now. return 0; } # Usage Example: # In a subroutine which builds you a multidimesional hash: # skip generating the hash from scratch if it exists # on disk from a previous session. if ( -f "$DataFile" ) { $RefType=RestoreStructure(\%::WordsLength, "$DataFile") or die"ERROR: can't retrieve WordsLength!\n"; print "Using $DataFile; Delete to rebuild WordsLength $RefType\.\n"; return 0; } # Save a copy of this WordsLength for later use $RefType=CaptureStructure(\%::WordsLength, "$DataFile"); ($@) and (die "Error writing to file: $@");

In reply to CaptureStructure and RestoreStructure by rhardy

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.