in reply to Getopt, regexen, and newlines

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 }

print substr("Just another Perl hacker", 0, -2);
- apotheon
CopyWrite Chad Perrin

Replies are listed 'Best First'.
Re^2: Getopt, regexen, and newlines
by parv (Parson) on Oct 13, 2005 at 03:31 UTC
    Above code when run w/ @ARGV of -d '\n\n' -s '\n' on file containing (there is an empty line after dot)...
    polka
    
    
    dot
    
    

    prints...

    polka\n
    dot\n
    

    ...BTW this input works as expected: -d 'o' -s 'O'; no surprise there.

    Am i specifying the input correctly, or the script still does not do what you wanted initially?

      Actually, I think you're right. I'll have to take another look at it. I haven't paid any attention to this for some time, and pretty much forgot all about it until now.

      Thanks for the reminder, and pointing out the (new?) problem. As I said, I'll have a look.

      EDIT: Okay, now I'm really confused. That regex will fix the input for what's to be substituted, so that you can just delete any instances of two linebreaks (for instance), but won't properly handle the substition if you've got escape characters on the -s argument.

      Yeah, I don't know what it's doing.

      print substr("Just another Perl hacker", 0, -2);
      - apotheon
      CopyWrite Chad Perrin