[wplug] sigh...more perl/unix...

bpmedley at 4321.tv bpmedley at 4321.tv
Wed Aug 1 20:39:56 EDT 2001


Here is the actual attachment.

~'`^`'~=-.,__,.-=~'`^`'~=-.,__,.-=~'`^`'~=-., \|/  (___)  \|/ _,.-=~'`^`
                          Brian Medley         @~./'O o`\.~@
"Knowledge is Power" brian.medley at verizon.net /__( \___/ )__\  *PPPFFBT!*
  -- Francis Bacon                               `\__`U_/'
 _,.-=~'`^`'~=-.,__,.-=~'`^`'~=-.,__,.-=~'`^`'~= <____|'  ^^`'~=-.,__,.-=
~`'^`'~=-.,__,.-=~'`^`'~=-.,__,.-=~'`^`'~=-.,__,.-==--^'~=-.,__,.-=~'`^`
-------------- next part --------------
#! /usr/bin/perl -w

use strict;

use POSIX;

# 
# This is a small program that tries to intelligently prompt the user.  Below are some
# cases that show when to prompt the user for input.
#
# case: 
#   ./kill_names.pl
#   always prompt
#
# case:
#   ./kill_names.pl -
#   always prompt
#
# case:
#   ./kill_names.pl file -
#   no prompting when parsing 'file'
#   prompt when parsing '-'
#   
# case: 
#   ./kill_names.pl file
#   never prompt
#   
# case:
#   echo input | ./kill_names.pl 
#   never prompt
#
# case:
#   echo input | ./kill_names.pl file -
#   never prompt
#
# BEGIN prompt if:
#   STDIN is a terminal && first file will be STDIN (i.e. '-')
#       this will happen if no arguments to script or if the '-' filename is passed
#       in.
# 
# START prompting if:
#   STDIN is a terminal && next file will be STDIN.
#

my $isatty;
my $prompt_msg;

my $FH;

my $favorite_food;
my $yucky_food;

# these are needed to determine if we need to prompt the user
$isatty = POSIX::isatty (fileno (STDIN));
@ARGV = ('-') unless @ARGV;

# our personal tastes
$favorite_food = ",cookies,ice cream,cake,linux,penguins,apple pie,";
$yucky_food = ",carrots,microsoft,windows,brussel sprouts,corn,";

while ($FH = shift) {
    open FH, $FH or warn "Can't open $FH: $!\n";

    $prompt_msg = "";
    $prompt_msg = "Feed me: " if "-" eq $FH && $isatty;

    print $prompt_msg;

    while (<FH>) {
        chomp;

        if (/^\s*$/) {
            print "No input detected.\n";
            next;
        }

        # :)
        if ($favorite_food =~ /,$_,/) {
            print "mmm...I like $_\n";
        } elsif ($yucky_food =~ /,$_,/) {
            print "yuck...I hate $_\n";
        } else {
            print "sigh..I guess $_ is OK\n";
        }

    } continue {
        print "$prompt_msg";
    } # end searching the user's file

    # this tries to make the ouput less "jagged" (b/c when the user types EOF a
    # newline is not output)
    if ("-" eq $FH && $isatty && POSIX::isatty (fileno (STDOUT))) {
        print "\n";
    }
} # end parsing file list


More information about the wplug mailing list