B::Deparse module will show what switches do to your perl code,
perl -MO=Deparse -e 's/eat/read/g' file
s/eat/read/g;
nothing interesting here, your code executes as it is.
perl -MO=Deparse -i -e 's/eat/read/g' file
BEGIN { $^I = ""; }
s/eat/read/g;
ok, -i
changed global variable $^I
for inline editing, but no file is being read, and no loop here.
perl -MO=Deparse -p -i -e 's/eat/read/g' file
BEGIN { $^I = ""; }
LINE: while (defined($_ = <ARGV>)) {
s/eat/read/g;
}
continue {
print $_;
}
so -p
adds while(){}
loop where every line is read, altered by your code, and written again to file (print in continue{}
block does that)
perl -MO=Deparse -n -i -e 's/eat/read/g' file
BEGIN { $^I = ""; }
LINE: while (defined($_ = <ARGV>)) {
s/eat/read/g;
}
so -n
doesn't have continue{}
block with print like -p
switch, and you have to print manually after s///
substitution if you want to make desired changes to the file (otherwise nothing is printed and your file ends truncated as you've already noticed).