Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
This would probably be a lot more useful if it were implemented as a subclass of Tie::File.

Actually, this seems like a job for inside-out objects. I just uploaded to CPAN a new, quick-and-dirty version of File::Marker (0.11) that adds the ability to save/load markers from an external file. It works similarly to what you've described, though you have to do the line-numbering work yourself. On the flip side, it handles the seeking for you and works like an IO::File object (which it subclasses).

Creating the index:

use File::Marker; my $file = File::Marker->new( $filename ); # make the (zero-based) index my $i = 0; while ( ! $file->eof ) { $file->set_marker($i++); <$file>; } # save and rewind $file->save_markers( $index_filename ); $file->goto_marker('LAST'); # rewind

Re-use the index

use File::Marker; my $file = File::Marker->new( $filename ); $file->load_markers( $index_filename ); $file->goto_marker( 22 ); # start of 23rd line

You could probably extend it to focus on line-numbers instead of marking any type of position in a file or add the various special features people have mentioned. Alternatively -- perhaps even better -- would be to reimplement it using Class::InsideOut, since File::Marker is only really intended to be a teaching module. If I get around to figuring out how to sanely and safely serialize IO::File based inside-out objects with Storable, this could be even easier.


Code written by xdg and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.

In reply to Re: Proof of concept: File::Index by xdg
in thread Proof of concept: File::Index by davido

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, 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, 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.
  • Log In?

    What's my password?
    Create A New User
    [marto]: good morning all
    [Corion]: Hi marto!

    How do I use this? | Other CB clients
    Other Users?
    Others musing on the Monastery: (9)
    As of 2018-05-24 07:18 GMT
    Find Nodes?
      Voting Booth?