On Tuesday 11 December 2007 10:22, [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,
man 2 lseek
[ SNIP ]
NOTES
This document's use of whence is incorrect English, but
maintained for historical reasons.
> 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.
This shows the influence that the C programming language has on Perl.
for (;;) { ... } is used in C for an infinite loop. In Perl you could
also write that as while (1) { ... }.
> 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.
Perl programmers usually frown on the use of CamelCase variable names.
> 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 ...)
Code like this is probably refined over many years of programmers and
sysadmins hacking on many different versions of Unix/Linux so although
it may not make much sense to beginners it will probably work in most
cases.
> 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.
>
> It left me thinking something like this should work but it absolutely
> fails to print tell() from seek(FILE, -($bytes -100) ,2) position.
I had to look up what '2' meant in this context. It usually means
SEEK_END but it might mean something different on your system so you
should use the seek constants in the Fcntl module. Also $bytes starts
out at 0 so the expression -($bytes -100) will be equal to +100 which
is 100 bytes past the end of the file. You probably want to do this
instead:
use Fcntl ':seek';
seek FILE, -100, SEEK_END or die "Cannot seek on './myfile' $!";
> 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);
John
--
use Perl;
program
fulfillment
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/