I don't think tying is going to help you the way you hope it will.

You're still going to have to write the methods or subs that open the text files and search through and aggregate your data (which is what you were trying to avoid.) All tying does is give you a nice, clean, familiar interface to use (although nothing you can't do outside of tying.) In other words, if you tie a hash, you'd be able to access your data like

tie %hash, 'MyHashClass', @files; my @data = $hash{$key}; # convienent!

but somewhere in the background you'll still need something like

package MyHashClass; use strict; use Carp; use Tie::Hash; @MyHashClass::ISA = qw/ Tie::StdHash /; sub FETCH { my ($self, $key) = @_; my @ret = (); foreach my $filename ( @files ) { open( F, $filename ) or croak "$!"; while( <F> ) { if ( matches ) { push @ret, $_; } } close F; } return @ret; } 1;

Hmm. Actually this is horribly inefficient and you wouldn't want to do it this way. You'd probably open all of the files when the hash is tied or something.

But I think I'm getting off track. I guess my point is that tying isn't going to save you any work.

If you have a lot of data or you're going to have to search through it very often, it's worth combining and/or sorting your files (or converting to a database like Berkeley DB (DB_File)) so you can search them more efficiently. Otherwise doing what you say (open each one, pull the appropriate row, etc.) will work.


In reply to Re: Re: Re: Tie::RefHash by eg
in thread Tie::RefHash by malaga

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.