Syntactic Confectionery Delight | |
PerlMonks |
Re: option control in scriptby kcott (Archbishop) |
on Jul 22, 2018 at 11:18 UTC ( [id://1219045]=note: print w/replies, xml ) | Need Help?? |
G'day dideod.yang, Firstly, I've used this data file for all examples:
You can add options to the shebang line of your script (see perlrun). Some options, such as -e, don't make any sense on the shebang line. Whether this is useful will depend on the options you want and if you're happy to have the same ones for every run. Here's an example with the -a and -n from your OP; and also using -l because it's useful here.
Sample run:
Note that 'use warnings;' is preferable to -w. See "perlrun: -w" and "warnings: What's wrong with -w and $^W". 'I operate option -ane "perl -ane script.pl". ... but I always forgot option -ane' Well, forgetting the -e in this case would be a good thing: -e expects actual Perl code, not a filename. Here's some examples:
The functionality you want from a particular option may be obtained from a different, but related Perl function (e.g. -l is typically used to avoid writing "\n" after every print statement: say provides this; printf might also be useful here). In other cases, Perl functions may have special features that can provide what was wanted from options. Consider these examples:
Obviously, the purpose of many options is to act as shortcuts; however, writing the code without options doesn't necessarily mean it needs to be substantially longer. Update (minor edit): In the last example, changed [0,1,2] to [0..2]: this was just to keep it closer to the previous example whose functionality it emulates. — Ken
In Section
Seekers of Perl Wisdom
|
|