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


In reply to Re: option control in script by kcott
in thread option control in script by dideod.yang

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.