SPUG: Re: Multiple uses of <STDIN>

Tim Maher/CONSULTIX tim at consultix-inc.com
Fri Oct 15 16:05:34 CDT 1999


>> Message submitted at: Fri Oct 15 14:05:34 PDT 1999
X-Mailer: ELM [version 2.4 PL25]
MIME-Version: 1.0
Content-Type: text/plain; charset=US-ASCII
Content-Transfer-Encoding: 7bit
Content-Length: 2294      

According to El JoPe Magnifico:
> 
> So why doesn't it work when you pipe the results from 'find' to it?
> Because when piping, the shell connects the STDIN of 'test.pl' to the
> STDOUT of 'find' rather than to the usual /dev/tty.  I don't know
> offhand of any way that you can funnel both input sources into the
> single STDIN (basically, an input version of 'tee').

There's no problem with merging two input sources into a single
input stream:

(find . -print;  cat /dev/tty) | script
find . -print | (cat - /dev/tty) | script	# another way

However, with this arrangement, his initial @array=<STDIN> wouldn't
sense EOF until ^D from the keyboard, so he'd need to provide a marker,
and have his script watch for it, with associated code to fill the
array and stop on seeing the EOF word.

(find . -temp;  echo EOF; cat /dev/tty) | script

Too much trouble, and too much sophistication required on the
part of the user typing in the command line, who would rather
just run the script!

 . . .

> You can even take advantage of perl's -n switch, so you don't even
> need to put the while loop in your code at all.  Try the following,
> which should allow you to pipe the output of 'find' into it...
> 
> #!/usr/bin/perl -n
> 
> ## This gets run once (automatically) before the loop
> sub BEGIN {
> 	open (TTY, "</dev/tty") or die("Could not open tty.");
> }
> 
> ## The loop created by -p assigns the STDIN current line to $_

You meant -n in that line ^^^

> my $line = $_;
> chomp ($line);

Right, and if he used -ln instead of -n, he could even skip the chomp() !

> 
> ## Do whatever additional per-line-item stuff here
> print "line = '$line': [Y:N] "; 
> my $y_or_n = <TTY>;  ## Shouldn't need ^D, just [enter]	
> chomp ($y_or_n);
> print "yorn = '$y_or_n'\n\n";
> 
> ## Do _not_ put exit() at the end, or it won't loop properly!

Unless that exit is in an END{ } block.

*==================================================================*
| Tim Maher, PhD  CEO, Consultix &    (206) 781-UNIX/8649          |
|      Pacific Software Gurus, Inc.   Email: tim at consultix-inc.com |
| "The UNIX/Perl Training Experts"    http://www.consultix-inc.com |
|Classes: 11/1 Shell/Utils  11/15 Adv Shell  12/7 LINUX  12/13 Perl|
*==================================================================*

 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    POST TO: spug-list at pm.org        PROBLEMS: owner-spug-list at pm.org
 Seattle Perl Users Group (SPUG) Home Page: http://www.halcyon.com/spug/
 SUBSCRIBE/UNSUBSCRIBE: Replace ACTION below by subscribe or unsubscribe
        Email to majordomo at pm.org: ACTION spug-list your_address





More information about the spug-list mailing list