How to print a line to STDERR and STDOUT in Perl?

Posted on In QA

In Perl, how to print a string as a line to STDOUT? That is, the string and the newline character, nicely?

And similarly, how to print the line to STDERR?

In Perl, to nicely print a new line to STDOUT, you can use the “say” feature which “Just like print, but implicitly appends a newline”:

use 5.010;
say "hello world!"

An example,

$ perl -de1

main::(-e:1):	1
  DB<1> use 5.010;

  DB<2> say "hello world"
hello world

  DB<3> 

In Perl, to print to STDERR, you can print to STDERR which is the STDERR:

say STDERR "your message here"

For example,

$ perl -de1

main::(-e:1):	1
  DB<1> use 5.010;

  DB<2> say STDERR "hello world";
hello world

  DB<3> 

Leave a Reply

Your email address will not be published. Required fields are marked *