in reply to Re^4: Have trouble implementing a shell-alike script
in thread Have trouble implementing a shell-alike script

So anonymous smartalec, post some code that proves your point.

I think that it is beyond discussion that Perl prints LF for \n on a Unix system and Perl prints CRLF for \n on a Windows system. I further claim that Perl will print CRLF for \n on a Network socket.

  • Comment on Re^5: Have trouble implementing a shell-alike script

Replies are listed 'Best First'.
Re^6: Have trouble implementing a shell-alike script
by Anonymous Monk on Sep 14, 2011 at 10:42 UTC

    So anonymous smartalec, post some code that proves your point.

    The documentation proves my point. You want to refute the documentation, you post the code.

Re^6: Have trouble implementing a shell-alike script
by Anonymous Monk on Sep 14, 2011 at 11:05 UTC

    Here it is, adopted from perlipc, which has small typo ( die ... unless if... )

    The server

    #!/usr/bin/perl -Tw use strict; BEGIN { $ENV{PATH} = "/usr/bin:/bin" } use Socket; use Carp; my $EOL = "\015\012"; sub logmsg { print "$0 $$: @_ at ", scalar localtime(), "\n" } my $port = shift || 2345; die "invalid port" unless $port =~ /^ \d+ $/x; my $proto = getprotobyname("tcp"); socket(Server, PF_INET, SOCK_STREAM, $proto) || die "socket: $!"; setsockopt(Server, SOL_SOCKET, SO_REUSEADDR, pack("l", 1)) || die "setsockopt: $! +"; bind(Server, sockaddr_in($port, INADDR_ANY)) || die "bind: $!"; listen(Server, SOMAXCONN) || die "listen: $!"; logmsg "server started on port $port"; my $paddr; $SIG{CHLD} = \&REAPER; for ( ; $paddr = accept(Client, Server); close Client) { my($port, $iaddr) = sockaddr_in($paddr); my $name = gethostbyaddr($iaddr, AF_INET); logmsg "connection from $name [", inet_ntoa($iaddr), "] at port $port"; print Client prelen( "Hello there, $name, it's now ", scalar local +time(), $EOL ); print Client prelen( 'backslash \n__'."\n" ); print Client prelen( 'backslash \r\n'."\r\n" ); print Client prelen( 'backslash EOL_'.$EOL ); } sub prelen { my $str = join '',@_; my $len = length $str; return "($len+6)$str"; }

    And the client

    #!/usr/bin/perl -w use strict; use Socket; my ($remote, $port, $iaddr, $paddr, $proto, $line); $remote = shift || "localhost"; $port = shift || 2345; # random port if ($port =~ /\D/) { $port = getservbyname($port, "tcp") } die "No port" unless $port; $iaddr = inet_aton($remote) || die "no host: $remote"; $paddr = sockaddr_in($port, $iaddr); $proto = getprotobyname("tcp"); socket(SOCK, PF_INET, SOCK_STREAM, $proto) || die "socket: $!"; connect(SOCK, $paddr) || die "connect: $!"; binmode SOCK or die "binmode $!"; ## no effect on client side while ($line = <SOCK>) { my $length = length $line; print "($length)$line"; } close (SOCK) || die "close: $!"; exit(0); __END__ $ perl client.pl (65)(59+6)Hello there, localhost, it's now Wed Sep 14 03:53:10 2011 (21)(15+6)backslash \n__ (22)(16+6)backslash \r\n (22)(16+6)backslash EOL_

    The output from client

    (65)(59+6)Hello there, localhost, it's now Wed Sep 14 03:53:10 2011
    (21)(15+6)backslash \n__
    (22)(16+6)backslash \r\n
    (22)(16+6)backslash EOL_

    The odd man out is the lone \n, it doesn't become \r\n

    The strings are all the same size except for the line endings, with \n remaining \012 and \r remaining \015

    So the documentation is correct

      The client in my question is not a robot/program,

      but a human using telnet.

        Ok? Just do what perlport#Newlines says
        use Socket qw(:DEFAULT :crlf); local($/) = LF; # not needed if $/ is already \012 while (<SOCKET>) { s/$CR?$LF/\n/; # not sure if socket uses LF or CRLF, OK # s/\015?\012/\n/; # same thing }
        Does that work for you?

      This code is generating text manually,

      what I ask is how to translate the text format generated by running shell commands `$cmd` when transfer to windows telnet client.

Re^6: Have trouble implementing a shell-alike script
by Anonymous Monk on Sep 14, 2011 at 10:53 UTC

    I think that it is beyond discussion that Perl prints LF for \n on a Unix system and Perl prints CRLF for \n on a Windows system.

    No. That depends entirely upon layers/discipline and not on platforms.

    Yes, some platforms have defaults, but they're not absolute.