I have a loop waiting for 'y', which works well. However, when i run it via telnet (and xinetd), it does not:
$| = 1; while (1) { $_ = <>; open FILE, ">/tmp/moo1"; print FILE $_; close FILE; chomp; open FILE, ">/tmp/moo2"; print FILE $_; close FILE; last if $_ eq "y"; } Checking the files: > od -c /tmp/moo1 0000000 > od -c /tmp/moo2 0000000 y \r 0000002
So, this is obviously because of telnet. But why is the file empty until a chomp, and \r instead of \n afterward?

-------------------------

UPDATE 10/31/11:

Following http://www.perlmonks.org/?node_id=549385, i found $/ set to \n, telnet sends \r\n. chomp removed the \n, but not the \r, hence it never matched.

As to why /moo1 is empty, i do not know, but it's interesting.

To test, here is a simple script:

#!/usr/bin/perl -w use strict; # for telnet, turn off buffering. $| = 1; while (1) { print "? "; $_ = <>; #print(unpack('H*', $_), "\n"); print $_; open FILE, ">/tmp/moo"; print FILE $_; close FILE; last if $_ eq "y"; } print "\n";

To use xinetd, add an entry to /etc/services:

test 4217/tcp

And add a file to the /etc/xinetd.d directory (at least on Debian) with the following contents:

service test { disable = no socket_type = stream wait = no user = chacham server = /home/chacham/test.pl }

To execute: telnet localhost 4217 ; od -c /tmp/moo;

Removing while(1) or last..., uncommenting the unpack, or making write >, append >>, makes it fill moo with the data.


In reply to Telnet to script, chomp stdin issues by chacham

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.