Step 1. Your code, because of the -n, is all wrapped up in a loop. Which means that your "my $type = shift" statement will be executed each time through the loop. It also means that your $type is lexical to each time through the loop. Changing that line to:
solves that by creating $type as a global, and only setting it once (using ||=), and doing it before the loop (using BEGIN).our $type; BEGIN { $type ||= shift or die "no type"; }
Step 2: fixing the regex. \w is "word" characters, which includes letters and numbers and the _. You probably want [[:alpha:]]. In which case, the total program looks like this:
Tested. Hope that helps.#!/usr/bin/perl -an our $type; BEGIN { $type ||= shift or die "no type"; } if ( $type eq 'letters' ) { print if $_ =~ /[[:alpha:]]/; } elsif ( $type eq 'numbers' ) { print if $_ =~ /\d/; } else { print "bad type: $type" }
PS: ++ - that's really a cool use for perl, too ;-)
In reply to Re: custom args with perl -an type invocation?
by Tanktalus
in thread custom args with perl -an type invocation?
by tphyahoo
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |