Slash Boxes
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
More | Login | Reply
Loading... please wait.
  • Your code could be compacted quite a bit, if you change the calling syntax slightly and rely on the while () construct - and the surrounding perl magic :)

    Something like the following

    if (@ARGV < 1) {
        die "usage: fog -|FILE1 ...|-s STRING\n"

    my $fog_index = new Lingua::EN::Fathom;

    if ($ARGV[0] =~ /^-s$/) {
        # string argument
        $fog_index->analyse_block($ARGV[1], 1);
        print $fog_index->report, "\n";

    local $/; # slurp whole files

    • Just curious--what was it in the diction/style package that you wanted to improve?
      • Well, there were a couple of points, some of them are probably a matter of personal taste/preferences ...

        • Consistency - I wanted to stick to Mark's command line arguments and allow the string argument. Personally I would have chosen a "clean" 'while ()' type of interface, meaning if I wanted to know the fog index of a string, I would pass it in via the shell with echo "string" | fog. This way the script behaves like a normal unix command line tool in that it can work as a filter for piping or be used to work on (one or more) files given on the command line. The while () was made for exactly that purpose so I try to use it whenever possible for my scripts. That results in the mentioned consistency - and makes the script even shorter ;-).
        • Doing more work than neccesary - where is the benefit of doing
          $ifh = *STDIN;
          @input = <$ifh>;
          $input = join("\n", @input);
          local $/;
          $input = <STDIN>;
        • Showing the usefulness of $ARGV in connection with the while () loop.
        • Removing unneccesary 'cruft' - why do you need $file_count and $more_files? Why handle the opening, closing, etc. of files given on the command line manually when while () does that automatically for you (including the error messages and more (*))? Why is this handling of files written twice in the code?

        (*) The while () loop has built in error handling, it skips e.g. files (with an error message) that it can't open and continues processing the rest of the files.