Simple question about the print function 
Author Message
 Simple question about the print function

I'm attempting to learn perl and I don't understand a simple thing
about the print function.

If I have a one line program like this:
        print "Hello World!";
it doesn't work. No errors when I run it with the -w option, it just
does nothing. But if I also print a newline character it works fine:
        print "Hello World!\n";

Things that make you go hmmmm...if someone could explain what is
happening that would be great!

Thanks,
Matt



Sun, 16 May 2004 02:33:17 GMT  
 Simple question about the print function

Quote:
> I'm attempting to learn perl and I don't understand a simple thing
> about the print function.

> If I have a one line program like this:
>    print "Hello World!";
> it doesn't work. No errors when I run it with the -w option, it just
> does nothing. But if I also print a newline character it works fine:
>    print "Hello World!\n";

> Things that make you go hmmmm...if someone could explain what is
> happening that would be great!

Try redirecting the output to a file

 perl hw.pl > out.txt

or to a pager

 perl hw.pl | more

What do you see?



Sun, 16 May 2004 02:41:38 GMT  
 Simple question about the print function

Quote:

> If I have a one line program like this:
>    print "Hello World!";
> it doesn't work. No errors when I run it with the -w option, it just
> does nothing. But if I also print a newline character it works fine:
>    print "Hello World!\n";

> Things that make you go hmmmm...if someone could explain what is
> happening that would be great!

It's a buffering problem- your OS's stdout buffer isn't getting fflush(3)ed
to the terminal without a trailing newline.  For a thorough discussion, see

  http://perl.plover.com/FAQs/Buffering.html

--
Joe Schaefer    "Whoever undertakes to set himself up as a judge of Truth and
                    Knowledge is shipwrecked by the laughter of the gods."
                                               --Albert Einstein



Sun, 16 May 2004 02:48:56 GMT  
 Simple question about the print function

Quote:
>I'm attempting to learn perl and I don't understand a simple thing
>about the print function.

>If I have a one line program like this:
>    print "Hello World!";
>it doesn't work. No errors when I run it with the -w option, it just
>does nothing. But if I also print a newline character it works fine:
>    print "Hello World!\n";

>Things that make you go hmmmm...if someone could explain what is
>happening that would be great!

Chances are your shell/prompt/command interpretor/whatever uses a carriage
return (or some equivalent for your system) to go back to the beginning of the
line. In doing so it is overwriting the output of your program.

The obvious solution is to always output a newline character at the end.

You could also try:

$|=1;
print "Hello World!";
sleep 2;

Which should print Hello World! wait a couple of seconds and then exit (when
your prompt will erase the Hello World!).

The $| bit is to turn off buffering, this is explained in perlfaq5, under the
"How do I flush/unbuffer an output filehandle?  Why must I do this?" FAQ.

--
Sam Holden



Sun, 16 May 2004 02:56:45 GMT  
 Simple question about the print function
Hi,

are you storing that in a file and then trying to run it?

i would do it like this, first store the following code in a text file called
hello.

#!/usr/bin/perl
print "Hello, World!";

then, make the script executable with the command:

chmod +x hello

Then run the program with the command:

./hello

You can also produce exactly the same output with the following program:

#!/usr/bin/perl

print <<HELLO;
Hello, World!
HELLO

I learnt this trick just today ;-)

Also, in the example you gave you did not say whether you included
#!/usr/bin/perl on the first line of your program.

I hope I have been able to help you.

Kind regards,

martin


Quote:
> I'm attempting to learn perl and I don't understand a simple thing
> about the print function.

> If I have a one line program like this:
>    print "Hello World!";
> it doesn't work. No errors when I run it with the -w option, it just
> does nothing. But if I also print a newline character it works fine:
>    print "Hello World!\n";

> Things that make you go hmmmm...if someone could explain what is
> happening that would be great!

> Thanks,
> Matt

--
Posted from cache-wit-hsi.cableinet.co.uk [62.30.192.1]
via Mailgate.ORG Server - http://www.Mailgate.ORG


Sun, 16 May 2004 03:03:43 GMT  
 Simple question about the print function

Quote:

> I'm attempting to learn perl and I don't understand a simple thing
> about the print function.

> If I have a one line program like this:
>    print "Hello World!";
> it doesn't work. No errors when I run it with the -w option, it just
> does nothing. But if I also print a newline character it works fine:
>    print "Hello World!\n";

> Things that make you go hmmmm...if someone could explain what is
> happening that would be great!

It's impossible to know, but I expect that it has something to do with
the environment you are running the program in.  For instance, the
next command prompt could be over-writing your output.  In any case,
this is not a perl-specific issue.

Jon
--
  Two are better than one, because they have a good return for their
  work: If one falls down, his friend can help him up.  But pity the
  man who falls and has no one to help him up! ... Though one may be
  overpowered, two can defend themselves. A cord of three strands is
  not quickly broken. -- Ecclesiastes 4:9-10,12 (NIV)



Sat, 15 May 2004 18:56:46 GMT  
 Simple question about the print function

  MS> print "Hello, World!";

  MS> You can also produce exactly the same output with the following program:

  MS> print <<HELLO;
  MS> Hello, World!
  MS> HELLO

that is not the same output. think carefully about why.

  MS> I learnt this trick just today ;-)

good. then i hope you learned that <<FOO (called a here doc) is NOT a
feature of print. too many newbies think that since it is used so often
with print.

and learn to not top post. read these pages:

        http://www.btinternet.com/~chiba/sbox/topposters.html
        http://www.uwasa.fi/~ts/http/quote.html
        http://www.geocities.com/nnqweb/nquote.html

uri

--

-- Stem is an Open Source Network Development Toolkit and Application Suite -
----- Stem and Perl Development, Systems Architecture, Design and Coding ----
Search or Offer Perl Jobs  ----------------------------  http://jobs.perl.org



Sun, 16 May 2004 05:32:21 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. Simple print question

2. Simple print question

3. Question on LIBWWW function LWP::Simple get subroutine.

4. Simple question about UNLINK function

5. Newbie question Perl Print function using files

6. Simple Perl Function Question for ya

7. Simple function argument question

8. XS - Simple answer to simple question?

9. Simple Question from a simple chap!

10. Simple Question - LWP::Simple not accessing the internet

11. simple, simple question

12. Simple Unix Question, ( Simple for you maybe )

 

 
Powered by phpBB® Forum Software