in reply to Have trouble implementing a shell-alike script

It appears to me that there is a bit of O/S and protocol confusion going on here. Perl is actually pretty smart about how it deals with this - it will do the "right thing" about LF (Line Feed) vs CRLF (Carriage Return, Line Feed), but you have to give it a clue.

Here we are reading with one kind of a \n (the capture of the ls command on Unix) and desiring to write with another kind of \n (the socket write). Oh, yes both Windows and Network line termination is the same (CRLF). Unix line termination is just LF. Perl considers LF and CRLF the same for reading in line mode.

I suggest this ...

my @captured_output = split(/\n/,$ls_captured_line); ...then... foreach (@captured_output) { print socket "$_\n"; }
At the moment, the Unix machine that I test with is down. However, the above code is in theory correct- check it out yourself! On read, \n means LF or CRLF. When Perl writes to Windows, a \n is CRLF. When Perl writes to a network socket, a \n is CRLF. I think the compiler can also figure out what to do with: join("/n", @captured_output);

My point is that Perl can read lines terminated either way (\n meaning either just LF or CRLF). And further when Perl writes a \n, it will emit a LF or a CRLF depending upon what it is writing to. As weird as it sounds, Windows and Network \n is different than Unix \n.

Replies are listed 'Best First'.
Re^2: Have trouble implementing a shell-alike script
by Anonymous Monk on Sep 14, 2011 at 09:59 UTC
      Thank you for making my point. If you click on your own link, you will see the below.
      A common misconception in socket programming is that \n eq \012 everywhere. When using protocols such as common Internet protocols, \012 and \015 are called for specifically, and the values of the logical \n and \r (carriage return) are not reliable.
      The link gives examples:
      print SOCKET "Hi there, client!\r\n"; # WRONG print SOCKET "Hi there, client!\015\012"; # RIGHT <- also wrong!
      The \r is wrong. I think that:
      print SOCKET "Hi there, client!\n"; #WORKS, on Unix or Windows!! #sends CR/LF: \015\012
      Basically do not ever put \r (CR) in a print statement. I agree.
      For the \n in the source code, Perl will send either LF or CRLF depending upon what it is taking to.

        Thank you for making my point. If you click on your own link, you will see the below.

        And incredibly, in the line right after the one you quoted it says

        print SOCKET "Hi there, client!\r\n"; # WRONG print SOCKET "Hi there, client!\015\012"; # RIGHT

        Keep reading smartalec