I'm trying to tie a large hash (like: $db{userdata}->{username}->{password}) to a file and have it save in Perl code format. This allows me to simply require it to load my hash. It's current saving (rewriting) what's already in the hash but isn't adding new values or modifying old values. I'd like to stay away from modules for the tie part of this too... if it's possible.

I'm currently using the code (sorry for it's length):

# Tying code: tie(%db, wpconfig); ...do stuff with %db :)... untie(%db) if %db; # Tie stuff: package wpconfig; sub TIEHASH { my $class = shift; require 'wpconfig.pl'; # loads data into %db bless \%db, $class; } sub STORE { my ($self, $key, $value) = @_; if (ref $value) { while (($ikey, $ivalue) = each % $value) { if (ref $ivalue) { while (($iikey, $iivalue) = each % $ivalue) { $self->{$key}->{$ikey}->{$iikey} = $iivalue; } } else { $self->{$key}->{$ikey} = $ivalue; } } } else { $self->{$key} = $value; } saveConfig($self); return $value; } sub FETCH { my $self = shift; $self->{$_[1]}; } sub FIRSTKEY { } sub NEXTKEY { } sub EXISTS { } sub DELETE { } sub CLEAR { } sub DESTROY { saveConfig($_[0]); } # saveConfig: This is what actually saves wpconfig.cgi. sub saveConfig { my $self = shift; open GOO, '>wpconfig.pl'; print {GOO} main::genHash(db, \%$self); print GOO "\n1;"; close GOO; } package main; # genHash: What generates the Perl code for my hashes. This works fine +... though if someone wants to explain how to use data dumper to do t +his I'd change it :) sub genHash { my ($name, $hash) = @_; my (@lines, @ilines, @iilines, $entry); $code = "\%$name = (\n"; while (($key, $value) = each %$hash) { $entry = "'$key' => "; if (ref $value) { @ilines = (); $entry .= '{'; while (($ikey, $ivalue) = each %$value) { if (ref $ivalue) { $entry .= "'$ikey' => "; @iilines = (); $entry .= '{'; while (($iikey, $iivalue) = each %$ivalue) { push @iilines, "'$iikey' => q~" . safeTilde($i +ivalue) . '~'; } $entry .= join ",\n", @iilines; $entry .= '}'; } else { push @ilines, "'$ikey' => q~" . safeTilde($ivalue) + . '~'; } } $entry .= join ",\n", @ilines; $entry .= '}'; } else { $entry .= 'q~' . safeTilde($value) . '~'; } push @lines, $entry; } $code .= join ",\n", @lines; return "$code\n);\n"; } # safeTilde: Escapes a ~ (tilde). sub safeTilde { my $code = shift; $code =~ s/\~/\\~/g; return $code; }

In reply to Tying big hashes by L0rdPhi1

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.