in reply to Re^2: sysread and null characters
in thread sysread and null characters

Ok, maybe I should have been a wee bit more explicit on the sysread part. What you want is:

sysread DF, $digit, $c;
The fourth parameter to sysread is called "offset". But it's not the offset into the file, it's the offset into the buffer (in your case, $digit). All file-reading functions read from the "current file position". Always. You can change the "current file position" on physical files (using seek), but not on all filehandles (e.g., a pipe from another process, or a socket). The act of reading from (or writing to) a file handle implicitly advances the position.

The purpose of the offset in sysread, then, is to automatically concatenate multiple reads in a single buffer. This is not what you're doing.

The reason for the .* parts in a substitution (s///) operator is to have the regular expression match the whole string. This way you're replacing the whole string rather than just replacing the digit (with itself). You may want to peruse the Regular expression tutorial and/or the regular expression reference for more info here.

Replies are listed 'Best First'.
Re^4: sysread and null characters
by ggg (Scribe) on Mar 24, 2005 at 17:18 UTC
    Congratulations on Sainthood, Tanktalus!

    Your explanation of sysread clarifies some other puzzeling behaviors I was seeing but didn't mention. Thanks!

    I've bookmarked your links to regex info - your explaination gave me a cross between Do'h and deja vu.

    One more thing, if you don't mind. The sysread info on perldoc.perl.org says Use sysread() and check for a return value for 0 to decide whether you're done. I'd like to use that feature to end the while loop rather than incrementing a counter, but is that return value zero? If so, how would I tell the difference from a zero in the data?

    Or is that talking about some test value the function itself returns? ($EndTest = systead( FH, $a, $b ))

    ggg

      The idea is that sysread is not returning the data, but a simple code that says "success" or "failure". The data is stored in your buffer ($digit).

      my $i; while (sysread FH, $digit, 1) # same as: while (sysread(FH, $digit, 1) != 0) # (well, not quite, but close enough here.) { ++$i; # keep track of which digit we're looking at. # deal with the $digit you just got. printf "%04d %d\n", $i, $digit; }

        Thank you, Tanktalus! I really appreciate your time on this. It surprises me how much was left out of the perldoc description of this function.

        Again, thanks!

        ggg