Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks, I would like to know a value of certain variable in the last record of xml file. Is it possible to read the file from the end and check newly red lines for regex? Of course it could be possible to read the whole file and then check with regex told that the string should be at the end. But the file is rather big. Tx.
  • Comment on reading file from the end till some value is found

Replies are listed 'Best First'.
Re: reading file from the end till some value is found
by jrtayloriv (Pilgrim) on Feb 19, 2008 at 13:48 UTC
    File::Tail will let you grab just the last part of the file. Post up a snippet of the XML, and what you are trying to do, and maybe someone could help you, if you can't figure it out from here.
Re: reading file from the end till some value is found
by pc88mxer (Vicar) on Feb 19, 2008 at 15:09 UTC
    Check out LMX. It's a reverse XML parser. It parses an XML file in reverse, and it seems like what you are looking for.

    AFIAK, there isn't a perl interface. And it requires MacOS. The author says that the next version will be cross-platform, however.

Re: reading file from the end till some value is found
by svenXY (Deacon) on Feb 19, 2008 at 13:36 UTC
    Hi,
    Please read How (Not) To Ask A Question, especially the part about posting the code that you already tried yourself.
    Regards,
    svenXY
      Hi. Sorry, there was no try on my part, it was just begginers question if there is a command telling perl to start reading from the end of the file (and to my surprise googling didnt help). The answers below are appreciated (unfortunately i dont have mac). I found yet another package File::ReadBackwards and whe trying I bumped into a surprising problem: when I read xml file it is broken into individual letters instead of lines. And when I tried standard procedure
      open(DATAFILE, $file); while (defined ($line = <DATAFILE>)) { chomp $line; print $line } close(DATAFILE);
      and got the same letter soup. What is wrong? I tried to redefine end of line by setting local $/ as "something at the beggining of the file" and something not in the file but it doesnt helped (and did similar in File::ReadBackwards->new( $filename_in,"something as above" ))). Please help.