chacham has asked for the wisdom of the Perl Monks concerning the following question:
So, this is obviously because of telnet. But why is the file empty until a chomp, and \r instead of \n afterward?$| = 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
-------------------------
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/tcpAnd 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 | |
by chacham (Prior) on Oct 28, 2011 at 16:26 UTC | |
by chacham (Prior) on Oct 28, 2011 at 16:51 UTC |