Hi all,

I'm trying to write a kind of wrapper for Tie::File to allow it to manage different data formats (not only lines, or simple paragraphs). The idea is to access data records from a file as if they were in an array.

So far, the module looks like this:

package Tie::File::AnyData; use strict; use warnings; use Tie::File; sub TIEARRAY { my ($pack,$file,%opts) = @_; if (defined $opts{'code'}){ my $code = $opts{'code'}; delete $opts{'code'}; *Tie::File::get_next_rec = $code; no warnings 'redefine'; *Tie::File::_read_record = sub { my ($self) = @_; my $rec; $rec = Tie::File::get_next_rec ( $self->{'fh'} ); return $rec; } } Tie::File::TIEARRAY("Tie::File",$file,%opts); } 1;

The idea is to pass an anonymous subroutine that is able to retrieve the data from the file one record by one. Then, we redefine the original _read_record subroutine (that is present in Tie::File) to use this code.

This module can be used, for example, in the following way:

use Tie::File::AnyData; my $coderef = sub{ ## Code to retrieve one by one the records of a given format }; tie my @data, 'Tie::File::AnyData', $file, code => $coderef; ## Use the tied array

The code seems to work ok (as far as my tests can tell), but has one big limitation: you can not work with 2 data formats at the same time, because a second call to "tie" will override the get_next_rec subroutine:

my $format1 = sub { ## Subroutine that reads records with format 1 } my $format2 = sub { ## Subroutine that reads records with format 2 } tie my @dataf1, 'Tie::File::AnyData', $file_format1, code => $format1; + ## Creates get_next_rec and redefines _read_record tie my @dataf2, 'Tie::File::AnyData', $file_format2, code => $format2; + ## Re-defines get_next_rec!!

I think that this can be solved with some kind of polymorphism, but I don't know exactly how. Any suggestion in solving this would be highly appreciated

Thanks in advance!

citromatik


In reply to dynamic polymorphism by citromatik

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.