open (INFILE, "input.txt") or die "cannot open input: $!"; open (OUTFILE, ">output.txt") or die "cannot open output: $!"; while (<INFILE>) { # note the < and > brackets s/foo/bar/sg; # no =~ needed for $_ # no m needed print OUTFILE; # no "$_\n" needed: $_ is automatic # and the \n is still in $_ (you # don't cho(m)p it... } close INFILE; close OUTFILE;
If you need to replace a multi-line string, you might try setting $/ to undef (slurp the whole file) or "" (read in paragraphs), and maybe adding a /s modifier to the replacement regex.
I'd recommend reading:
perldoc -f open perldoc perlvar (look for the $/ entry) perldoc perlre (for info about regular expressions)
Update: added or die, and close parts, trying not to set a bad example :-)
-- Joost downtime n. The period during which a system is error-free and immune from user input.
In reply to Re: reading/writing line by line
by Joost
in thread reading/writing line by line
by amarceluk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |