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

I have seen many ways to run Perl code or scripts with different flags. However, when I try to use Google for each flag, I mostly get results for common Perl sites, and no specific information about the flags or their use was found.

Below are the flags that I meet most often, and I donโ€™t know what they mean:

  • perl -pe
  • perl -pi
  • perl -p
  • perl -w
  • perl -d
  • perl -i
  • perl -t

I would be very grateful if you tell me that each of them also means some use cases for them, or at least tell me a way to find out their meaning.

+79
command-line perl flags
Jun 10
source share
5 answers

Yes, Google is notoriously difficult to find punctuation and, unfortunately, Perl seems to consist mainly of punctuation marks :-)

Command line switches are described in detail in perlrun . (accessible from the command line by calling perldoc perlrun )

Incoming options briefly, alternately:

 -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.
+109
Jun 10 2018-11-11T00:
source share

The -p flag basically runs the script with

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

-e allows you to pass the script to STDIN

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

-i tells the interpreter that all data passed to STDIN by the executable script must be executed in place.

-w same as use warnings; but in the global, not the local area

-d launches the Perl debugger

+9
Jun 10 '11 at 4:45
source share

Others mentioned perlrun. If you use B :: Deparse, you can see what this 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 represents "???" because it is optimized.

 $ 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 as

 $ 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 the public argument 2 arguments, so file names starting with > < do not start with / t24>.

+6
Jun 13 2018-11-11T00:
source share

There is also one important flag, -n , which is not listed.

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

This way Perl can replace grep | sed grep | sed on one single line.

For example:

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

Print any integer value found after "Reading messages:", and nothing more.

+3
May 23 '16 at 11:54
source share

As a side element: a very good book on perl one-liners: http://www.catonmat.net/blog/perl-one-liners-no-starch-press/

For a general taste, see the blog entries of the author. It's about -pie, etc.: http://www.catonmat.net/blog/introduction-to-perl-one-liners/ (and for some fun see http://www.catonmat.net/blog / secret-perl-operators / )

0
Oct 13 '17 at 12:54 on
source share



All Articles