By the way, there is a mode in Perl to run like sed, editing files in place. Like in sed, this mode is activated with -i switch, described in perlrun. In a Perl program this mode can be controlled via the $^I variable. There is also special ARGV filehandle which iterates over command-line filenames in @ARGV. It's easy to combine these two to edit a file without slurping it into memory:
{
local ($^I, @ARGV) = (".bak", $filename);
# $filename is renamed to "$filename.bak" and new "$filename" is open
+ed for writing
while (<>) { # ARGV filehandle is opened to read from "$filename.bak"
s/foo/bar/g;
print;
}
} # at this point @ARGV and $^I are restored to normal
This might look too low-level. There are modules implementing this behaviour: File::Inplace and IO::InSitu. The latter is described in the Perl Best Practices book by Damian Conway.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|