Its interesting to see myself go from reading books that teach perl that is poor form (never mentioning -w or strict) to hearing how a monk should really be putting the digital plume to scripture.
I think I am finally understanding the use of seek(); and why just copying and pasting it from another's example didnt work.

My code looked like:

open FILE, "$filename" or die $!; flock(FILE,2); seek(FILE,0,2); @entries = <FILE>; flock(FILE,8);

Yet when I went to view the contents of @entries, it was empty. I scratched my head for hours on this one since all the examples of seek I had seen seemed to call it with (FILEHANDLE,0,2). Finally, I came to some sort of enlightenment thinking that since the whence portion of the seek statement was set to 2, the @entries list is only reading from the end of the FILEHANDLE on. When I changed the entry to seek(FILEHANDLE,0,0) the outcome was more what I expected.

So, I guess I am looking for someone with more experienced robes than mine to give me the nod that this logic is correct and the moral of "never blindly copy another's code" fits the bill.

humbly -c

Replies are listed 'Best First'.
Re: seek(UNDERSTANDING);
by runrig (Abbot) on Aug 06, 2001 at 21:14 UTC
    If you want to lock a file that you're going to write to, you'll want to seek to the end of it, because someone may have written more data while you were waiting for the lock. But if you're just going to read from it, then there's no need to seek to the beginning; you're already there.
Re: seek(UNDERSTANDING);
by tadman (Prior) on Aug 10, 2001 at 05:25 UTC
    When learning any language, Perl or otherwise, it is vital to have a handy desk reference. I have found Programming Perl to be the book of choice for me, and many others, as it provides a function-by-function breakdown of what stuff does. A good portion of this is also available in 'perldoc', if you happen to have access to that. As in, 'perldoc -f seek', which would return:
    seek FILEHANDLE,POSITION,WHENCE

    Sets FILEHANDLE's position, just like the fseek call of stdio. FILEHANDLE may be an expression whose value gives the name of the filehandle. The values for WHENCE are 0 to set the new position to POSITION, 1 to set it to the current position plus POSITION, and 2 to set it to EOF plus POSITION (typically negative). For WHENCE you may use the constants SEEK_SET, SEEK_CUR, and SEEK_END (start of the file, current position, end of the file) from the Fcntl module. Returns 1 upon success, 0 otherwise.
    The moral of the story is if you ever stumble across something you "don't understand", don't just paste it in there, read up on it until you have a handle on what it does, at least in theory.

    Unlike, say, advanced particle physics where there is an element of the unknown, Perl is very well documented and there is little left to mystery. Cargo Cult Programming is not a productive way to learn.