Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: How to make EOF?

by samtregar (Abbot)
on Mar 04, 2009 at 06:35 UTC ( #748009=note: print w/ replies, xml ) Need Help??


in reply to How to make EOF?

There isn't an end-of-file character, per-se. You can cause the other end of a socket to detect an EOF by closing the socket. When the other end finishes reading what you wrote eof(SOCKET) will return true.

If you need the other end to read multiple files then you'll need to rely on some convention. Personally I prefer to prefix potentially long transmissions with the size of the transmission rather than relying a special character which could someday appear by accident in the data stream. For example on the sending side:

  print WRITER length($yaml), "\n", $yaml;

Then the reader can do:

my $length = <READER>; chomp($length); my $buffer = ""; while(length $buffer < $length and not eof(READER)) { read(READER, $buffer, 4048, length $buffer); } # do something with the YAML data in buffer...

Possibly interesting side-note - I was just reading the memcached protocol spec and this is exactly how it works too.

-sam


Comment on Re: How to make EOF?
Select or Download Code
Re^2: How to make EOF?
by roboticus (Canon) on Mar 04, 2009 at 21:21 UTC
    samtregar:

    Minor (historical) detail: In fact, some operating systems *do* use an EOL marker in files. For example, in CP/M, the directory entries tell you which sectors hold data, but not how many bytes are in your files. So text files would use a ^Z to mark the end of the document. That carried over into DOS, so in DOS programs you'll often find some ^Z characters in files.

    Thank goodness we no longer have to deal with *that* mess!

    ...roboticus

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (7)
As of 2014-10-02 04:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    What is your favourite meta-syntactic variable name?














    Results (48 votes), past polls