views:

337

answers:

5

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.

+2  A: 

In Perl 6 there is, the say function

Michael Mrozek
It's in 5.010 too :-)
nc3b
@nc3b Ah, handy
Michael Mrozek
+15  A: 

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.

Josh Kelley
+1 for mentioning the required package
Mike
In fact, all you have to do is `use 5.012;` or `use 5.010;` to get it if you're running those newer `perl`s.
Robert P
+4  A: 

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';
Eric Strom
Adam Bernier
+8  A: 

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.

Schwern
facepalm.jpg ...
Mike
Check out [`nextgen.pm`](http://search.cpan.org/~ecarroll/nextgen/)
Evan Carroll
A: 

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

$\ = "\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.

David M
or just `perl -le 'print q{hello};print q{goodbye}'`
ysth
No, please don't do that. While technically a correct answer, using a special variable for something as trivial as this is a maintenance burden down the road.
tsee
@tsee I agree completely, actually. As I edited my answer to indicate, my answer describes the capability, even if one wouldn't recommend it as a general practice.
David M
The real danger comes from the fact that all code will now add newlines, even code in other people's modules that weren't expecting it. Imagine networking code that would have sent the message `"helo\r\n"` that now sends `"helo\r\n\n"`.
Chas. Owens