Why use the -p|-n in slurp mode in perl one liner? -
in perl 1 liner slurp mode 0777 hope equal below script
open $fh, "<","file"; local $/; $s = <$fh>; #now whole file stored $s here not using loop storing elements together(single data).
but in perl 1 liner, why use -p|-n switch enable slurp mode (-0777)? performance gain here.?
-p | -n using looping purpose. actual performance of 1 liner below script or else?
open $fh, "<","file"; $s; while (<$fh>) { $s.=$_; } print $s;
without -n or -p there no implicit while (<>) loop default $_ not set, , stdin isn't read either. if want have <> , $_ default in slurp mode need 1 of these switches along -0777, on own merely (un)sets $/. this
echo "hello" | perl -0777 -e 'print' prints nothing, , -w warns of use of uninitialized value $_. this
echo "hello" | perl -0777 -e '$v = <>; print $v' does print hello. stdin can read variable, 'slurp' is on.
in terms of equivalent to, mere -0777 $/ = undef. if add read
# use warnings; local $/; <>; print; the <> read in 1 go there no default input , pattern-searching space $_ read not assigned anything. warnings on hear it. (thanks jonathan leffler mentioning stdin in comment.) code equivalent using -n is
while (defined($_ = <argv>)) { } so standard input , $_ set up. run perl -mo=deparse -n -e 1 see these.
in perlvar conditions listed when "... perl assume $_ ...". last bullet
the default place put next value or input record when , readline, readdir or each operation's result tested sole criterion of while test. outside while test, not happen.
with -n or -p switches this, , of standard input.
note, example not equivalent since does assign.
comment on specific statements in question.
the slurp not "enabled" these switches -- set -0777 flag. use them because automatic standard input , $_ them.