Re: breaking a line on printing
by tachyon (Chancellor) on Mar 19, 2002 at 10:14 UTC
|
# 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
| [reply] [d/l] [select] |
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 | [reply] [d/l] |
Re: breaking a line on printing
by demerphq (Chancellor) on Mar 19, 2002 at 10:12 UTC
|
| [reply] [d/l] [select] |
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.
| [reply] [d/l] |
|
| [reply] |
|
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;
| [reply] [d/l] [select] |
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!
| [reply] [d/l] |
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. | [reply] [d/l] |
|
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?
| [reply] |
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;
| [reply] [d/l] |
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. | [reply] |