in reply to Writing unix-style filters
The "-t", used without any parameter, will return true if your current STDIN is coming from a terminal (as opposed to coming from a pipeline or input redirection). Here's how you might use it:
Try running that without args and without any input pipe or redirection, and you'll see the "Usage:" message.#!/usr/bin/perl use strict; use warnings; my @files; if ( @ARGV ) { @files = grep { -f } @ARGV; } elsif ( -t ) { # true if STDIN is a tty (not a pipe) die "Usage: $0 file [file2 ...]\n or: ls | $0\n"; } else { # get here when STDIN is connected to a pipe @files = <>; chomp @files; } # ... do stuff with @files
Update: <pedantic> I felt compelled to point out that the particular design you want, IMO, is not really a "unix-style filter". For the great majority unix-style filters, the following types of command lines are (more or less) equivalent:
(The first example, of course, demonstrates "useless use of cat", but I included it for pedagogical completeness). </pedantic>cat file1 | tool tool file1 tool < file1 cat file1 file2 ... | tool tool file1 file2 ... # (no equivalent using "<" in this case)
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: Writing unix-style filters
by parv (Parson) on Apr 11, 2007 at 05:03 UTC |