Assuming you're ok with loading the entire file into memory, you can use the following:
$str =~ s{$pat}{
prompt( $_, $-[0], $+[0]-$-[0], $&, $repl, $`, $' )
? $repl
: $&
}eg
or
use String::Substitution qw( interpolate_match_vars last_match_vars );
$str =~ s{$pat}{
my $true_repl = interpolate_match_vars( $repl, last_match_vars() );
prompt( $_, $-[0], $+[0]-$-[0], $&, $true_repl, $`, $' )
? $true_repl
: $&
}eg
The latter supports interpolation of numerical captures such as $1 and ${1} in $repl, though you might need to escape some dollar signs and backslashes.
It's up to you to provide $str (the contents of the file), $pat (the pattern for which to search as a string or compiled regex), $repl (the replacement string) and prompt.
Args passed to prompt:
- $str
- The position in $str at which the match starts.
- The length of the match.
- The substring of $str that matched.
- The string with which the match will be replaced if prompt returns a true value.
- The substring of $str that precedes the match. Use however much of it that you want.
- The substring of $str that follows the match. Use however much of it that you want.
Have prompt return a true value to replace, or a false value to skip the replacement.
Update: Added second version and a lot of info.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
|
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.