About perl

The perl command is the interpreter of the Perl programming language.

  • About perl
  • Description
  • Syntax
  • Running Perl
  • Locations of Perl
  • Options
  • Environment
  • Related commands
  • Syntax
  • Perl Data Types
  • Perl Subroutines
  • Perl Operators
  • Perl Functions
  • Perl Pragmas
  • Linux commands help

Description

“Perl” officially stands for “Practical Extraction and Report Language.” It was originally a language optimized for scanning arbitrary text files, extracting information from those text files, and printing reports based on that information. It quickly became a good language for many system management tasks. Over the years, Perl has grown into a general-purpose programming language. It’s widely used for everything from quick “one-liners” to full-scale application development.

The language is intended to be practical (easy to use, efficient, complete) rather than beautiful (tiny, elegant, minimal). It combines some of the best features of sed, awk, and sh, making it familiar and easy to use for Unix users to whip up quick solutions to annoying problems. Its general-purpose programming facilities support procedural, functional, and object-oriented programming paradigms, making Perl a comfortable language for major projects.

Perl’s roots in text processing haven’t been forgotten over the years. It still boasts some of the most powerful regular expressions to be found anywhere, and its support for Unicode text is world-class. It handles all kinds of structured text, too, through an extensive collection of extensions. Those libraries, collected in the CPAN, provide ready-made solutions to an astounding array of problems.

The Perl motto is “There’s more than one way to do it.”

Running Perl

