in reply to Getopt, regexen, and newlines

The shell is probably what is sanitizing your arguments. How did you try to call it? With single-quotes around the strings, I hope?

Incidentally, there is a shell tool that will squeeze newlines.

tr -s '\n' < file > file.squeezed
I'm afraid your script has no real advantage over the one-liner:
perl -pe 's/from-expression/to-expression/g' file > new_file
or the essentially-the-same
sed 's/from-expression/to-expression/g' file > new_file

Caution: Contents may have been coded under pressure.

Replies are listed 'Best First'.
Re^2: Getopt, regexen, and newlines
by apotheon (Deacon) on Oct 12, 2005 at 18:40 UTC

    See my response to Corion above this one, re: the shell.

    This script does have an advantage over the Perl and sed one-liners you posted, however: You don't have to know either Perl or sed to use it, which was sorta the point.

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

      You don't have to know either Perl or sed to use it
      You don't have to know Perl or sed to use the one-liners, either. You just need to know those particular commands. The amount of knowledge isn't much different in any case. The one advantage of your script is that it will explain its calling syntax (if the user remembers the -h argument).

      I'd probably rewrite it without Getopts. Just take three parameters. Spit out the help if there aren't three parameters. Users can specify '' for an empty parameter. Something like:

      my ($from, $to) = (shift, shift); @ARGV == 1 or die "Usage: $0 from_string to_string filename\n"; while (<>) { s/\Q$from/$to/g; print; }

      Caution: Contents may have been coded under pressure.

        I prefer to avoid forcing users to specify arguments in a particular order to avoid accidentally screwing up their source files, and I prefer to not have to write more control structures than necessary (such as to allow rearranging of arguments). Of course, if there's no way to do this with Getopt, "necessary" might turn out to be what writing more control structures becomes.

        In any case, this isn't just about making this one trivial script work better: it's about learning how to use what's available in Perl. The example script is just what got me to this point.

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