Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: option control in script

by kcott (Chancellor)
on Jul 22, 2018 at 11:18 UTC ( #1219045=note: print w/replies, xml ) Need Help??


in reply to option control in script

G'day dideod.yang,

Firstly, I've used this data file for all examples:

$ cat pm_1219033_test_data.txt 0 1 2 3 a b c d () {} [] <>

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.

$ cat pm_1219033_test_shebang.pl #!/usr/bin/perl -lan use strict; use warnings; print $F[2];

Sample run:

$ ./pm_1219033_test_shebang.pl pm_1219033_test_data.txt 2 c []

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:

$ cat ./pm_1219033_test_script.pl print "hello\n"; $ perl -e ./pm_1219033_test_script.pl syntax error at -e line 1, near "." Search pattern not terminated at -e line 1. $ PATH=$PATH:. $ perl -e pm_1219033_test_script.pl $ perl pm_1219033_test_script.pl hello $ perl -e 'print "hello\n";' hello $

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:

$ perl -lane 'print $F[2]' pm_1219033_test_data.txt 2 c [] $ perl -E 'say +(split)[2] while <>' pm_1219033_test_data.txt 2 c [] $ perl -lane 'print "@F[0..2]"' pm_1219033_test_data.txt 0 1 2 a b c () {} [] $ perl -E 'say "@{[(split)[0..2]]}" while <>' pm_1219033_test_data.txt 0 1 2 a b c () {} [] $

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

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1219045]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (2)
As of 2018-07-22 13:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?















    Results (454 votes). Check out past polls.

    Notices?