I'm acclimatizing to the "unix" way, and have found some situations where it's useful to process output of another command with pipes, through a custom perl script. Now, invoking the script with "perl -an" is often useful, I like it because it behaves in an intuitive way when dealing with pipes.

Now I have some little utilities built on "perl -an" that I would like to make more powerful by allowing flags to specify certain types of behavior. A simple example follows, where I'd like to select either letters or numbers from my piped output. Here, it doesn't work because I guess perl -an wants another file where I am trying to give it a flag.

Is there some way to do what I want with "perl -an" type invocation? Or do I have to use the diamond operator on STDOUT, like I was used to doing before I discovered perl -an?

Thanks... :)

$cat input.txt | ./anWithArgs.pl letters # should spit out lines with +a letter Can't open letters: Datei oder Verzeichnis nicht gefunden. $cat input.txt | ./anWithArgs.pl numbers # should spit out lines with +a number Can't open numbers: Datei oder Verzeichnis nicht gefunden. $cat anWithArgs.pl #!/usr/bin/perl -an my $type = shift or die "no type"; if ( $type eq 'letters' ) { print if $_ =~ /\w/; } elsif ( $type eq 'numbers' ) { print if $_ =~ /\d/; } else { print "bad type: $type" } $cat input.txt a b c d e f g 1 2 3 4 5 6 7 8 9

In reply to custom args with perl -an type invocation? by tphyahoo

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.