Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 622
Mike Avatar asked May 24 '10 18:05

Mike


2 Answers

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.

like image 116
Josh Kelley Avatar answered Oct 06 '22 09:10

Josh Kelley


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

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

Output:

hello hello 
like image 40
jaypal singh Avatar answered Oct 06 '22 08:10

jaypal singh