Boston Linux & Unix (BLU) Home | Calendar | Mail Lists | List Archives | Desktop SIG | Hardware Hacking SIG
Wiki | Flickr | PicasaWeb | Video | Maps & Directions | Installfests | Keysignings
Linux Cafe | Meeting Notes | Blog | Linux Links | Bling | About BLU

BLU Discuss list archive


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

perl/unix question



Hi,

I have a perl / unix question.  It has to do with getting input from the
user.  Below is what I want:

Situation: Running perl script w/o piping data to it

- If the user starts the program with no file list then they are prompted
  for input.
- If the user passes in a file list, that does not contain '-', then they
  are not prompted for input.
- If the user passes in a file list, that does contain '-', then they are
  prompted only when '-' is open.

Situation: Running perl script when piping data to it

- This should never prompt.

Below are examples:

./script.pl
always prompt

./script.pl file -
no prompting when parsing 'file'
prompt when parsing '-'

./script.pl file
never prompt

echo input | ./script.pl
never prompt

echo input | ./script.pl file -
never prompt

Based on my (probably incomplete tests) I have a script that does that.
However, it is IMHO, a less than ideal setup.  I am curious if you can
come up w/ a better version than what I've got.  I have attached my
version.

~'`^`'~=-.,__,.-=~'`^`'~=-.,__,.-=~'`^`'~=-., \|/  (___)  \|/ _,.-=~'`^`
                          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



BLU is a member of BostonUserGroups
BLU is a member of BostonUserGroups
We also thank MIT for the use of their facilities.

Valid HTML 4.01! Valid CSS!



Boston Linux & Unix / webmaster@blu.org