Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re^2: sysread and null characters

by ggg (Scribe)
on Mar 24, 2005 at 15:58 UTC ( [id://442090]=note: print w/replies, xml ) Need Help??


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

Your example #4 is what I had tried first, but without the ".*" parts. I'm still not sure why they're needed. What I thought I was asking for was a match with any digit no matter where it was in the string. My bad.

At this point, after adding the ".*", it's doing just what I expected. Hurray!

As to the need for ++$i; don't I need to change the offset each time I read the file in order to step further into the file to get the next digit? You seem to be implying that that's unneeded. Does sysread auto-increment it's own offset when it's used in a loop? If I just remove the ++, I get a runaway loop, so I guess not. What did you have in mind, please?

I could leave it just the way it is, but I'd rather understand sysread a little better.

ggg

Replies are listed 'Best First'.
Re^3: sysread and null characters
by Tanktalus (Canon) on Mar 24, 2005 at 16:31 UTC

    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.

      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; }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://442090]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (6)
As of 2024-04-25 11:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found