Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

comment on

( [id://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.

-xdg

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

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 having a coffee break in the Monastery: (4)
As of 2024-03-29 15:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found