I don't know why I didn't think of this earlier. I've just added some code to the script that iteratively replaces \\ with \ in %arguments before the arguments are dumped into the regex at the heart of this little utility. I guess that was just too simple for me to think of right away.
Here's the inserted code:
foreach $key (keys %argument) { $argument{$key} =~ s/\\\\/\\/g; }
I just inserted that between the getopts() statement and my if statement. The complete script now looks something like this:
#!/usr/bin/perl use strict; use Getopt::Std; my (%argument, @contents, $contents, $key); getopts('hd:s:', \%argument); foreach $key (keys %argument) { $argument{$key} =~ s/\\\\/\\/g; } if ($argument{h}) { helptext() }else{ open(FILEHANDLE, "< $ARGV[0]") or die "cannot open file: $!"; $contents = do{local $/; <FILEHANDLE>}; $contents =~ s/$argument{d}/$argument{s}/g; print $contents; close(FILEHANDLE); } sub helptext { print <<"EOT"; ===== syntax: fts [-h] -d <string> [-s <string>] <file> -h prints this help text and exits: invoking the help argument causes all other arguments to be discarded by this utility -d takes string as input, searches for that string: replaces with string from -s or with an empty string if no -s argument is specified -s takes string as input, substitutes it for string specified by the -d argument -- if not specified, text matching the -d argument will simply be deleted <file> specifies [path and] name of file to take as input, on whose contents this utility operates description: fts (aka "file text substitution") takes a file's contents as input and operates upon them, doing a simple find and replace operation globally throughout the file. The results are dumped to STDOUT, so the original file is untouched. If you want the original file to be overwritten with the new contents, use a shell redirect. bugs: There are no known bugs at this time. credits: Chad L. Perrin (author) Perlmonks community (contributors) license: This utility released under CCD CopyWrite. See following URL for details. http://ccd.apotheon.org EOT }
|
- apotheon
CopyWrite Chad Perrin |
In reply to Re: Getopt, regexen, and newlines
by apotheon
in thread Getopt, regexen, and newlines
by apotheon
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |