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

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.

Replies are listed 'Best First'.
Re: Telnet to script, chomp stdin issues
by choroba (Cardinal) on Oct 28, 2011 at 13:37 UTC
    You should check success of open:
    open my $handle, '<', "filename" or die "Cannot open: $!";
    Maybe you cannot overwrite already existing moo1?
    \r\n is the end-of-line on some systems. What system runs the telnet, what system does the target computer run?
      Thanx for looking.
      • Running on Debian.
      • Telnet from Debian (same system) or Windows XP fails.
      • The file fails even when written the first time.
      • The file is updated with the current date and time.
        It also runs fine when run from the command line. It only fails when run through telnet.

        That is, i telnet to the system, xinetd picks up and runs the code.