The normal way to run a Perl program is by making it directly executable, or else by passing the name of the source file as an argument on the command line. (An interactive Perl environment is also possible.) Upon startup, Perl looks for your program in one of the following places:

  • Specified line by line via -e or -E switches on the command line.
  • Contained in the file specified by the first file name on the command line. (Note that systems supporting the #! notation, such as bash, invoke interpreters this way.)
  • Passed in implicitly via standard input. This works only if there are no file name arguments–to pass arguments to a STDIN-read program you must explicitly specify a “-” for the program name.

With methods 2 and 3, Perl starts parsing the input file from the beginning, unless you’ve specified a -x switch, in which case it scans for the first line starting with #! and containing the word “perl”, and starts there instead. This is useful for running a program embedded in a larger message. (In this case you would indicate the end of the program using the END token.)

The #! line is always examined for switches as the line is being parsed. Thus, if you’re on a machine that allows only one argument with the #! line, or worse, doesn’t even recognize the #! line, you still can get consistent switch behaviour regardless of how Perl was invoked, even if -x was used to find the beginning of the program.

Because historically some operating systems silently chopped off kernel interpretation of the #! line after 32 characters, some switches may be passed in on the command line, and some may not; you could even get a “-” without its letter, if you’re not careful. You probably want to make sure that all your switches fall either before or after that 32-character boundary. Most switches don’t actually care if they’re processed redundantly, but getting a “-” instead of a complete switch could cause Perl to try to execute standard input instead of your program. And a partial -I switch could also cause odd results.

Some switches do care if they are processed twice, for instance combinations of -l and -0. Either put all the switches after the 32-character boundary (if applicable), or replace the use of -0digits by BEGIN{ $/ = “\0digits”; }.

Parsing of the #! switches starts wherever “perl” is mentioned in the line. The sequences “-*” and “- " are specifically ignored.

If the #! line does not contain the word “perl” nor the word “indir” the program named after the #! is executed instead of the Perl interpreter. This is slightly bizarre, but it helps people on machines that don’t do #!, because they can tell a program that their SHELL environment variable is /usr/bin/perl, and Perl then dispatches the program to the correct interpreter for them.

After locating your program, Perl compiles the entire program to an internal form. If there are any compilation errors, execution of the program is not attempted. (This is unlike the typical shell script, which might run part-way through before finding a syntax error.)

If the program is syntactically correct, it is executed. If the program runs off the end without hitting an exit() or die() operator, an implicit exit(0) is provided to indicate successful completion.

Location of Perl

Perl can be located wherever you choose, but it’s best for both /usr/bin/perl and /usr/local/bin/perl to be symlinks to the actual binary. If that can’t be done, system administrators are strongly encouraged to put symlinks to perl and its accompanying utilities into a directory often found along a user’s PATH, or in some other obvious and convenient place.

In this documentation, #!/usr/bin/perl on the first line of the program stands in for whatever method works on your system. You are advised to use a specific path if you care about a specific version:

#!/usr/local/bin/perl5.14

or if you only want to be running (at least) a certain version, place a statement like this at the top of your program:

use 5.014;

Syntax

perl [ -sTtuUWX ] [ -hv ] [ -V[:configvar] ] [ -cw ] [ -d[t][:debugger] ] [ -D[number/list] ] [ -pna ] [ -Fpattern ] [ -l[octal] ] [ -0[octal/hexadecimal] ] [ -Idir ] [ -m[-]module ] [ -M[-]‘module…’ ] [ -f ] [ -C [number/list] ] [ -S ] [ -x[dir] ] [ -i[extension] ] [ [-e|-E] ‘command’ ] [ – ] [ programfile ] [ argument ]…

Options

perl accepts the following command-line arguments:

Environment

The following environment variables affect the operation of perl:

find . -name ‘*.orig’ -print0 | perl -n0e unlink

perl -ane ‘print pop(@F), “\n”;’

while (<>) { @F = split(’ ‘); print pop(@F), “\n”;}

If you have “env” utility

env PERLDB_OPTS=“NonStop=1 AutoTrace=1 frame=2” perl -dS program

Bourne shell syntax

PERLDB_OPTS=“NonStop=1 AutoTrace=1 frame=2” perl -dS program

csh syntax

(setenv PERLDB_OPTS “NonStop=1 AutoTrace=1 frame=2”; perl -dS program)

BEGIN { do { local $!; -f “$Config{sitelib}/sitecustomize.pl”; } && do “$Config{sitelib}/sitecustomize.pl”;}

($backup = $extension) =~ s/*/$file_name/g;

perl -pi’orig_*’ -e ’s/bar/baz/’ fileA # backup to ‘orig_fileA’

perl -pi’old/*.orig’ -e ’s/bar/baz/’ fileA # backup to ‘old/fileA.orig’

overwrite current fileperl -pi -e ’s/bar/baz/’ fileA # overwrite current fileperl -pi’’ -e ’s/bar/baz/’ fileA # backup to ‘fileA.orig’perl -pi’.orig’ -e ’s/bar/baz/’ fileA # backup to ‘fileA.orig’perl -pi’.orig’ -e ’s/bar/baz/’ fileA

perl -p -i.orig -e “s/foo/bar/; … "

#!/usr/bin/perl -pi.orig s/foo/bar/;

#!/usr/bin/perl$extension = ‘.orig’;LINE: while (<>) { if ($ARGV ne $oldargv) { if ($extension !~ /*/) { $backup = $ARGV . $extension; } else { ($backup = $extension) =~ s/*/$ARGV/g; } rename($ARGV, $backup); open(ARGVOUT, “>$ARGV”); select(ARGVOUT); $oldargv = $ARGV; } s/foo/bar/;}continue { print; # this prints to original file name}select(STDOUT);

perl -p -i’/some/file/path/*’ -e 1 file1 file2 file3…

perl -p -i’.orig’ -e 1 file1 file2 file3…

perl -pi~ -e ’s/foo/bar/’ file1 file2 file3…

perl -lpe ‘substr($_, 80) = “”’

gnufind / -print0 | perl -ln0e ‘print “found $_” if -p’

LINE: while (<>) { … # your program goes here}

find . -mtime +7 -print | perl -nle unlink

LINE: while (<>) { … # your program goes here } continue { print or die “-p destination: $!\n”;}

#!/usr/bin/perl -sif ($xyz) { print “$xyz\n” }

#!/usr/bin/perleval ’exec /usr/bin/perl -wS $0 ${1+"[email protected]”}‘if $running_under_some_shell;

eval ‘(exit $?0)’ && eval ’exec perl -wS $0 ${1+"[email protected]”}’& eval ’exec /usr/bin/perl -wS $0 $argv:q’if $running_under_some_shell;

perl -V:libc

libc=’/lib/libc-2.2.4.so’;

perl -V:lib.

libs=’-lnsl -lgdbm -ldb -ldl -lm -lcrypt -lutil -lc’;libc=’/lib/libc-2.2.4.so’;

perl -V:lib.*

libpth=’/usr/local/lib /lib /usr/lib’;libs=’-lnsl -lgdbm -ldb -ldl -lm -lcrypt -lutil -lc’;lib_ext=’.a’;libc=’/lib/libc-2.2.4.so’;libperl=‘libperl.a’;….

echo “compression-vars: " perl -V:z.*: " are here !”

compression-vars: zcat=’’ zip=‘zip’ are here !

echo “goodvfork="./perl -Ilib -V::usevfork

goodvfork=false;

echo building_on perl -V::osname: -V::PERL_API_.*: now

building_on ’linux’ ‘5’ ‘1’ ‘9’ now

Perl and its various modules and components, including its test frameworks, may sometimes make use of certain other environment variables. Some of these are specific to a particular platform. Please consult the appropriate module documentation and any documentation for your platform for variables peculiar to those specific situations.

use lib “/my/directory”;

env PERLIO_DEBUG=/dev/tty perl script …

set PERLIO_DEBUG=CONperl script …

BEGIN { require “perl5db.pl” }

HASH_FUNCTION = ONE_AT_A_TIME_HARD HASH_SEED = 0x652e9b9349a7a032 PERTURB_KEYS = 1 (RANDOM)

$ 3>foo3 PERL_MEM_LOG=3m perl …

Perl makes all environment variables available to the program being executed, and passes these along to any child processes it starts. However, programs running setuid would do well to execute the following lines before doing anything else, only to keep people honest:

$ENV{PATH} = “/bin:/usr/bin”; # or whatever you need $ENV{SHELL} = “/bin/sh” if exists $ENV{SHELL}; delete @ENV{qw(IFS CDPATH ENV BASH_ENV)};

Also see:

Syntax Perl Data Types Perl Subroutines Perl Operators Perl Functions Perl Pragmas

awk — Interpreter for the AWK text processing programming language. sed — A utility for filtering and transforming text.