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
.
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';
Perhaps you want to change your output record separator to linefeed with:
local $\ = "\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.
In Perl 6 there is, the say
function
If you're stuck with pre-5.10, then the solutions provided above will not fully replicate the say
function. For example
sub say { print @_, "\n"; }
Will not work with invocations such as
say for @arr;
or
for (@arr) {
say;
}
... because the above function does not act on the implicit global $_
like print
and the real say
function.
To more closely replicate the perl 5.10+ say
you want this function
sub say {
if (@_) { print @_, "\n"; }
else { print $_, "\n"; }
}
Which now acts like this
my @arr = qw( alpha beta gamma );
say @arr;
# OUTPUT
# alphabetagamma
#
say for @arr;
# OUTPUT
# alpha
# beta
# gamma
#
The say
builtin in perl6 behaves a little differently. Invoking it with say @arr
or @arr.say
will not just concatenate the array items, but instead prints them separated with the list separator. To replicate this in perl5 you would do this
sub say {
if (@_) { print join($", @_) . "\n"; }
else { print $_ . "\n"; }
}
$"
is the global list separator variable, or if you're using English.pm
then is is $LIST_SEPARATOR
It will now act more like perl6, like so
say @arr;
# OUTPUT
# alpha beta gamma
#
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.
Here's what I found at https://perldoc.perl.org/perlvar.html:
$\ 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.
example:
$\ = "\n";
print "a newline will be appended to the end of this line automatically";
You can use the -l
option in the she-bang header:
#!/usr/bin/perl -l
$text = "hello";
print $text;
print $text;
Output:
hello
hello
Source: Stackoverflow.com