4.4 years ago by
I think, the intention is not to have a difference. However, when executing your second line of code in BASH, you have to escape the dollar symbol, like this:
perl -lane "s/\s+/_/g if /^>/; print \$_ "
or use single quotes like so:
perl -lane 's/\s+/_/g if /^>/; print $_'
If used correctly, the output of both commands is the same. This is due to Perl print's implementation: "If LIST is omitted, prints
$_ to the currently selected output handle." Which means that without further selection of the filehandle to use, print() without any argument will write the content of $_ to the standard output. The same applies for a large number of built-in functions including regular expressions like `s///` which also use $_ if no other variable is given. Thus, the following to lines of code are equivalent:
$_ =~ s/\s+/_/g;
A general note: using perlvar short forms in Perl software does not only make the code harder to read and to understand but can easily introduce errors. From my point of view, a nice rule of thumb is: "If you have to use it explicitly, use something else instead"