There's more than one way to do things | |
PerlMonks |
Re: Funny usage of print functionby davido (Cardinal) |
on Sep 13, 2003 at 21:44 UTC ( [id://291295]=note: print w/replies, xml ) | Need Help?? |
If you want to embed newlines in your output (or any string) you shouldn't be doing it like this,
Although this works in some cases, you could concievably run into trouble when you take a script with such embedded newlines from a *nix type OS to a M$ operating system. The other problem with embedding invisible newlines in quoted strings is that it is difficult to tell the difference between wrapped text and text with newlines. And it is difficult to find the end of the string. People just don't expect " quoted " text to span multiple lines. At very least, use the visible version of newline, "\n". A more efficient and foolproof way of doing it if you have multiple lines of literal text is to use here documents. Here documents can be used directly within a print statement, or in an assignment statement, or anywhere else that a string is expected. Here's an example:
As I mentioned before, here documents can also be used directly within a print statement:
An interesting thing about here documents is that the text is assumed to be "double quoted" unless you wrap the leading marker in some other type of quotes. And as we all know, double-quoted text is subject to variable interpolation as well as "\n" conversion. So each of the following examples use a different sort of quoting:
I dove into this response because I was concerned when I saw in the Q&A section the promotion of using multiline "quoted" text with embedded invisible newlines. Yes, it can work. Yes, it can be quite confusing. Yes, that's what here-docs are for.
In Section
Seekers of Perl Wisdom
|
|