Perl flags -pe, -pi, -p, -w, -d, -i, -t?

☆樱花仙子☆ 提交于 2019-11-26 19:34:08
paxdiablo

Yes, Google is notoriously difficult for looking up punctuation and, unfortunately, Perl does seem to be mostly made up of punctuation :-)

The command line switches are all detailed in perlrun. (available from the command line by calling perldoc perlrun)

Going into the options briefly, one-by-one:

-p: Places a printing loop around your command so that it acts on each
    line of standard input. Used mostly so Perl can beat the
    pants off awk in terms of power AND simplicity :-)
-n: Places a non-printing loop around your command.
-e: Allows you to provide the program as an argument rather
    than in a file. You don't want to have to create a script
    file for every little Perl one-liner.
-i: Modifies your input file in-place (making a backup of the
    original). Handy to modify files without the {copy,
    delete-original, rename} process.
-w: Activates some warnings. Any good Perl coder will use this.
-d: Runs under the Perl debugger. For debugging your Perl code,
    obviously.
-t: Treats certain "tainted" (dubious) code as warnings (proper
    taint mode will error on this dubious code). Used to beef
    up Perl security, especially when running code for other
    users, such as setuid scripts or web stuff.
zellio

The -p flag basically runs the script with

while (<>) {
# exec here
}
continue {
    print or die "-p destination: $!\n";
}

-e allows you to pass a script to STDIN

perl -e '$x = "Hello world!\n"; print $x;'

-i directs the interpreter that all data passed to STDIN by the executing script is to be done inplace.

-w is the same as use warnings;, but in a global rather than local scope

-d runs the Perl debugger

liame

Other have mentioned perlrun. If you use B::Deparse, you can see what it means (for most things):

$ perl -MO=Deparse   -p  -e 1
LINE: while (defined($_ = <ARGV>)) {
    '???';
}
continue {
    die "-p destination: $!\n" unless print $_;
}
-e syntax OK

1 is represented by '???', because it is optimized away.

$ perl -MO=Deparse   -p -i  -e 1
BEGIN { $^I = ""; }
LINE: while (defined($_ = <ARGV>)) {
    '???';
}
continue {
    die "-p destination: $!\n" unless print $_;
}
-e syntax OK

-i sets $^I, like

$ perl -MO=Deparse   -p -i.bak  -e 1
BEGIN { $^I = ".bak"; }
LINE: while (defined($_ = <ARGV>)) {
    '???';
}
continue {
    die "-p destination: $!\n" unless print $_;
}
-e syntax OK

But remember, <ARGV> uses 2-argument open, so don't have filenames that start with > < or start/end with |.

There is also one important flag -n which is not mentioned in the list.

-n works the same as -p, only it does not print $_ by default. This can be very useful in filtering text files.

In this way Perl can replace grep | sed in a single one-liner.

For example:

perl -ne 'print "$1\n" if /Messages read: (\d+)/' <my_input.txt

Will print out every integer value found after "Messages read: ", and nothing more.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!