dideod.yang has asked for the wisdom of the Perl Monks concerning the following question:

Hi monks. I wonder that Can script control option by itself?? For example, when I want to use @F(automatic split line by line) , I operate option -ane "perl -ane script.pl". also there are many options such as -n -e ... Recently I write some script to open text file and really useful on @F. but I always forgot option -ane :(.. I know I am not man.. So I need your help. When I operate script "perl script.pl" then script is operated on "-ane" to use @F. Thank you :)

Replies are listed 'Best First'.
Re: option control in script
by Corion (Patriarch) on Jul 22, 2018 at 06:18 UTC

    The easy way is to look at B::Deparse and use the code that Perl writes for you in the background:

    perl -MO=Deparse -lane "print $F[2]"
    BEGIN { $/ = "\n"; $\ = "\n"; } LINE: while (defined($_ = readline ARGV)) { chomp $_; our @F = split(' ', $_, 0); print $F[2]; }

    So, the line

    our @F = split(' ', $_, 0);

    basically is the -a command line switch.

Re: option control in script
by haukex (Archbishop) on Jul 22, 2018 at 07:16 UTC

    Yes, almost every option on the command line translates to Perl code that you can use in your script. This is covered almost entirely in perlrun (see this for the -i switch). Corion showed how to use B::Deparse, but note that the code it shows is often a little longer than it needs to be. For example, perl -lane 'print $F[2]' can be written like the following. I've added warnings and strict, and used my @F instead of our @F and local $\ to limit their scope.

    use warnings; use strict; local $\ = "\n"; # -l while (<>) { # -n chomp; # -l my @F = split; # -a print $F[2]; # -e }

    Also note that Perl parses command-line options on the shebang line. So for example, to get the effect of -s (rudimentary command-line switches) without having to specify it on the command line:

    $ cat s.pl #!/usr/bin/perl -s use warnings; use strict; our $x; print "<$x>\n"; $ ./s.pl -x=foo <foo>
      Under Windows, you normally do not have a shebang line. You can add one to specify run-time switches. It has no other effect.
      Bill
        It has no other effect.

        I know I'm nitpicking, but for example, Apache will read it (ScriptInterpreterSource), and I'm going to take a guess that Cygwin might do so too.

Re: option control in script
by kcott (Archbishop) on Jul 22, 2018 at 11:18 UTC

    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