How can Perl add a new line by default?

In Perl, most of my print statements take the form

 print "hello." . "\n"; 

Is there a good way to avoid all annoying "\ n" lying?

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

+78
perl newline printing
May 24 '10 at 18:40
source share
9 answers

Perl 6 has a say function that automatically adds \n .

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

 use feature qw(say); 

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

See perldoc for details .

+86
May 24 '10 at 18:47
source share

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

 #!/usr/bin/perl -l $text = "hello"; print $text; print $text; 

Output:

 hello hello 
+28
Aug 03 '14 at 3:37
source share

If Perl 5.10+ is not an option, this is a quick and dirty approximation. This is not quite the same, because, say, it has some magic when its first argument is a handle, but for printing in STDOUT:

 sub say {print @_, "\n"} say 'hello'; 
+23
May 24 '10 at 18:50
source share

The way you write your print statement is too detailed. There is no need to separate a new line in your line. It's enough.

 print "hello.\n"; 

This implementation is likely to simplify your encoding as a whole.

In addition to using the use feature "say" or use 5.10.0 or use Modern::Perl to get the built-in say function, I'm going to pimp perl5i , which by default includes a lot of reasonable missing Perl 5 functions.

+18
May 24 '10 at 18:58
source share

Perhaps you want to change the output record separator to a string with:

local $\ = "\n";

 $ perl -e 'print q{hello};print q{goodbye}' | od -c 0000000 hellogoodbye 0000014 $ perl -e '$\ = qq{\n}; print q{hello};print q{goodbye}' | od -c 0000000 hello \ngoodbye \n 0000016 

Update: my answer is about capabilities, not about expediency. I don’t think adding the "\ n" at the end of lines should be a "annoying" chorus, but if someone really wants to avoid them, this is one way. If I were to support a bit of code that uses this technique, I would probably reorganize it pronto.

+9
May 24 '10 at 19:00
source share

Perl 6 has a say function

+3
May 24 '10 at 18:41
source share

If you are stuck with pre-5.10, then the solutions above will not fully replicate the say function. for example

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

Will not work with calls such as

 say for @arr; 

or

 for (@arr) { say; } 

... because the above function does not affect the implicit global $_ as print and the real say function.

To more accurately reproduce perl 5.10+ say , you need this function

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

Now it acts like

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

say built-in perl6 behaves a little differently. A call with say @arr or @arr.say will not just be a concatenation of the elements of the array, but will instead print them, separated by a list separator. To reproduce this in perl5 you will do it

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

$" is a global list separator variable, or if you are using English.pm , i.e. $LIST_SEPARATOR

Now it will be more like perl6, so

 say @arr; # OUTPUT # alpha beta gamma # 
+1
Aug 03 '15 at 0:02
source share

You can write a more readable form of using 5.010:

 use v5.10; 
0
Sep 04 '13 at 12:59 on
source share

Here is what I found at https://perldoc.perl.org/perlvar.html :

$ \ Output separator for the print statement. If specified, this value is printed after the last print argument. By default, this is undef.

You cannot call output_record_separator () on the handle only as a static method. See IO :: Handle.

Mnemonics: you set $ \ instead of adding "\ n" at the end of printing. Also, this is the same as $ /, but this is what you get back from Perl.

example:

 $\ = "\n"; print "a newline will be appended to the end of this line automatically"; 
0
Jan 15 '19 at 18:42
source share



All Articles