vect0r has asked for the wisdom of the Perl Monks concerning the following question:

I just started into Perl, as in the past hour or so, and when I ran this code using perl hi.pl:
#!/usr/local/bin/perl print 'hello world';
It doesn't print but when I put 27 spaces before hello world it prints it all. What is wrong with this?

Replies are listed 'Best First'.
Re: Messed up printing
by ctilmes (Vicar) on Aug 09, 2004 at 23:32 UTC
    try this:
    print "hello world\n";
    Without the "\n", your shell prompt is probably overwriting the output. The 27 spaces is enough on your system such that the prompt doesn't overwrite it.
Re: Messed up printing
by davido (Cardinal) on Aug 10, 2004 at 04:27 UTC

    The script is not outputting a "newline" character, "\n". Without that, it's possible that either the output buffer isn't being flushed (unlikely), or that the print is there, but without a new line, when the program terminates your command-line prompt is getting printed over your "Hello world".

    This might happen, for example, if no newline is output, and yet when the command shell takes over upon program termination, it starts the prompt with a return to the leftmost screen column... on the same line that you just printed "Hello world".

    Just to be on the safe side, until you get used to how buffering and newlines work, end your print statements with a '\n'. That's an overgeneralization, but you'll quickly catch on to when/where you need newlines after you've tinkered a few more hours.

    I know this is probably not the right teaching example, but I used the technique of printing with \r (return), without \n (newline) to accomplish a JAPH that gave the visual impression of text sliding across the bottom of the screen. It's at: Slipery JAPH, if you're interested. ...just an aside; it's nothing serious.

    The need to output newlines is not entirely foriegn to people familiar with C. The buffering is a little different, IIRC, but the principle is along the same lines.


    Dave