I think jdporter covered things pretty well. One more trick you might want to know: the "-t" function (one of the things described in perlfunc, particularly in the section that appears when you run "perldoc -f -X").

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:

#!/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
Try running that without args and without any input pipe or redirection, and you'll see the "Usage:" message.

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:

cat file1 | tool tool file1 tool < file1 cat file1 file2 ... | tool tool file1 file2 ... # (no equivalent using "<" in this case)
(The first example, of course, demonstrates "useless use of cat", but I included it for pedagogical completeness). </pedantic>

In reply to Re: Writing unix-style filters by graff
in thread Writing unix-style filters by njcodewarrior

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.