in reply to Re^2: a simple exercise in readability
in thread a simple exercise in readability

First of all, apotheon, that's a very nice syntax message.  So many times, when I've run a script or program which gave no clue as to its purpose, I've wished for a syntax message even 1/10th as good as that one, so kudos for writing something clear and descriptive!

Secondly, instead of:

HELP_MESSAGE() and exit unless $ARGV[1]; foreach (@ARGV) { HELP_MESSAGE() and exit unless /^-?\d+$/ } # ... and later ... sub HELP_MESSAGE { print $help; }

why not just do the following, which is (I think) more clear, takes less space, and is more precise in the case of invalid input ...

die $help unless $ARGV[1]; map { /^-?\d+$/ or die "Invalid input $_\n" } @ARGV;

s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/

Replies are listed 'Best First'.
Re^4: a simple exercise in readability
by apotheon (Deacon) on Jan 15, 2007 at 15:40 UTC

    Thanks for the compliments on my help message text. I do try to be clear.

    You may have a point, re: your replacement code, with the exception that eliminating the subroutine definition for HELP_MESSAGE() would cause the script to fail to produce the contents of $help when invoked with the --help option. Thus, my two explicit conditional invocations of HELP_MESSAGE() should probably be replaced as you indicated, though I'd keep the subroutine. New code:

    die $help unless $ARGV[1]; map { /^-?\d+$/ or die "Invalid input $_\n" } @ARGV; # . . . and later . . . sub HELP_MESSAGE { print $help; }

    If I don't keep that subroutine in there, the --help option causes the following output to be produced:

    /home/ren/bin/ies version 1.0 calling Getopt::Std::getopts (version 1. +05), running under Perl version 5.8.8. Usage: series [-OPTIONS [-MORE_OPTIONS]] [--] [PROGRAM_ARG1 ...] The following single-character options are accepted: Boolean (without arguments): -s Options may be merged together. -- stops processing of options.

    edit: johngg pointed out the "unless" bug above, where unless @ARGV should read unless defined @ARGV instead.

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

      My eyes could be playing me tricks again but I think your

      die $help unless $ARGV[1];

      and it's previous incarnations will die erroneously if the user wants to sum from, say, -7 to 0. Perhaps unless defined $ARGV[1]; would be better?

      Cheers,

      JohnGG

        Good catch. Thanks -- I've edited the code on my system at home, though I'll leave the error as it is where you noticed it here at PerlMonks.

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