Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

\n won't work?!

by iamrobj (Initiate)
on Dec 29, 2002 at 04:47 UTC ( #222851=perlquestion: print w/replies, xml ) Need Help??

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

Hello!!! I am still new to Perl, but reading a book and trying some of the example scripts right now.

I am using strict; and CGI ':standard'; and when I print I am doing this:
print "blah blah \n\n\n blah blah blah";
AND
print qq(\n\n what ever \n\n);
and the bloody \n's aren't working...?!

What am I doing wrong?

Replies are listed 'Best First'.
Re: \n won't work?!
by sauoq (Abbot) on Dec 29, 2002 at 05:04 UTC

    I'm going to look into my crystal ball and predict that you are sending a content-type of text/html. Browsers generally collapse whitespace in HTML.

    -sauoq
    "My two cents aren't worth a dime.";
    
      I am using the line:
      print "Content-type: text/html\n\n";

      Why do you think the book (the book: CGI and Perl for the WWW, PeachPit Press) would use this line along with examples of using \n for a new line?

        There are three reviews of that book on this site. Perhaps you should take a look at those. You'll see that the book isn't very highly regarded.

        --
        <http://www.dave.org.uk>

        "The first rule of Perl club is you do not talk about Perl club."
        -- Chip Salzenberg

Re: \n won't work?!
by dbp (Pilgrim) on Dec 29, 2002 at 05:06 UTC
    Think about what your script is doing; it's outputing html. Html ignores whitespace, including eols. If you want a linebreak you'll need to use html tags to get the effect. <br>, <p></p> etc.
      Thanks a lot guys :)

      If I am to learn more about Perl, would you suggest that I use CGI.pm, or do more coding by myself? Is CGI.pm very popular?

        CGI.pm is definitely the way to go for web/cgi programming; for an explanation of why see use CGI or die;. But Perl is a rich language and is useful for a lot more than web-automation and cgi programming involves a lot of little (often annoying) details that may hamper your ability to learn the fundamentals of the language. I'd suggest picking up a good general Perl book, such as The Camel, and an introductory text like Learning Perl if you are really interested in learning Perl. Even if all you want to do is web programming, a general understanding of the language will serve you well.
Re: \n won't work?!
by tachyon (Chancellor) on Dec 29, 2002 at 05:17 UTC

    HTML ingones line breaks and also will condense consecutive spaces down to a single space. You have two mainoptions to get 'newlines' in HTML - <pre> and <br>:

    # first you must always print a header line or you will get a 500 erro +r print "Content-Type: text/html\n\n"; # now you can do this: print "<pre>\nLine1\nLine2\n$some_var\nLine4\n</pre>"; # which is the same as this but not as pretty # (here I demonstrate the qq operator as well as using literal \n in t +ext print qq| <pre> Line1 Line2 $some_var Line4 </pre>|; # alternatively you can use <br> (here is a demo of herpage notation a +s well) print <<HTML <p> Line1<br> Line2<br> $some_var<br> Line4 </p> HTML

    cheers

    tachyon

    s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

Re: \n won't work?!
by thezip (Vicar) on Dec 29, 2002 at 08:58 UTC
    There is another clarification that can be made. While browsers will visibly collapse multi-white-spaces, you can do a "View Source" within the browser to see the raw (unrendered) HTML. From here you will see exactly what Perl has generated. For example, this script:
    #!/perl/bin/perl -w use strict; use CGI; my $q = new CGI; print $q->header(); print qq(<!--prints "Hello World" with two embedded newlines-->\n); print qq(<b>Hello\n\nWorld</b>\n);
    ... will render:

    Hello World

    in the browser. "View Source" reveals:
    <!--prints "Hello World" with two embedded newlines--> <b>Hello World</b>
    By judiciously using newlines and HTML comments, you can document and beautify the raw HTML source so that troubleshooting is a snap.

    I hope this helps.

    Where do you want *them* to go today?
      If beautifying your HTML source is your goal, don't worry about adding \n's to your CGI.pm output... just slap a
      use CGI::Pretty;
      at the top of your program and let the CGI::Pretty module automagically do it for you. Laziness is a virtue. :-)

      Gary Blackburn
      Trained Killer

      How can I learn about all the different subroutines that are included in CGI.pm?

      Thanks :)

        The best place to start would be perldoc CGI.

        --- print map { my ($m)=1<<hex($_)&11?' ':''; $m.=substr('AHJPacehklnorstu',hex($_),1) } split //,'2fde0abe76c36c914586c';
Re: \n won't work?!
by dws (Chancellor) on Dec 29, 2002 at 05:18 UTC
    the bloody \n's aren't working...?!

    Since you're new to Perl, is it also true that you're new to HTML? If so, you might be expecting that a \n will force a line-break that will show up when your HTML is displayed in a browser. Such is not the case, unless you're emitting the \n with a <pre> or <code> block.

Re: \n won't work?!
by iamrobj (Initiate) on Dec 29, 2002 at 04:48 UTC
    By the way, I am using CGI.pm version: 2.752

    http://www.robj.ca/cgi-bin/animal.pl?animal=monkey&site=http://www.robj.ca

      As I'm sure you are aware by now, your problem is HTML related and not really Perl related. HTML ignores whitespace as others have stated.

      To answer the rest of your question, I'd definitely continue using CGI.pm. It is a VERY useful tool for manipulating form data, handling HTML, etc. Personally, I do not use the HTML-handling portions of CGI.pm. Like a lot of programmers out there I prefer to keep my HTML code seperate from my Perl. It can be much easier to edit an HTML file when the boss wants a button placed in a different spot than to dig through lines of Perl code.

      Be sure to check out online documentation for HTML if you don't know it yet, and check out templating modules such as HTML::Template or the more advanced Template Toolkit. Also check out CGI::Application (one of my favorites) once you get the hang of things.

Re: \n won't work?!
by bart (Canon) on Dec 30, 2002 at 00:06 UTC
    Use the "view source" option in your browser and you'll see that it did work.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://222851]
Approved by rob_au
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (3)
As of 2021-12-01 22:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    R or B?



    Results (15 votes). Check out past polls.

    Notices?