Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it wrong to print $/ instead of \n?

Tags:

newline

perl

Perl's documentation says that $/ is:

The input record separator, newline by default. This influences Perl's idea of what a "line" is.

So, is it basically wrong to:

print STDERR $var, $/;

instead of:

print STDERR "$var\n";

?

What could go wrong if I do the former?

like image 664
n.r. Avatar asked Jul 13 '15 16:07

n.r.


Video Answer


1 Answers

Perhaps you are looking for the output record separator instead?

perldoc perlvar:

 IO::Handle->output_record_separator( EXPR )
   $OUTPUT_RECORD_SEPARATOR
   $ORS
   $\

The output record separator for the print operator. If defined, this value is printed after the last of print's arguments. Default is "undef".

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

Mnemonic: you set "$\" instead of adding "\n" at the end of the print. Also, it's just like $/, but it's what you get "back" from Perl.

For example,

$\ = $/;
print STDERR $var;
like image 156
xxfelixxx Avatar answered Sep 21 '22 11:09

xxfelixxx