Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

Dominus, you have again created a GREAT module, and I will surely put it to use very often.

A string cannot be used to store data structures, unless serialized. It would however be great to use this efficient Tie::File together with array or hash references. Because putting the serialization in Tie::File would ruin all non-serializing operation, I thought it would be nice to tie an array and have automatic serialization. I have searched CPAN, but couldn't find a module that does what I want, so I created this quick hack:

package Tie::FreezeThaw; # NOTE: # This is a quick hack and has NOT been tested thoroughly! # NOTE: # You can't use the elements directly as references! # (if @xyzzy is tied, $xyzzy[1][2] won't work. # Use $foo = $xyzzy[1]; $foo->[2] instead.) use FreezeThaw qw(freeze thaw); use base 'Tie::Array'; use strict; sub TIEARRAY { bless $_[1], $_[0] } sub FETCHSIZE { scalar @{ $_[0] } } sub STORESIZE { @{ $_[0] } = $_[1] } sub EXISTS { exists $_[0]->[$_[1]] } sub DELETE { delete $_[0]->[$_[1]] } sub STORE { ($_[2] = freeze $_[2]) =~ s/([^\x20-\x7E])/sprintf "\xFF%02x", $1/ +ge; $_[0]->[$_[1]] = $_[2]; } sub FETCH { (my $foo = $_[0]->[$_[1]]) =~ s/\xFF(..)/chr hex $1/ge; return (thaw $foo)[0]; } 1;
Which allows me to use Tie::File with complexer data structures (of course, entire records will be overwritten, but that's still more efficient than re-writing the entire file) without having to think about the serialization.

use Tie::File; use Tie::FreezeThaw; use strict; tie my @foo, 'Tie::File', 'testfile' or die $!; tie my @bar, 'Tie::FreezeThaw', \@foo; push @bar, [ qw/1..10/ ];
(If there's already a module like my quick Tie::FreezeThaw hack, please let me know)

U28geW91IGNhbiBhbGwgcm90MTMgY
W5kIHBhY2soKS4gQnV0IGRvIHlvdS
ByZWNvZ25pc2UgQmFzZTY0IHdoZW4
geW91IHNlZSBpdD8gIC0tIEp1ZXJk


In reply to Re: How do I insert a line into a file? by Juerd
in thread How do I insert a line into a file? by Dominus

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (4)
As of 2024-04-18 05:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found