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

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re: I can't make a new line =(
by davorg (Chancellor) on Jun 13, 2006 at 07:59 UTC

    Not all Perl is CGI and not all CGI is Perl.

    There was nothing in your original post that told us that you were using CGI and generating HTML. Without that information any answer that we could have given would have been complete guesswork. It's only because your later post included a use of CGI.pm that we are able to work out what your problem is.

    If you're having a problem with the output from a CGI program then you should always check the actual output rather than the output as rendered by a browser. You can do that by running the program in your browser and using the "view source" menu item. Or you can run the program from the command line (in which case you may need to go to a little more effort and simulate the CGI environment).

    Had you done that, you would have seen that the newline is created in your output exactly as you wanted it. The problem is that browsers ignore most newlines when rendering HTML. Your problem then becomes an HTML problem rather than a Perl problem.

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

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

      Thanks for all the help guys... I found out that "br" works perfectly in my script... though for some reason
      came up as an error, but I think I'll be able to do fine at the moment with br.
Re: I can't make a new line =(
by monkfan (Curate) on Jun 13, 2006 at 06:07 UTC
    It's very hard to figure out what you intend to do.
    Basicallly ("\n") stands for a newline so whenever you include it, it will break the line? Perhaps what you want is "concatenation".
    If so you can do it this way:
    my @lines = qw (foo bar qux); my $final_line = ""; foreach $line(@lines) { $final_line .= $line." "; } print "$final_line\n"; #include linebreak (\n) here
    gives you:
    foo bar qux #newline here
    or is there any particular thing other than that?
    Can you give us your sample script?

    Regards,
    Edward

      yea, i want to break the line into a new one... this is my script. i don't know how to minimize the font, so i just put a comment between the two statements i want to separate.

      #!C:\Perl\bin\perl.exe -wT use strict; use CGI ':standard'; my ($number,$value); $number = param('number'); $value = param('name'); print "Content-type:text/html\n\n"; $number = $number + 20; print "$value will be $number in twenty years."; # separate above operation from below condition if ($number >= 40) { print "Wow $value, you're old!"; } else { print "Wow $value, you're still young!"; }

      davorg: added code tags

        Please update your reply by putting <code> and </code> around your perl snippet, so we can read that part correctly as perl code.

        You are printing html output to a browser; the browser is supposed to treat all white-space characters the same (newlines are no different from spaces or tabs or carriage returns).

        To tell the browser to break a line, include a "<br>" tag (or, to be more XML-ish, "<br/>") at the point where you would normally use "\n".

        Update: to be more precise, something like this would do:

        my $quality = ( $number >= 40 ) ? "old" : "still young"; print "$value will be $number in twenty years.<br>Wow $value, you're $ +quality!";
        Just use br tag, see the script below.
        #!C:\Perl\bin\perl.exe -wT use strict; use CGI ':standard'; my ($number,$value); $number = param('number'); $value = param('name'); print "Content-type:text/html",br,br; $number = $number + 20; print "$value will be $number in twenty years."; # separate above operation from below condition print br,br; #<------------- Use br if ($number >= 40) { print "Wow $value, you're old!"; } else { print "Wow $value, you're still young!"; }

        Regards,
        Edward

        Ok, now I've added code tags, I can see what you're doing and can make another suggestion.

        you're telling the browser that your output is HTML (in the content type header). But actually, you're not sending HTML, you're sending plain text. If you tell the browser that you're sending plain text, then it will treat your output as plain text and honour your newlines.

        The simplest way to do that is to change your content type header:

        print "Content-type:text/plain\n\n";

        But as you're using CGI.pm, why not use the header function to make your life easier:

        print header(-type => 'text/plain');
        --
        <http://dave.org.uk>

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

        sorry, just saw how piled up that looks... i don't know how to make it look like the post you just made
Re: I can't make a new line =(
by davido (Cardinal) on Jun 13, 2006 at 06:57 UTC

    Well, if you are playing with CGI, you should be outputting HTML tags such as <p></p> or <br />.

    The \n that you're outputting is putting newlines in your HTML document, but that doesn't affect how the document is rendered in a browser. If you want a newline to be rendered in the browser, the HTML that your script is outputting needs a tag that would be rendered as a newline. <br /> is one option, or using proper paragraph tags.


    Dave

      A touch OT, but, since it's been mentioned twice in this thread...

      Has the HTML standard been amended to make <br /> the 'proper' form of the break tag or are people just generalizing and trying to be XMLish in HTML?