How can Perl's print add a newline by default?

PerlPrintingNewline

Perl Problem Overview


In Perl most of my print statements take the form

print "hello." . "\n";

Is there a nice way to avoid keeping all the pesky "\n"s lying around?

I know I could make a new function such as myprint that automatically appends \n, but it would be nice if I could override the existing print.

Perl Solutions


Solution 1 - Perl

Perl 6 has the say function that automatically appends \n.

You can also use say in Perl 5.10 or 5.12 if you add

use feature qw(say);

to the beginning of your program. Or you can use Modern::Perl to get this and other features.

See perldoc feature for more details.

Solution 2 - Perl

You can use the -l option in the she-bang header:

#!/usr/bin/perl -l

$text = "hello";

print $text;
print $text;

Output:

hello
hello

Solution 3 - Perl

If Perl 5.10+ is not an option, here is a quick and dirty approximation. It's not exactly the same, since say has some magic when its first arg is a handle, but for printing to STDOUT:

sub say {print @_, "\n"}

say 'hello';

Solution 4 - Perl

The way you're writing your print statement is unnecessarily verbose. There's no need to separate the newline into its own string. This is sufficient.

print "hello.\n";

This realization will probably make your coding easier in general.

In addition to using use feature "say" or use 5.10.0 or use Modern::Perl to get the built in say feature, I'm going to pimp perl5i which turns on a lot of sensible missing Perl 5 features by default.

Solution 5 - Perl

Perhaps you want to change your output record separator to linefeed with:

local $\ = "\n";

$ perl -e 'print q{hello};print q{goodbye}' | od -c
0000000    h   e   l   l   o   g   o   o   d   b   y   e                
0000014
$ perl -e '$\ = qq{\n}; print q{hello};print q{goodbye}' | od -c
0000000    h   e   l   l   o  \n   g   o   o   d   b   y   e  \n        
0000016

Update: my answer speaks to capability rather than advisability. I don't regard adding "\n" at the end of lines to be a "pesky" chore, but if someone really wants to avoid them, this is one way. If I had to maintain a bit of code that uses this technique, I'd probably refactor it out pronto.

Solution 6 - Perl

Here's what I found at <https://perldoc.perl.org/perlvar.html>;: > $
> The output record separator for the print operator. If defined, this value is > printed after the last of print's arguments. Default is undef. > > You cannot call output_record_separator() on a handle, only as a static method. > See IO::Handle. > > Mnemonic: you set $\ instead of adding "\n" at the end of the print. Also, it's > just like $/ , but it's what you get "back" from Perl.

example:

$\ = "\n";
print "a newline will be appended to the end of this line automatically";

Solution 7 - Perl

In Perl 6 there is, the say function

Solution 8 - Perl

If you're stuck with pre-5.10, then the solutions provided above will not fully replicate the say function. For example

sub say { print @_, "\n"; }

Will not work with invocations such as

say for @arr;

or

for (@arr) {
    say;
}

... because the above function does not act on the implicit global $_ like print and the real say function.

To more closely replicate the perl 5.10+ say you want this function

sub say {
    if (@_) { print @_, "\n"; }
    else { print $_, "\n"; }
}

Which now acts like this

my @arr = qw( alpha beta gamma );
say @arr;
# OUTPUT
# alphabetagamma
#
say for @arr;
# OUTPUT
# alpha
# beta
# gamma
#

The say builtin in perl6 behaves a little differently. Invoking it with say @arr or @arr.say will not just concatenate the array items, but instead prints them separated with the list separator. To replicate this in perl5 you would do this

sub say {
    if (@_) { print join($", @_) . "\n"; }
    else { print $_ . "\n"; }
}

$" is the global list separator variable, or if you're using English.pm then is is $LIST_SEPARATOR

It will now act more like perl6, like so

say @arr;
# OUTPUT
# alpha beta gamma
#

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMikeView Question on Stackoverflow
Solution 1 - PerlJosh KelleyView Answer on Stackoverflow
Solution 2 - Perljaypal singhView Answer on Stackoverflow
Solution 3 - PerlEric StromView Answer on Stackoverflow
Solution 4 - PerlSchwernView Answer on Stackoverflow
Solution 5 - PerlDavid MView Answer on Stackoverflow
Solution 6 - PerlhofergabrielView Answer on Stackoverflow
Solution 7 - PerlMichael MrozekView Answer on Stackoverflow
Solution 8 - PerlJoshuaView Answer on Stackoverflow