On Dec 11, 2007 1:22 PM,  <[EMAIL PROTECTED]> wrote:
> Using perldoc -q tail
> leading to
> perldoc -f seek
> perldoc -f tell
>
> I'm not getting how to use those functions.  Partly because what
> passes for examples in those docs doesn't use normal language, instead
> they use terms like WHENCE, something that's almost never used in
> normal language.  When WHERE would get the point across at a glance
> instead of having to dig into the details,
snip

Whence is actually proper English.  You may not here it often, but
that has more to do with falling standards of education.  Using WHERE
instead of WHENCE may be easier for you, but, since this function is
exposing an underlying POSIX function that uses WHENCE to describe
that parameter, it would be confusing to those who already have some
exposure to programming (in particular ANSI C).  The docs are made for
reference not teaching, if you feel the need to be taught you should
invest in the O'Reilly series of books.  You should find the Llama
book (Learning Perl) particularly helpful.

snip
>
> At first I took it to mean something more involved than giving a
> possition.
> No biggee I guess  but then I see:
>
>                    for (;;) {
>                        for ($curpos = tell(FILE); $_ = <FILE>;
>                             $curpos = tell(FILE)) {
>                            # search for some stuff and put it into files
>                        }
>                        sleep($for_a_while);
>                        seek(FILE, $curpos, 0);
>                    }
>
> Even here what the heck does `;;' mean.  This stuff is supposed to be
> readable by someone who doesn't know these things.  Even down to
> `curpos'.  I didn't get what it meant for a few seconds.  Why not
> spell it out... $CurrentPostion.  After all clarity is what we're
> after here.
>
> Again no biggee I guess,
>
> However, I still don't see how it is supposed to work.  Is there a law
> against simple examples? hehe.
>  (ok enough complaining ...)
>
> seek documentation indicates the for loop probably won't be necessary
> unless the IO implementation is `particularly cantankerous'.  So I'm
> guessing there is some easier way to access the stuff below where I've
> told the interpreter to seek to.
snip

That is a fairly straightforward implementation of tail -f.  It does
use the C-style for loop in a way that one doesn't often use in Perl,
but there is nothing really odd about it.  Perhaps you will understand
this a little better:

my $pos = tell FILE;
while (1) {
    while (<FILE>) {
       $pos = tell FILE;
       #do something with each new line
    }
    sleep $timeout;
    seek(FILE, $pos, 0);
}

snip
> It left me thinking something like this should work but it absolutely
> fails to print tell() from seek(FILE, -($bytes -100) ,2) position.
>
> use strict;
> use warnings;
>
> my $bytes;
> open(FILE,">>./myfile")or die " Can't open ./myfile: $!";
>      $bytes = tell(FILE);
>    print "hpdb pre seek bytes <$bytes>  \n";
> print FILE "line\nline\nline\nline\n";
>
> ## go back to 100 bytes before previous end of file.
> seek(FILE, -($bytes -100) ,2);
> while(<FILE>){
>    print "hpdb tell by line:" . tell(FILE) . "\n";
> }
> close(FILE);
snip

That is because you are telling Perl to seek to position -($bytes
-100) from the end of the file.  That is most likely not the place you
want given the comment.  You want either

#seek to a position 100 bytes before position $bytes
seek FILE, $bytes - 100, 0;

or

#seek -120 bytes from the end of the file (this should be -125 on
Win32, due to the CR)
seek FILE, -120, 2;

The seek function has three ways of measuring what the second argument means:
0: move relative the beginning of the file
1: move relative to the current position in the file
2: move relative to the end of the file

So if you want to rewind twenty bytes you can say

seek FILE, -20, 1;

if you want to hop forward twenty bytes you can say

seek FILE, 20, 1;

if you want to move to after the 100th byte of the file you can say

seek FILE, 100, 0;

If you want to move back two hundred bytes from end of the file you can say

seek FILE, -200, 2;

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to