pileofrogs has asked for the wisdom of the Perl Monks concerning the following question:

Greetings Monks!

Short Version:How do I create an End of File character?

Long VersionI'm sending some YAML over a socket and I'm trying to figure out the best way to delimit the end of the YAML stream. I'm thinking of setting $/ to EOF on the reading end and sending an EOF after my YAML stream from the sending end. But, I don't know how to generate an EOF to send. I think EOF has the integer value of -1, but there's probably a more elgant portable way to do it, rather than just hard-coding it.


Update:Great responses everyone! ++ all round!

Replies are listed 'Best First'.
Re: How to make EOF?
by samtregar (Abbot) on Mar 04, 2009 at 06:35 UTC
    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.



      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!

Re: How to make EOF?
by ELISHEVA (Prior) on Mar 04, 2009 at 07:07 UTC
    Are you looking for an EOF (if so, just close the socket) or are you looking for a way within the YAML document to say "I'm done"? If so, from the YAML 1.0 spec (applies to 1.1 and 1.2 as well):
    YAML uses three dashes (---) to separate documents within a stream. Comment lines begin with the pound sign (#). Three dots (...) indicate the end of a document without starting a new one, for use in communication channels.

    Best, beth

Re: How to make EOF?
by CountZero (Bishop) on Mar 04, 2009 at 06:23 UTC
    The ASCII character set has an ETX (End of Text), number 3 or an EOT (End of Transmission), number 4. Use chr NUMBER to get that character.


    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

Re: How to make EOF?
by zentara (Archbishop) on Mar 04, 2009 at 11:34 UTC
Re: How to make EOF?
by cdarke (Prior) on Mar 04, 2009 at 09:31 UTC
    You are probably thinking of the EOF returned by some C functions, which is indeed -1, but that is used in a different way. Even socket programs written in C would not use that. Windows uses a Control-Z character as an EOF marker in text files, but that causes all sorts of problems - don't go there.

    I always prefix my socket data blocks with a fixed length red-tape block at the front. This can consist of a sanity check (to make sure we know this is the start of a new block), a request code (you might not need, but could also be an error code) and the length of the data block which follows.