Search code examples
perl

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.


Solution

  • Raku (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.