Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Change</p> <pre><code>foreach my $f (@files){ if ($f =~ s/thisgoesout/thisgoesin/gi) { #inplace file editing, or something like that } } </code></pre> <p>To</p> <pre><code>foreach my $f (@files){ open my $in, '&lt;', $f; open my $out, '&gt;', "$f.out"; while (my $line = &lt;$in&gt;){ chomp $line; $line =~ s/thisgoesout/thisgoesin/gi print $out "$line\n"; } } </code></pre> <p>This assumes that the pattern doesn't span multiple lines. If the pattern might span lines, you'll need to slurp in the file contents. ("slurp" is a pretty common Perl term).</p> <p>The chomp isn't actually necessary, I've just been bitten by lines that weren't <code>chomp</code>ed one too many times (if you drop the <code>chomp</code>, change <code>print $out "$line\n";</code> to <code>print $out $line;</code>).</p> <p>Likewise, you can change <code>open my $out, '&gt;', "$f.out";</code> to <code>open my $out, '&gt;', undef;</code> to open a temporary file and then copy that file back over the original when the substitution's done. In fact, and especially if you slurp in the whole file, you can simply make the substitution in memory and then write over the original file. But I've made enough mistakes doing that that I always write to a new file, and verify the contents.</p> <hr> <p><strong>Note</strong>, I originally had an if statement in that code. That was most likely wrong. That would have only copied over lines that matched the regular expression "thisgoesout" (replacing it with "thisgoesin" of course) while silently gobbling up the rest.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload