There's more than one way to do things | |
PerlMonks |
Re: user input Regular Expressionby jcb (Parson) |
on Dec 14, 2020 at 01:03 UTC ( [id://11125145]=note: print w/replies, xml ) | Need Help?? |
If this is a command-line tool, have you considered simply using sed(1)? You seem to be reimplementing it. If there is some frontend interface here more complex (like a CGI script, other Web toolkit, or a GUI, like Tk) then you are mostly on the right track, but it would be far better to write to a new file and then use rename to replace the existing file after the process is complete. Something like: (untested)
Note that the s/// pattern-match-replacement operator returns the number of matches found, so there is no need in this program to separately count the matches. Also note that using $$ (the process ID) to construct temporary filenames is only suitable in a secure and trusted environment but is very simple for a demonstration; you should probably use File::Temp and the DIR option to tempfile to ensure that the output file is created in the same directory as the input file. (The rename builtin can only be guaranteed (barring "disk full" and other errors) to work within the same directory.)
In Section
Seekers of Perl Wisdom
|
|