You are Suffering from Buffering

The '\n' newline character causes the file buffer to be flushed. Yes, STDOUT (in this case the screen) is considered a file. Without the newline, the buffer isn't flushed as often as you would probably like, and the result is that nothing gets printed (yet).

This is a pretty common problem. So common, in fact, that there's a new feature that you can use, called "say()". say() is identical to print(), but automatically appends the newline character for you. You have to tell perl to use the feature for it to be available. See say and feature for an explanation of how to enable and use say()

Have a look at the article I linked to at the top of this post.

If you don't mind, in the future, try to ask the question in a block of text preceding the code snippet. It's tedious trying to find the question embedded within the script.


Dave


In reply to Re: Strangely adding a "\n" character prints a variable by davido
in thread Strangely adding a "\n" character prints a variable by iphone

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.