Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

breaking a line on printing

by hotshot (Prior)
on Mar 19, 2002 at 09:41 UTC ( [id://152692]=perlquestion: print w/replies, xml ) Need Help??

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

I'm sure someone has answered this already (I couldn't find it in the history), but here goes (a stupid one i'm sure):
How do I use the print command in a way that my code will be more organized, for example:

If i want to print a long (more then a row) sentence then my code looks ugly, something like this:
print "Once upon a time there was a little programmer that asked to ma +ny questions.\nToday his question was realy a stupd one.\nand so on a +nd on..."; if ($bla eq 'test') { ....; }
and you can see this looks ugly. is there a line seperator or something else to handle these situation of long lines of code (actually not only in print command)?

Thanks

Hotshot

Replies are listed 'Best First'.
Re: breaking a line on printing
by tachyon (Chancellor) on Mar 19, 2002 at 10:14 UTC

    You can do any of these:

    # using a heredoc print <<TEXT; Once upon a time there was a little programmer that asked to many ques +tions. Today his question was realy a stupd one. and so on and on... TEXT # using literal newlines in the text print 'Once upon a time there was a little programmer that asked to ma +ny questions. Today his question was realy a stupd one. and so on and on... '; # using the comma operator print "Once upon a time there was a little programmer that asked to ma +ny questions.\n", "Today his question was realy a stupd one\n", "and so on and on...\n"; # using the concatenation operator print "Once upon a time there was a little programmer that asked to ma +ny questions.\n" . "Today his question was realy a stupd one\n" . "and so on and on...\n"; # using a custom sub. declare prototype so we can use it as # a bareword. We use ^\n to mark our line wraps and remove # the leading whitespace on the next line. We use a regex # to remove these and presto..... sub wrap; print wrap "Once upon a time there was a little programmer ^ that asked to many questions.\nToday his question ^ was realy a stupd one\nand so on and on...\n"; sub wrap { $text = shift; $text =~ s/\^\n\s*//g; return $text; }

    Update

    You can even define your own custom print function that does the (un)wrap bit for you:

    sub printw; $interpolate = "Interpolate this\n"; $comma = "We can use the comma operator\n"; @ary = qw (this_ is_ an_ array); printw "Once upon a time there was a little programmer ^ that asked to many questions.\nToday his question ^ was realy a stupd one\nand so on and on...\n^ $interpolate^ ", $comma, @ary; sub printw { my @text = @_; s/\^\n\s*//g, print for @text; }

    cheers

    tachyon

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

Re: breaking a line on printing
by Tyke (Pilgrim) on Mar 19, 2002 at 10:06 UTC
    Have you looked at the 'heredoc' notation?
    #!/usr/bin/perl use strict; use warnings; print <<_END_; Once upon a time there was a little programmer that asked too many que +stions. Today his question wasn't realy a stupid one. And so on and on.. _END_
    Not a perfect solution with regards to code indentation, but still very useful
Re: breaking a line on printing
by demerphq (Chancellor) on Mar 19, 2002 at 10:12 UTC
    There are a few variations of Bikers answer

    • Set $, to be a newline (or use join explicitly as suits your taste and style), also set $\ to a newline.
      { # setting $, $\ should be done in a block with local local $\="\n"; { local $,="\n"; print "These", "Are", "Seperate", "lines"; } print join ("\n", "As", "Are", "These"); }
    • Use a here doc. (Consult the docs, here docs can be a bit tricky.)
      print <<END_OF_TEXT; These are seperate lines END_OF_TEXT

    Yves / DeMerphq
    --
    This space for rent.

Re: breaking a line on printing
by webadept (Pilgrim) on Mar 19, 2002 at 11:59 UTC
    All of the above are great.. I didn't notice anyone using my favorite
    print qq` This is text and I want to print this text on many lines as I can with "quotes and without quotes +" .. `;
    I use that for a lot of HTML stuff and such things. That's not a single quote or apostorphe there.. is the .. heck I don't really know what its called.. its on the same key as the ~ is but I believe you can use just about anything as long as that something is not inside the text you are using.

    Oh.. and it holds formating.. so watch your line breaks. Try it out, you'll see what I mean.

    hope that helps

    Glenn H.
      That's a backtick.
        a backtick?? Oh no! I hate ticks, burn it off, burn it off!!
        And as for webadept's answer, that is also one of my favorites. I generally use something like

        #!/usr/bin/perl -w use strict; print qq|Content-type: text/html\n <html> <head> <title>Yeah Yeah</title> </head> <!-- More HTML stuff here, etc... --> Oh looky here, a whatever this character is called \| |;

        As long as you backslash any |'s in the text you'll be fine (and I don't think your output contains many if any of these...)

        If you are outputting strings that contain a lot of strange characters, I'd definitely suggest use heredocs, especially if you are outputting very long HTML documents. For example:

        print <<'YayIAmDone'; look at this text. lots of non-alphanumeric characters in this print s +tatement! 8)^404`76`5`65^%!^%^@éA¿º¿ªÑñç¦ and we don't even have to l +ook for anything to backslash! !$#!#!1~!````~!~!~21&$3(&6(*7_*(+|}|]{ +]{]":':?,,?< YayIAmDone exit;
Re: breaking a line on printing
by Biker (Priest) on Mar 19, 2002 at 09:51 UTC

    What you're really asking for is a way to define a quoted string on several lines.

    As far as I'm aware there is no direct way of doing this. (Like in VB or some such.) You could take a different approach:

    print "This is a long string\n". "on more than one\n". "line\n";
    This implies that perl will have to do the concatenation, which 'feels wrong'. But the Perl interpreter will optimize this during the compilation phase and create one long string to be printed.


    Everything will go worng!

Re: breaking a line on printing
by ellem (Hermit) on Mar 19, 2002 at 17:28 UTC
    You can do all of the wonderful things everyone has already suggested BUT if I read you question correctly you are concerned that your code is ugly... (strange for a Perl programmer.. but OK =) )
    print "I want this on one line I want this on another line and finally put this here on yeat another line.\n"
    It's practically WYSIWYG. Just hit enter at the end of what you're doing.
    --
    ellem@optonline.net
    There's more than one way to do it, but only some of them actually work.
      I'm surprised more people haven't suggested this approach. I see people do this all the time, without ill effects. Has anyone seen this *not* work?
Re: breaking a line on printing
by abaxaba (Hermit) on Mar 19, 2002 at 17:12 UTC
    There have been some good suggestions here, but no one has mentioned my particular favorite, which I use especially with html output.
    I push everything onto an array, then just dump the array. This seems to work for me for a couple of reasons:
    1. Format whitespace, easier readability.
    2. Pass array ref around to different subs, to build up output string in a modular format, without worrying about return values:
    #!perl main(); sub main { my @output; push (@output, "Here is something to print out\n", "Here is yet another line\n"); extra(\@output); print @output; } sub extra { my $resRef = shift; push (@$resRef, "Here is an extra line\n"); } exit 0;
Re: breaking a line on printing
by code_drifter (Initiate) on Mar 19, 2002 at 14:17 UTC
    I just hit Enter rather than using the "\n". Perl will use the literal keystroke of the enter key.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (6)
As of 2024-04-18 00:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found