Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Why is "\n" displayed as space in browser?

by neutron (Sexton)
on Mar 16, 2009 at 22:38 UTC ( [id://751052]=perlquestion: print w/replies, xml ) Need Help??

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

Hi everyone,

Really quick question: when I use the "\n" command in cgi, I get a space and not a new line, why??
#!/perl/bin/perl -wT use CGI qw(:standard); use CGI::Carp qw(warningsToBrowser fatalsToBrowser); use strict; print header; print start_html( "Environment Variables" ); foreach my $key( sort( keys( %ENV ) ) ) { print "$key = $ENV( $key )\n"; } print end_html;
Help, as always, much appreciated.
neutron

Replies are listed 'Best First'.
Re: Why is "\n" displayed as space in browser?
by moritz (Cardinal) on Mar 16, 2009 at 22:45 UTC
    You see spaces in the browser because your output is interpreted as HTML, and in HTML all kind of whitespaces are just interpreted as spaces.

    You can use pargraphs (<p>...</p>), line breaks (<c><br>) or <pre>...</pre> to do more formatting. Or just emit plain text instead.

Re: Why is "\n" displayed as space in browser?
by FunkyMonk (Chancellor) on Mar 16, 2009 at 23:13 UTC
    When I run your code, I get a lot of
    [Mon Mar 16 23:05:22 2009] pm: Use of uninitialized value $ENV in conc +atenation (.) or string [...]

    errors. Perhaps you should have used

    print "$key = $ENV{ $key }<br>"; # ^------^ braces, not parentheses

    instead (which also includes moritz's suggestion).

Re: Why is "\n" displayed as space in browser?
by targetsmart (Curate) on Mar 17, 2009 at 06:06 UTC
    browsers only considers your data as HTML, because your header says that 'content-type: text/html', if it has said 'text/plain' then this new line will be interpreted as it is (but other html tags won't work), if you want to insert a newline when the content type is 'text/html' use <br> or <p> tags.

    Vivek
    -- In accordance with the prarabdha of each, the One whose function it is to ordain makes each to act. What will not happen will never happen, whatever effort one may put forth. And what will happen will not fail to happen, however much one may seek to prevent it. This is certain. The part of wisdom therefore is to stay quiet.
Re: Why is "\n" displayed as space in browser?
by bradcathey (Prior) on Mar 17, 2009 at 02:59 UTC

    use two:

    \n\n

    together.

    —Brad
    "The important work of moving the world forward does not wait to be done by perfect men." George Eliot

      Does not work, sorry to say. Any amount of whitespace is interpreted as a space by HTML.

      This: An apple a day, keeps the doctor away. is the same as this:
      An apple a day keeps the doctor away.
      for(split(" ","tsuJ rehtonA lreP rekcaH")){print reverse . " "}print "\b.\n";

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (4)
As of 2024-04-23 22:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found