Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: option control in script

by kcott (Archbishop)
on Jul 22, 2018 at 11:18 UTC ( [id://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
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1219045]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (5)
As of 2024-04-24 11:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found