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.";
| [reply] |
|
|
| [reply] |
|
|
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
| [reply] |
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. | [reply] [d/l] |
|
|
| [reply] |
|
|
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.
| [reply] |
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
| [reply] [d/l] [select] |
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? | [reply] [d/l] [select] |
|
|
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 | [reply] [d/l] |
|
|
How can I learn about all the different subroutines that are included in CGI.pm?
Thanks :)
| [reply] |
|
|
---
print map { my ($m)=1<<hex($_)&11?' ':'';
$m.=substr('AHJPacehklnorstu',hex($_),1) }
split //,'2fde0abe76c36c914586c';
| [reply] [d/l] [select] |
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.
| [reply] |
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. | [reply] |
Re: \n won't work?!
by iamrobj (Initiate) on Dec 29, 2002 at 04:48 UTC
|
| [reply] |
|
|
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.
| [reply] |