Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

Funny usage of print function

by Anonymous Monk
on Mar 20, 2000 at 16:47 UTC ( #5708=categorized question: print w/replies, xml ) Need Help??
Contributed by Anonymous Monk on Mar 20, 2000 at 16:47 UTC
Q&A  > input and output


I found it is possible to use print function in the following way:
$a=4; $b=5; print " Something = $a Something else = $b ";
It makes newlines itself! I didn't find anything like this in documentation and FAQs.

Answer: Funny usage of print function
contributed by davido

If you want to embed newlines in your output (or any string) you shouldn't be doing it like this,

my $a; $a = " Some stuff here Some more stuff on the next line ";

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:

my $string = <<END_HERE; Here is some text. Newlines, though invisible, are implicitly understood, and in fact, are expected. A here document won't confuse people. END_HERE

As I mentioned before, here documents can also be used directly within a print statement:

print << END_HERE; Here is multi-line text created with a here document. END_HERE

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:

my $string; $string = <<END_HERE; # Seen as double quotes. text END_HERE $string = <<"END_HERE"; # Same thing. $string = <<'END_HERE'; # Interpolated as 'single quotes'. $string = <<`END_HERE`; # Interpolated as `backtick` quoted text. or even..... ( $string <<END_HERE ) =~ s/^\s+//gm; indented text becomes unindented because the substitution regexp removes preceding whitespace. END_HERE ...and so on.

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.

Answer: Funny usage of print function
contributed by chromatic

If you have a newline embedded in a quoted string (single or double both work for me), Perl will include it when printing:

print "a b c d";
a b c d
while print "a b c d"; prints a b c d Check for line breaks in your code.
Answer: Funny usage of print function
contributed by comatose

You should also be aware that most likely, it will work unexpectedly on different operating systems. If you have a Unix formatted script and try to run it on Win32, you'll get the same results as if you just used \n.

Please (register and) log in if you wish to add an answer

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others perusing the Monastery: (5)
    As of 2021-03-02 21:40 GMT
    Find Nodes?
      Voting Booth?
      My favorite kind of desktop background is:

      Results (63 votes). Check out past polls.