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.


In reply to Re: Have trouble implementing a shell-alike script by Marshall
in thread Have trouble implementing a shell-alike script by PerlOnTheWay

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.