in reply to Re: Getopt, regexen, and newlines
in thread Getopt, regexen, and newlines

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

Replies are listed 'Best First'.
Re^3: Getopt, regexen, and newlines
by Roy Johnson (Monsignor) on Oct 12, 2005 at 18:54 UTC
    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