in reply to custom args with perl -an type invocation?

Although I like perl's command line switches for one-liners, I question their value inside of scripts (maybe my concept of best practice is wonky). The only one I ever really used was -w but I've given that up for use warnings;. For the example given, I don't see any need for the -a and you can avoid the -n by just coding explicity:

#!/usr/bin/perl our $type ||= shift or die "no type"; while( <> ) { if ( $type eq 'letters' ) { print if $_ =~ /[[:alpha:]]/; } elsif ( $type eq 'numbers' ) { print if $_ =~ /\d/; } else { print "bad type: $type" } }
While the -an and the BEGIN blocks highlight TIMTOWTDI, I find the explicit looping makes the script easier/quicker to understand and will be better suited if/when you shift to argument parsing modules.

-derby

Replies are listed 'Best First'.
Re^2: custom args with perl -an type invocation?
by andyford (Curate) on Jun 27, 2006 at 16:41 UTC
    I would also change
    print "bad type: $type"
    to
    die "bad type: $type"
    just so you don't have to endure output like
    bad type: letterbad type: letterbad type: letterbad type: letterbad ty +pe: letterbad type: letterbad type: letterbad type: letterbad type: l +etterbad type: letterbad type: letterbad type: letterbad type: letter +bad type: letterbad type: letterbad type: letterbad type: letterbad t +ype: letterbad type: letterbad type: letterbad type: letterbad type: +letterbad type: letterbad type: letterbad type: letterbad type: lette +rbad type: letterbad type: letterbad type: letterbad type: letterbad +type: letterbad type: letter$
Re^2: custom args with perl -an type invocation?
by tphyahoo (Vicar) on Jun 29, 2006 at 15:45 UTC
    Here, I think you can just say $type = shift, don't need to say $type ||= shift