madtoperl has asked for the wisdom of the Perl Monks concerning the following question:
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: File Manipulation Issue
by McDarren (Abbot) on Jun 14, 2006 at 04:06 UTC | |
| [reply] |
|
Re: File Manipulation Issue
by GrandFather (Saint) on Jun 14, 2006 at 04:10 UTC | |
Super search for File::Tail (in the Match text containing field) for SoPW. Among meny other hit you will get Re^3: Reading from a file, Re: logging ftp log into mysql db... and Re: Reading from an appended file. DWIM is Perl's answer to Gödel | [reply] |
|
Re: File Manipulation Issue
by liverpole (Monsignor) on Jun 14, 2006 at 11:31 UTC | |
Here's a subroutine I use on occasion, called tail_closure. You call the subroutine with 2 arguments, the name of the file to tail, and an optional flag (0 = read from beginning, nonzero = read from end). The subroutine returns a closure which you can call with a value N, to get at most the next N lines (N defaults to 0 => no limit).
The main program is a test wrapper which calls the closure on a file "test.txt". In this case, the flag to the closure is zero, so it reads from the beginning of the file, and then prints the last 10 lines (the usual behavior of the "tail" program). Then it monitors the file each time $psub is invoked, grabbing up to 256 lines of text and displaying the lines. You can test the program by running it in one shell, and appending lines to test.txt in another shell. s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/ | [reply] [d/l] |