1=head1 NAME 2 3perlrun - how to execute the Perl interpreter 4 5=head1 SYNOPSIS 6 7B<perl> S<[ B<-gsTtuUWX> ]> 8 S<[ B<-h?v> ] [ B<-V>[:I<configvar>] ]> 9 S<[ B<-cw> ] [ B<-d>[B<t>][:I<debugger>] ] [ B<-D>[I<number/list>] ]> 10 S<[ B<-pna> ] [ B<-F>I<pattern> ] [ B<-l>[I<octal>] ] [ B<-0>[I<octal/hexadecimal>] ]> 11 S<[ B<-I>I<dir> ] [ B<-m>[B<->]I<module> ] [ B<-M>[B<->]I<'module...'> ] [ B<-f> ]> 12 S<[ B<-C [I<number/list>] >]> 13 S<[ B<-S> ]> 14 S<[ B<-x>[I<dir>] ]> 15 S<[ B<-i>[I<extension>] ]> 16 S<[ [B<-e>|B<-E>] I<'command'> ] [ B<--> ] [ I<programfile> ] [ I<argument> ]...> 17 18=head1 DESCRIPTION 19 20The normal way to run a Perl program is by making it directly 21executable, or else by passing the name of the source file as an 22argument on the command line. (An interactive Perl environment 23is also possible--see L<perldebug> for details on how to do that.) 24Upon startup, Perl looks for your program in one of the following 25places: 26 27=over 4 28 29=item 1. 30 31Specified line by line via L<-e|/-e commandline> or L<-E|/-E commandline> 32switches on the command line. 33 34=item 2. 35 36Contained in the file specified by the first filename on the command line. 37(Note that systems supporting the C<#!> notation invoke interpreters this 38way. See L</Location of Perl>.) 39 40=item 3. 41 42Passed in implicitly via standard input. This works only if there are 43no filename arguments--to pass arguments to a STDIN-read program you 44must explicitly specify a "-" for the program name. 45 46=back 47 48With methods 2 and 3, Perl starts parsing the input file from the 49beginning, unless you've specified a L</-x> switch, in which case it 50scans for the first line starting with C<#!> and containing the word 51"perl", and starts there instead. This is useful for running a program 52embedded in a larger message. (In this case you would indicate the end 53of the program using the C<__END__> token.) 54 55The C<#!> line is always examined for switches as the line is being 56parsed. Thus, if you're on a machine that allows only one argument 57with the C<#!> line, or worse, doesn't even recognize the C<#!> line, you 58still can get consistent switch behaviour regardless of how Perl was 59invoked, even if L</-x> was used to find the beginning of the program. 60 61Because historically some operating systems silently chopped off 62kernel interpretation of the C<#!> line after 32 characters, some 63switches may be passed in on the command line, and some may not; 64you could even get a "-" without its letter, if you're not careful. 65You probably want to make sure that all your switches fall either 66before or after that 32-character boundary. Most switches don't 67actually care if they're processed redundantly, but getting a "-" 68instead of a complete switch could cause Perl to try to execute 69standard input instead of your program. And a partial L<-I|/-Idirectory> 70switch could also cause odd results. 71 72Some switches do care if they are processed twice, for instance 73combinations of L<-l|/-l[octnum]> and L<-0|/-0[octalE<sol>hexadecimal]>. 74Either put all the switches after the 32-character boundary (if 75applicable), or replace the use of B<-0>I<digits> by 76C<BEGIN{ $/ = "\0digits"; }>. 77 78Parsing of the C<#!> switches starts wherever "perl" is mentioned in the line. 79The sequences "-*" and "- " are specifically ignored so that you could, 80if you were so inclined, say 81 82 #!/bin/sh 83 #! -*- perl -*- -p 84 eval 'exec perl -x -wS $0 ${1+"$@"}' 85 if 0; 86 87to let Perl see the L</-p> switch. 88 89A similar trick involves the I<env> program, if you have it. 90 91 #!/usr/bin/env perl 92 93The examples above use a relative path to the perl interpreter, 94getting whatever version is first in the user's path. If you want 95a specific version of Perl, say, perl5.14.1, you should place 96that directly in the C<#!> line's path. 97 98If the C<#!> line does not contain the word "perl" nor the word "indir", 99the program named after the C<#!> is executed instead of the Perl 100interpreter. This is slightly bizarre, but it helps people on machines 101that don't do C<#!>, because they can tell a program that their SHELL is 102F</usr/bin/perl>, and Perl will then dispatch the program to the correct 103interpreter for them. 104 105After locating your program, Perl compiles the entire program to an 106internal form. If there are any compilation errors, execution of the 107program is not attempted. (This is unlike the typical shell script, 108which might run part-way through before finding a syntax error.) 109 110If the program is syntactically correct, it is executed. If the program 111runs off the end without hitting an exit() or die() operator, an implicit 112C<exit(0)> is provided to indicate successful completion. 113 114=head2 #! and quoting on non-Unix systems 115X<hashbang> X<#!> 116 117Unix's C<#!> technique can be simulated on other systems: 118 119=over 4 120 121=item OS/2 122 123Put 124 125 extproc perl -S -your_switches 126 127as the first line in C<*.cmd> file (L</-S> due to a bug in cmd.exe's 128`extproc' handling). 129 130=item MS-DOS 131 132Create a batch file to run your program, and codify it in 133C<ALTERNATE_SHEBANG> (see the F<dosish.h> file in the source 134distribution for more information). 135 136=item Win95/NT 137 138The Win95/NT installation, when using the ActiveState installer for Perl, 139will modify the Registry to associate the F<.pl> extension with the perl 140interpreter. If you install Perl by other means (including building from 141the sources), you may have to modify the Registry yourself. Note that 142this means you can no longer tell the difference between an executable 143Perl program and a Perl library file. 144 145=item VMS 146 147Put 148 149 $ perl -mysw 'f$env("procedure")' 'p1' 'p2' 'p3' 'p4' 'p5' 'p6' 'p7' 'p8' ! 150 $ exit++ + ++$status != 0 and $exit = $status = undef; 151 152at the top of your program, where B<-mysw> are any command line switches you 153want to pass to Perl. You can now invoke the program directly, by saying 154C<perl program>, or as a DCL procedure, by saying C<@program> (or implicitly 155via F<DCL$PATH> by just using the name of the program). 156 157This incantation is a bit much to remember, but Perl will display it for 158you if you say C<perl "-V:startperl">. 159 160=back 161 162Command-interpreters on non-Unix systems have rather different ideas 163on quoting than Unix shells. You'll need to learn the special 164characters in your command-interpreter (C<*>, C<\> and C<"> are 165common) and how to protect whitespace and these characters to run 166one-liners (see L<-e|/-e commandline> below). 167 168On some systems, you may have to change single-quotes to double ones, 169which you must I<not> do on Unix or Plan 9 systems. You might also 170have to change a single % to a %%. 171 172For example: 173 174 # Unix 175 perl -e 'print "Hello world\n"' 176 177 # MS-DOS, etc. 178 perl -e "print \"Hello world\n\"" 179 180 # VMS 181 perl -e "print ""Hello world\n""" 182 183The problem is that none of this is reliable: it depends on the 184command and it is entirely possible neither works. If I<4DOS> were 185the command shell, this would probably work better: 186 187 perl -e "print <Ctrl-x>"Hello world\n<Ctrl-x>"" 188 189B<CMD.EXE> in Windows NT slipped a lot of standard Unix functionality in 190when nobody was looking, but just try to find documentation for its 191quoting rules. 192 193There is no general solution to all of this. It's just a mess. 194 195=head2 Location of Perl 196X<perl, location of interpreter> 197 198It may seem obvious to say, but Perl is useful only when users can 199easily find it. When possible, it's good for both F</usr/bin/perl> 200and F</usr/local/bin/perl> to be symlinks to the actual binary. If 201that can't be done, system administrators are strongly encouraged 202to put (symlinks to) perl and its accompanying utilities into a 203directory typically found along a user's PATH, or in some other 204obvious and convenient place. 205 206In this documentation, C<#!/usr/bin/perl> on the first line of the program 207will stand in for whatever method works on your system. You are 208advised to use a specific path if you care about a specific version. 209 210 #!/usr/local/bin/perl5.14 211 212or if you just want to be running at least version, place a statement 213like this at the top of your program: 214 215 use v5.14; 216 217=head2 Command Switches 218X<perl, command switches> X<command switches> 219 220As with all standard commands, a single-character switch may be 221clustered with the following switch, if any. 222 223 #!/usr/bin/perl -spi.orig # same as -s -p -i.orig 224 225A C<--> signals the end of options and disables further option processing. Any 226arguments after the C<--> are treated as filenames and arguments. 227 228Switches include: 229 230=over 5 231 232=item B<-0>[I<octal/hexadecimal>] 233X<-0> X<$/> 234 235specifies the input record separator (C<$/>) as an octal or 236hexadecimal number. If there are no digits, the null character is the 237separator. Other switches may precede or follow the digits. For 238example, if you have a version of I<find> which can print filenames 239terminated by the null character, you can say this: 240 241 find . -name '*.orig' -print0 | perl -n0e unlink 242 243The special value 00 will cause Perl to slurp files in paragraph mode. 244 245Any value 0400 or above will cause Perl to slurp files whole, but by convention 246the value 0777 is the one normally used for this purpose. The L</-g> flag 247is a simpler alias for it. 248 249You can also specify the separator character using hexadecimal notation: 250B<-0xI<HHH...>>, where the C<I<H>> are valid hexadecimal digits. Unlike 251the octal form, this one may be used to specify any Unicode character, even 252those beyond 0xFF. So if you I<really> want a record separator of 0777, 253specify it as B<-0x1FF>. (This means that you cannot use the L</-x> option 254with a directory name that consists of hexadecimal digits, or else Perl 255will think you have specified a hex number to B<-0>.) 256 257=item B<-a> 258X<-a> X<autosplit> 259 260turns on autosplit mode when used with a L</-n> or L</-p>. An implicit 261split command to the @F array is done as the first thing inside the 262implicit while loop produced by the L</-n> or L</-p>. 263 264 perl -ane 'print pop(@F), "\n";' 265 266is equivalent to 267 268 while (<>) { 269 @F = split(' '); 270 print pop(@F), "\n"; 271 } 272 273An alternate delimiter may be specified using L<-F|/-Fpattern>. 274 275B<-a> implicitly sets L</-n>. 276 277=item B<-C [I<number/list>]> 278X<-C> 279 280The B<-C> flag controls some of the Perl Unicode features. 281 282As of 5.8.1, the B<-C> can be followed either by a number or a list 283of option letters. The letters, their numeric values, and effects 284are as follows; listing the letters is equal to summing the numbers. 285 286 I 1 STDIN is assumed to be in UTF-8 287 O 2 STDOUT will be in UTF-8 288 E 4 STDERR will be in UTF-8 289 S 7 I + O + E 290 i 8 UTF-8 is the default PerlIO layer for input streams 291 o 16 UTF-8 is the default PerlIO layer for output streams 292 D 24 i + o 293 A 32 the @ARGV elements are expected to be strings encoded 294 in UTF-8 295 L 64 normally the "IOEioA" are unconditional, the L makes 296 them conditional on the locale environment variables 297 (the LC_ALL, LC_CTYPE, and LANG, in the order of 298 decreasing precedence) -- if the variables indicate 299 UTF-8, then the selected "IOEioA" are in effect 300 a 256 Set ${^UTF8CACHE} to -1, to run the UTF-8 caching 301 code in debugging mode. 302 303=for documenting_the_underdocumented 304perl.h gives W/128 as PERL_UNICODE_WIDESYSCALLS "/* for Sarathy */" 305 306=for todo 307perltodo mentions Unicode in %ENV and filenames. I guess that these will be 308options e and f (or F). 309 310For example, B<-COE> and B<-C6> will both turn on UTF-8-ness on both 311STDOUT and STDERR. Repeating letters is just redundant, not cumulative 312nor toggling. 313 314The C<io> options mean that any subsequent open() (or similar I/O 315operations) in main program scope will have the C<:utf8> PerlIO layer 316implicitly applied to them, in other words, UTF-8 is expected from any 317input stream, and UTF-8 is produced to any output stream. This is just 318the default set via L<C<${^OPEN}>|perlvar/${^OPEN}>, 319with explicit layers in open() and with binmode() one can 320manipulate streams as usual. This has no effect on code run in modules. 321 322B<-C> on its own (not followed by any number or option list), or the 323empty string C<""> for the L</PERL_UNICODE> environment variable, has the 324same effect as B<-CSDL>. In other words, the standard I/O handles and 325the default C<open()> layer are UTF-8-fied I<but> only if the locale 326environment variables indicate a UTF-8 locale. This behaviour follows 327the I<implicit> (and problematic) UTF-8 behaviour of Perl 5.8.0. 328(See L<perl581delta/UTF-8 no longer default under UTF-8 locales>.) 329 330You can use B<-C0> (or C<"0"> for C<PERL_UNICODE>) to explicitly 331disable all the above Unicode features. 332 333The read-only magic variable C<${^UNICODE}> reflects the numeric value 334of this setting. This variable is set during Perl startup and is 335thereafter read-only. If you want runtime effects, use the three-arg 336open() (see L<perlfunc/open>), the two-arg binmode() (see L<perlfunc/binmode>), 337and the C<open> pragma (see L<open>). 338 339(In Perls earlier than 5.8.1 the B<-C> switch was a Win32-only switch 340that enabled the use of Unicode-aware "wide system call" Win32 APIs. 341This feature was practically unused, however, and the command line 342switch was therefore "recycled".) 343 344B<Note:> Since perl 5.10.1, if the B<-C> option is used on the C<#!> line, 345it must be specified on the command line as well, since the standard streams 346are already set up at this point in the execution of the perl interpreter. 347You can also use binmode() to set the encoding of an I/O stream. 348 349=item B<-c> 350X<-c> 351 352causes Perl to check the syntax of the program and then exit without 353executing it. Actually, it I<will> execute any C<BEGIN>, C<UNITCHECK>, 354or C<CHECK> blocks and any C<use> statements: these are considered as 355occurring outside the execution of your program. C<INIT> and C<END> 356blocks, however, will be skipped. 357 358If the syntax check is successful perl will exit with a status of zero 359and report C<I<yourprogram> syntax OK>. On failure perl will print 360any detected errors and exit with a non-zero status. 361 362=item B<-d> 363X<-d> X<-dt> 364 365=item B<-dt> 366 367runs the program under the Perl debugger. See L<perldebug>. 368If B<t> is specified, it indicates to the debugger that threads 369will be used in the code being debugged. 370 371=item B<-d:>I<MOD[=bar,baz]> 372X<-d> X<-dt> 373 374=item B<-dt:>I<MOD[=bar,baz]> 375 376runs the program under the control of a debugging, profiling, or tracing 377module installed as C<Devel::I<MOD>>. E.g., B<-d:DProf> executes the 378program using the C<Devel::DProf> profiler. As with the L<-M|/-M[-]module> 379flag, options may be passed to the C<Devel::I<MOD>> package where they will 380be received and interpreted by the C<Devel::I<MOD>::import> routine. Again, 381like B<-M>, use -B<-d:-I<MOD>> to call C<Devel::I<MOD>::unimport> instead of 382import. The comma-separated list of options must follow a C<=> character. 383If B<t> is specified, it indicates to the debugger that threads will be used 384in the code being debugged. See L<perldebug>. 385 386=item B<-D>I<letters> 387X<-D> X<DEBUGGING> X<-DDEBUGGING> 388 389=item B<-D>I<number> 390 391sets debugging flags. This switch is enabled only if your perl binary has 392been built with debugging enabled: normal production perls won't have 393been. 394 395For example, to watch how perl executes your program, use B<-Dtls>. 396Another nice value is B<-Dx>, which lists your compiled syntax tree, and 397B<-Dr> displays compiled regular expressions; the format of the output is 398explained in L<perldebguts>. 399 400As an alternative, specify a number instead of list of letters (e.g., 401B<-D14> is equivalent to B<-Dtls>): 402 403 1 p Tokenizing and parsing (with v, displays parse 404 stack) 405 2 s Stack snapshots (with v, displays all stacks) 406 4 l Context (loop) stack processing 407 8 t Trace execution 408 16 o Method and overloading resolution 409 32 c String/numeric conversions 410 64 P Print profiling info, source file input state 411 128 m Memory and SV allocation 412 256 f Format processing 413 512 r Regular expression parsing and execution 414 1024 x Syntax tree dump 415 2048 u Tainting checks 416 4096 U Unofficial, User hacking (reserved for private, 417 unreleased use) 418 8192 h Show hash randomization debug output (changes to 419 PL_hash_rand_bits and their origin) 420 16384 X Scratchpad allocation 421 32768 D Cleaning up 422 65536 S Op slab allocation 423 131072 T Tokenizing 424 262144 R Include reference counts of dumped variables 425 (eg when using -Ds) 426 524288 J show s,t,P-debug (don't Jump over) on opcodes within 427 package DB 428 1048576 v Verbose: use in conjunction with other flags to 429 increase the verbosity of the output. Is a no-op on 430 many of the other flags 431 2097152 C Copy On Write 432 4194304 A Consistency checks on internal structures 433 8388608 q quiet - currently only suppresses the "EXECUTING" 434 message 435 16777216 M trace smart match resolution 436 33554432 B dump suBroutine definitions, including special 437 Blocks like BEGIN 438 67108864 L trace Locale-related info; what gets output is very 439 subject to change 440 134217728 i trace PerlIO layer processing. Set PERLIO_DEBUG to 441 the filename to trace to. 442 268435456 y trace y///, tr/// compilation and execution 443 444All these flags require B<-DDEBUGGING> when you compile the Perl 445executable (but see C<:opd> in L<Devel::Peek> or L<re/'debug' mode> 446which may change this). 447See the L<INSTALL> file in the Perl source distribution 448for how to do this. 449 450If you're just trying to get a print out of each line of Perl code 451as it executes, the way that C<sh -x> provides for shell scripts, 452you can't use Perl's B<-D> switch. Instead do this 453 454 # If you have "env" utility 455 env PERLDB_OPTS="NonStop=1 AutoTrace=1 frame=2" perl -dS program 456 457 # Bourne shell syntax 458 $ PERLDB_OPTS="NonStop=1 AutoTrace=1 frame=2" perl -dS program 459 460 # csh syntax 461 % (setenv PERLDB_OPTS "NonStop=1 AutoTrace=1 frame=2"; perl -dS program) 462 463See L<perldebug> for details and variations. 464 465=item B<-e> I<commandline> 466X<-e> 467 468may be used to enter one line of program. If B<-e> is given, Perl 469will not look for a filename in the argument list. Multiple B<-e> 470commands may be given to build up a multi-line script. Make sure 471to use semicolons where you would in a normal program. 472 473=item B<-E> I<commandline> 474X<-E> 475 476behaves just like L<-e|/-e commandline>, except that it implicitly 477enables all optional features and builtin functions (in the main 478compilation unit). See L<feature> and L<builtin>. 479 480=item B<-f> 481X<-f> X<sitecustomize> X<sitecustomize.pl> 482 483Disable executing F<$Config{sitelib}/sitecustomize.pl> at startup. 484 485Perl can be built so that it by default will try to execute 486F<$Config{sitelib}/sitecustomize.pl> at startup (in a BEGIN block). 487This is a hook that allows the sysadmin to customize how Perl behaves. 488It can for instance be used to add entries to the @INC array to make Perl 489find modules in non-standard locations. 490 491Perl actually inserts the following code: 492 493 BEGIN { 494 do { local $!; -f "$Config{sitelib}/sitecustomize.pl"; } 495 && do "$Config{sitelib}/sitecustomize.pl"; 496 } 497 498Since it is an actual C<do> (not a C<require>), F<sitecustomize.pl> 499doesn't need to return a true value. The code is run in package C<main>, 500in its own lexical scope. However, if the script dies, C<$@> will not 501be set. 502 503The value of C<$Config{sitelib}> is also determined in C code and not 504read from C<Config.pm>, which is not loaded. 505 506The code is executed I<very> early. For example, any changes made to 507C<@INC> will show up in the output of `perl -V`. Of course, C<END> 508blocks will be likewise executed very late. 509 510To determine at runtime if this capability has been compiled in your 511perl, you can check the value of C<$Config{usesitecustomize}>. 512 513=item B<-F>I<pattern> 514X<-F> 515 516specifies the pattern to split on for L</-a>. The pattern may be 517surrounded by C<//>, C<"">, or C<''>, otherwise it will be put in single 518quotes. You can't use literal whitespace or NUL characters in the pattern. 519 520B<-F> implicitly sets both L</-a> and L</-n>. 521 522=item B<-g> 523X<-g> 524 525undefines the input record separator (C<L<$E<sol>|perlvar/$E<sol>>>) and thus 526enables the slurp mode. In other words, it causes Perl to read whole 527files at once, instead of line by line. 528 529This flag is a simpler alias for L<-0777|/-0[octalE<sol>hexadecimal]>. 530 531Mnemonics: gobble, grab, gulp. 532 533=item B<-h> 534X<-h> 535 536prints a summary of the options. 537 538=item B<-?> 539X<-?> 540 541synonym for B<-h>: prints a summary of the options. 542 543=item B<-i>[I<extension>] 544X<-i> X<in-place> 545 546specifies that files processed by the C<E<lt>E<gt>> construct are to be 547edited in-place. It does this by renaming the input file, opening the 548output file by the original name, and selecting that output file as the 549default for print() statements. The extension, if supplied, is used to 550modify the name of the old file to make a backup copy, following these 551rules: 552 553If no extension is supplied, and your system supports it, the original 554I<file> is kept open without a name while the output is redirected to 555a new file with the original I<filename>. When perl exits, cleanly or not, 556the original I<file> is unlinked. 557 558If the extension doesn't contain a C<*>, then it is appended to the 559end of the current filename as a suffix. If the extension does 560contain one or more C<*> characters, then each C<*> is replaced 561with the current filename. In Perl terms, you could think of this 562as: 563 564 ($backup = $extension) =~ s/\*/$file_name/g; 565 566This allows you to add a prefix to the backup file, instead of (or in 567addition to) a suffix: 568 569 $ perl -pi'orig_*' -e 's/bar/baz/' fileA # backup to 570 # 'orig_fileA' 571 572Or even to place backup copies of the original files into another 573directory (provided the directory already exists): 574 575 $ perl -pi'old/*.orig' -e 's/bar/baz/' fileA # backup to 576 # 'old/fileA.orig' 577 578These sets of one-liners are equivalent: 579 580 $ perl -pi -e 's/bar/baz/' fileA # overwrite current file 581 $ perl -pi'*' -e 's/bar/baz/' fileA # overwrite current file 582 583 $ perl -pi'.orig' -e 's/bar/baz/' fileA # backup to 'fileA.orig' 584 $ perl -pi'*.orig' -e 's/bar/baz/' fileA # backup to 'fileA.orig' 585 586From the shell, saying 587 588 $ perl -p -i.orig -e "s/foo/bar/; ... " 589 590is the same as using the program: 591 592 #!/usr/bin/perl -pi.orig 593 s/foo/bar/; 594 595which is equivalent to 596 597 #!/usr/bin/perl 598 $extension = '.orig'; 599 LINE: while (<>) { 600 if ($ARGV ne $oldargv) { 601 if ($extension !~ /\*/) { 602 $backup = $ARGV . $extension; 603 } 604 else { 605 ($backup = $extension) =~ s/\*/$ARGV/g; 606 } 607 rename($ARGV, $backup); 608 open(ARGVOUT, ">$ARGV"); 609 select(ARGVOUT); 610 $oldargv = $ARGV; 611 } 612 s/foo/bar/; 613 } 614 continue { 615 print; # this prints to original filename 616 } 617 select(STDOUT); 618 619except that the B<-i> form doesn't need to compare $ARGV to $oldargv to 620know when the filename has changed. It does, however, use ARGVOUT for 621the selected filehandle. Note that STDOUT is restored as the default 622output filehandle after the loop. 623 624As shown above, Perl creates the backup file whether or not any output 625is actually changed. So this is just a fancy way to copy files: 626 627 $ perl -p -i'/some/file/path/*' -e 1 file1 file2 file3... 628or 629 $ perl -p -i'.orig' -e 1 file1 file2 file3... 630 631You can use C<eof> without parentheses to locate the end of each input 632file, in case you want to append to each file, or reset line numbering 633(see example in L<perlfunc/eof>). 634 635If, for a given file, Perl is unable to create the backup file as 636specified in the extension then it will skip that file and continue on 637with the next one (if it exists). 638 639For a discussion of issues surrounding file permissions and B<-i>, see 640L<perlfaq5/Why does Perl let me delete read-only files? Why does -i clobber 641protected files? Isn't this a bug in Perl?>. 642 643You cannot use B<-i> to create directories or to strip extensions from 644files. 645 646Perl does not expand C<~> in filenames, which is good, since some 647folks use it for their backup files: 648 649 $ perl -pi~ -e 's/foo/bar/' file1 file2 file3... 650 651Note that because B<-i> renames or deletes the original file before 652creating a new file of the same name, Unix-style soft and hard links will 653not be preserved. 654 655Finally, the B<-i> switch does not impede execution when no 656files are given on the command line. In this case, no backup is made 657(the original file cannot, of course, be determined) and processing 658proceeds from STDIN to STDOUT as might be expected. 659 660=item B<-I>I<directory> 661X<-I> X<@INC> 662 663Directories specified by B<-I> are prepended to the search path for 664modules (C<@INC>). 665 666=item B<-l>[I<octnum>] 667X<-l> X<$/> X<$\> 668 669enables automatic line-ending processing. It has two separate 670effects. First, it automatically chomps C<$/> (the input record 671separator) when used with L</-n> or L</-p>. Second, it assigns C<$\> 672(the output record separator) to have the value of I<octnum> so 673that any print statements will have that separator added back on. 674If I<octnum> is omitted, sets C<$\> to the current value of 675C<$/>. For instance, to trim lines to 80 columns: 676 677 perl -lpe 'substr($_, 80) = ""' 678 679Note that the assignment C<$\ = $/> is done when the switch is processed, 680so the input record separator can be different than the output record 681separator if the B<-l> switch is followed by a 682L<-0|/-0[octalE<sol>hexadecimal]> switch: 683 684 gnufind / -print0 | perl -ln0e 'print "found $_" if -p' 685 686This sets C<$\> to newline and then sets C<$/> to the null character. 687 688=item B<-m>[B<->]I<module> 689X<-m> X<-M> 690 691=item B<-M>[B<->]I<module> 692 693=item B<-M>[B<->]I<'module ...'> 694 695=item B<-[mM]>[B<->]I<module=arg[,arg]...> 696 697B<-m>I<module> executes C<use> I<module> C<();> before executing your 698program. This loads the module, but does not call its C<import> method, 699so does not import subroutines and does not give effect to a pragma. 700 701B<-M>I<module> executes C<use> I<module> C<;> before executing your 702program. This loads the module and calls its C<import> method, causing 703the module to have its default effect, typically importing subroutines 704or giving effect to a pragma. 705You can use quotes to add extra code after the module name, 706e.g., C<'-MI<MODULE> qw(foo bar)'>. 707 708If the first character after the B<-M> or B<-m> is a dash (B<->) 709then the 'use' is replaced with 'no'. 710This makes no difference for B<-m>. 711 712Since Perl version 5.39.8, the C<-M> switch and the module name can now 713be passed in separate command-line arguments: 714 715 perl -M MODULE ... # like perl -MMODULE ... 716 perl -M MODULE=arg1,arg2,... # like perl -MMODULE=arg1,arg2,... 717 718A little builtin syntactic sugar means you can also say 719B<-mI<MODULE>=foo,bar> or B<-MI<MODULE>=foo,bar> as a shortcut for 720B<'-MI<MODULE> qw(foo bar)'>. This avoids the need to use quotes when 721importing symbols. The actual code generated by B<-MI<MODULE>=foo,bar> is 722C<use module split(/,/,q{foo,bar})>. Note that the C<=> form 723removes the distinction between B<-m> and B<-M>; that is, 724B<-mI<MODULE>=foo,bar> is the same as B<-MI<MODULE>=foo,bar>. 725 726A consequence of the C<split> formulation 727is that B<-MI<MODULE>=number> never does a version check, 728unless C<I<MODULE>::import()> itself is set up to do a version check, which 729could happen for example if I<MODULE> inherits from L<Exporter>. 730 731=item B<-n> 732X<-n> 733 734causes Perl to assume the following loop around your program, which 735makes it iterate over filename arguments somewhat like I<sed -n> or 736I<awk>: 737 738 LINE: 739 while (<>) { 740 ... # your program goes here 741 } 742 743Note that the lines are not printed by default. See L</-p> to have 744lines printed. If a file named by an argument cannot be opened for 745some reason, Perl warns you about it and moves on to the next file. 746 747Also note that C<< <> >> passes command line arguments to 748L<perlfunc/open>, which doesn't necessarily interpret them as file names. 749See L<perlop> for possible security implications. 750 751Here is an efficient way to delete all files that haven't been modified for 752at least a week: 753 754 find . -mtime +7 -print | perl -nle unlink 755 756This is faster than using the B<-exec> switch of I<find> because you don't 757have to start a process on every filename found (but it's not faster 758than using the B<-delete> switch available in newer versions of I<find>. 759It does suffer from the bug of mishandling newlines in pathnames, which 760you can fix if you follow the example under 761L<-0|/-0[octalE<sol>hexadecimal]>. 762 763C<BEGIN> and C<END> blocks may be used to capture control before or after 764the implicit program loop, just as in I<awk>. 765 766=item B<-p> 767X<-p> 768 769causes Perl to assume the following loop around your program, which 770makes it iterate over filename arguments somewhat like I<sed>: 771 772 773 LINE: 774 while (<>) { 775 ... # your program goes here 776 } continue { 777 print or die "-p destination: $!\n"; 778 } 779 780If a file named by an argument cannot be opened for some reason, Perl 781warns you about it, and moves on to the next file. Note that the 782lines are printed automatically. An error occurring during printing is 783treated as fatal. To suppress printing use the L</-n> switch. A B<-p> 784overrides a B<-n> switch. 785 786C<BEGIN> and C<END> blocks may be used to capture control before or after 787the implicit loop, just as in I<awk>. 788 789=item B<-s> 790X<-s> 791 792enables rudimentary switch parsing for switches on the command 793line after the program name but before any filename arguments (or before 794an argument of B<-->). Any switch found there is removed from @ARGV and sets the 795corresponding variable in the Perl program, in the main package. The following program 796prints "1" if the program is invoked with a B<-xyz> switch, and "abc" 797if it is invoked with B<-xyz=abc>. 798 799 #!/usr/bin/perl -s 800 if ($xyz) { print "$xyz\n" } 801 802Do note that a switch like B<--help> creates the variable C<${-help}>, which is 803not compliant with C<use strict "refs">. Also, when using this option on a 804script with warnings enabled you may get a lot of spurious "used only once" 805warnings. For these reasons, use of B<-s> is discouraged. See L<Getopt::Long> 806for much more flexible switch parsing. 807 808=item B<-S> 809X<-S> 810 811makes Perl use the L</PATH> environment variable to search for the 812program unless the name of the program contains path separators. 813 814On some platforms, this also makes Perl append suffixes to the 815filename while searching for it. For example, on Win32 platforms, 816the ".bat" and ".cmd" suffixes are appended if a lookup for the 817original name fails, and if the name does not already end in one 818of those suffixes. If your Perl was compiled with C<DEBUGGING> turned 819on, using the L<-Dp|/-Dletters> switch to Perl shows how the search 820progresses. 821 822Typically this is used to emulate C<#!> startup on platforms that don't 823support C<#!>. It's also convenient when debugging a script that uses C<#!>, 824and is thus normally found by the shell's $PATH search mechanism. 825 826This example works on many platforms that have a shell compatible with 827Bourne shell: 828 829 #!/usr/bin/perl 830 eval 'exec /usr/bin/perl -wS $0 ${1+"$@"}' 831 if 0; # ^ Run only under a shell 832 833The system ignores the first line and feeds the program to F</bin/sh>, 834which proceeds to try to execute the Perl program as a shell script. 835The shell executes the second line as a normal shell command, and thus 836starts up the Perl interpreter. On some systems $0 doesn't always 837contain the full pathname, so the L</-S> tells Perl to search for the 838program if necessary. After Perl locates the program, it parses the 839lines and ignores them because the check 'if 0' is never true. 840If the program will be interpreted by csh, you will need 841to replace C<${1+"$@"}> with C<$*>, even though that doesn't understand 842embedded spaces (and such) in the argument list. To start up I<sh> rather 843than I<csh>, some systems may have to replace the C<#!> line with a line 844containing just a colon, which will be politely ignored by Perl. Other 845systems can't control that, and need a totally devious construct that 846will work under any of I<csh>, I<sh>, or Perl, such as the following: 847 848 eval '(exit $?0)' && eval 'exec perl -wS $0 ${1+"$@"}' 849 & eval 'exec /usr/bin/perl -wS $0 $argv:q' 850 if 0; # ^ Run only under a shell 851 852If the filename supplied contains directory separators (and so is an 853absolute or relative pathname), and if that file is not found, 854platforms that append file extensions will do so and try to look 855for the file with those extensions added, one by one. 856 857On DOS-like platforms, if the program does not contain directory 858separators, it will first be searched for in the current directory 859before being searched for on the PATH. On Unix platforms, the 860program will be searched for strictly on the PATH. 861 862=item B<-t> 863X<-t> 864 865Like L</-T>, but taint checks will issue warnings rather than fatal 866errors. These warnings can now be controlled normally with C<no warnings 867qw(taint)>. 868 869B<Note: This is not a substitute for C<-T>!> This is meant to be 870used I<only> as a temporary development aid while securing legacy code: 871for real production code and for new secure code written from scratch, 872always use the real L</-T>. 873 874This has no effect if your perl was built without taint support. 875 876=item B<-T> 877X<-T> 878 879turns on "taint" so you can test them. Ordinarily 880these checks are done only when running setuid or setgid. It's a 881good idea to turn them on explicitly for programs that run on behalf 882of someone else whom you might not necessarily trust, such as CGI 883programs or any internet servers you might write in Perl. See 884L<perlsec> for details. For security reasons, this option must be 885seen by Perl quite early; usually this means it must appear early 886on the command line or in the C<#!> line for systems which support 887that construct. 888 889=item B<-u> 890X<-u> 891 892This switch causes Perl to dump core after compiling your 893program. You can then in theory take this core dump and turn it 894into an executable file by using the I<undump> program (not supplied). 895This speeds startup at the expense of some disk space (which you 896can minimize by stripping the executable). (Still, a "hello world" 897executable comes out to about 200K on my machine.) If you want to 898execute a portion of your program before dumping, use the C<CORE::dump()> 899function instead. Note: availability of I<undump> is platform 900specific and may not be available for a specific port of Perl. 901 902=item B<-U> 903X<-U> 904 905allows Perl to do unsafe operations. Currently the only "unsafe" 906operations are attempting to unlink directories while running as superuser 907and running setuid programs with fatal taint checks turned into warnings. 908Note that warnings must be enabled along with this option to actually 909I<generate> the taint-check warnings. 910 911=item B<-v> 912X<-v> 913 914prints the version and patchlevel of your perl executable. 915 916=item B<-V> 917X<-V> 918 919prints summary of the major perl configuration values and the current 920values of @INC. 921 922=item B<-V:>I<configvar> 923 924Prints to STDOUT the value of the named configuration variable(s), 925with multiples when your C<I<configvar>> argument looks like a regex (has 926non-letters). For example: 927 928 $ perl -V:libc 929 libc='/lib/libc-2.2.4.so'; 930 $ perl -V:lib. 931 libs='-lnsl -lgdbm -ldb -ldl -lm -lcrypt -lutil -lc'; 932 libc='/lib/libc-2.2.4.so'; 933 $ perl -V:lib.* 934 libpth='/usr/local/lib /lib /usr/lib'; 935 libs='-lnsl -lgdbm -ldb -ldl -lm -lcrypt -lutil -lc'; 936 lib_ext='.a'; 937 libc='/lib/libc-2.2.4.so'; 938 libperl='libperl.a'; 939 .... 940 941Additionally, extra colons can be used to control formatting. A 942trailing colon suppresses the linefeed and terminator ";", allowing 943you to embed queries into shell commands. (mnemonic: PATH separator 944":".) 945 946 $ echo "compression-vars: " `perl -V:z.*: ` " are here !" 947 compression-vars: zcat='' zip='zip' are here ! 948 949A leading colon removes the "name=" part of the response, this allows 950you to map to the name you need. (mnemonic: empty label) 951 952 $ echo "goodvfork="`./perl -Ilib -V::usevfork` 953 goodvfork=false; 954 955Leading and trailing colons can be used together if you need 956positional parameter values without the names. Note that in the case 957below, the C<PERL_API> params are returned in alphabetical order. 958 959 $ echo building_on `perl -V::osname: -V::PERL_API_.*:` now 960 building_on 'linux' '5' '1' '9' now 961 962=item B<-w> 963X<-w> 964 965prints warnings about dubious constructs, such as variable names 966mentioned only once and scalar variables used 967before being set; redefined subroutines; references to undefined 968filehandles; filehandles opened read-only that you are attempting 969to write on; values used as a number that don't I<look> like numbers; 970using an array as though it were a scalar; if your subroutines 971recurse more than 100 deep; and innumerable other things. 972 973This switch really just enables the global C<$^W> variable; normally, 974the lexically scoped C<use warnings> pragma is preferred. You 975can disable or promote into fatal errors specific warnings using 976C<__WARN__> hooks, as described in L<perlvar> and L<perlfunc/warn>. 977See also L<perldiag> and L<perltrap>. A fine-grained warning 978facility is also available if you want to manipulate entire classes 979of warnings; see L<warnings>. 980 981=item B<-W> 982X<-W> 983 984Enables all warnings regardless of C<no warnings> or C<$^W>. 985See L<warnings>. 986 987=item B<-X> 988X<-X> 989 990Disables all warnings regardless of C<use warnings> or C<$^W>. 991See L<warnings>. 992 993Forbidden in C<L</PERL5OPT>>. 994 995=item B<-x> 996X<-x> 997 998=item B<-x>I<directory> 999 1000tells Perl that the program is embedded in a larger chunk of unrelated 1001text, such as in a mail message. Leading garbage will be 1002discarded until the first line that starts with C<#!> and contains the 1003string "perl". Any meaningful switches on that line will be applied. 1004 1005All references to line numbers by the program (warnings, errors, ...) 1006will treat the C<#!> line as the first line. 1007Thus a warning on the 2nd line of the program, which is on the 100th 1008line in the file will be reported as line 2, not as line 100. 1009This can be overridden by using the C<#line> directive. 1010(See L<perlsyn/"Plain Old Comments (Not!)">) 1011 1012If a directory name is specified, Perl will switch to that directory 1013before running the program. The B<-x> switch controls only the 1014disposal of leading garbage. The program must be terminated with 1015C<__END__> if there is trailing garbage to be ignored; the program 1016can process any or all of the trailing garbage via the C<DATA> filehandle 1017if desired. 1018 1019The directory, if specified, must appear immediately following the B<-x> 1020with no intervening whitespace. 1021 1022=back 1023 1024=head1 ENVIRONMENT 1025X<perl, environment variables> 1026 1027=over 12 1028 1029=item HOME 1030X<HOME> 1031 1032Used if C<chdir> has no argument. 1033 1034=item LOGDIR 1035X<LOGDIR> 1036 1037Used if C<chdir> has no argument and L</HOME> is not set. 1038 1039=item PATH 1040X<PATH> 1041 1042Used in executing subprocesses, and in finding the program if L</-S> is 1043used. 1044 1045=item PERL5LIB 1046X<PERL5LIB> 1047 1048A list of directories in which to look for Perl library files before 1049looking in the standard library. 1050Any architecture-specific and version-specific directories, 1051such as F<version/archname/>, F<version/>, or F<archname/> under the 1052specified locations are automatically included if they exist, with this 1053lookup done at interpreter startup time. In addition, any directories 1054matching the entries in C<$Config{inc_version_list}> are added. 1055(These typically would be for older compatible perl versions installed 1056in the same directory tree.) 1057 1058If PERL5LIB is not defined, L</PERLLIB> is used. Directories are separated 1059(like in PATH) by a colon on Unixish platforms and by a semicolon on 1060Windows (the proper path separator being given by the command C<perl 1061-V:I<path_sep>>). 1062 1063When running taint checks, either because the program was running setuid or 1064setgid, or the L</-T> or L</-t> switch was specified, neither PERL5LIB nor 1065L</PERLLIB> is consulted. The program should instead say: 1066 1067 use lib "/my/directory"; 1068 1069=item PERL5OPT 1070X<PERL5OPT> 1071 1072Command-line options (switches). Switches in this variable are treated 1073as if they were on every Perl command line. Only the B<-[CDIMTUWdmtw]> 1074switches are allowed. When running taint checks (either because the 1075program was running setuid or setgid, or because the L</-T> or L</-t> 1076switch was used), this variable is ignored. If PERL5OPT begins with 1077B<-T>, tainting will be enabled and subsequent options ignored. If 1078PERL5OPT begins with B<-t>, tainting will be enabled, a writable dot 1079removed from @INC, and subsequent options honored. 1080 1081=item PERLIO 1082X<PERLIO> 1083 1084A space (or colon) separated list of PerlIO layers. If perl is built 1085to use PerlIO system for IO (the default) these layers affect Perl's IO. 1086 1087It is conventional to start layer names with a colon (for example, C<:perlio>) to 1088emphasize their similarity to variable "attributes". But the code that parses 1089layer specification strings, which is also used to decode the PERLIO 1090environment variable, treats the colon as a separator. 1091 1092An unset or empty PERLIO is equivalent to the default set of layers for 1093your platform; for example, C<:unix:perlio> on Unix-like systems 1094and C<:unix:crlf> on Windows and other DOS-like systems. 1095 1096The list becomes the default for I<all> Perl's IO. Consequently only built-in 1097layers can appear in this list, as external layers (such as C<:encoding()>) need 1098IO in order to load them! See L<"open pragma"|open> for how to add external 1099encodings as defaults. 1100 1101Layers it makes sense to include in the PERLIO environment 1102variable are briefly summarized below. For more details see L<PerlIO>. 1103 1104=over 8 1105 1106=item :crlf 1107X<:crlf> 1108 1109A layer which does CRLF to C<"\n"> translation distinguishing "text" and 1110"binary" files in the manner of MS-DOS and similar operating systems, 1111and also provides buffering similar to C<:perlio> on these architectures. 1112 1113=item :perlio 1114X<:perlio> 1115 1116This is a re-implementation of stdio-like buffering written as a 1117PerlIO layer. As such it will call whatever layer is below it for 1118its operations, typically C<:unix>. 1119 1120=item :stdio 1121X<:stdio> 1122 1123This layer provides a PerlIO interface by wrapping system's ANSI C "stdio" 1124library calls. The layer provides both buffering and IO. 1125Note that the C<:stdio> layer does I<not> do CRLF translation even if that 1126is the platform's normal behaviour. You will need a C<:crlf> layer above it 1127to do that. 1128 1129=item :unix 1130X<:unix> 1131 1132Low-level layer that calls C<read>, C<write>, C<lseek>, etc. 1133 1134=back 1135 1136The default set of layers should give acceptable results on all platforms. 1137 1138For Unix platforms that will be the equivalent of ":unix:perlio" or ":stdio". 1139Configure is set up to prefer the ":stdio" implementation if the system's library 1140provides for fast access to the buffer (not common on modern architectures); 1141otherwise, it uses the ":unix:perlio" implementation. 1142 1143On Win32 the default in this release (5.30) is ":unix:crlf". Win32's ":stdio" 1144has a number of bugs/mis-features for Perl IO which are somewhat depending 1145on the version and vendor of the C compiler. Using our own C<:crlf> layer as 1146the buffer avoids those issues and makes things more uniform. 1147 1148This release (5.30) uses C<:unix> as the bottom layer on Win32, and so still 1149uses the C compiler's numeric file descriptor routines. 1150 1151The PERLIO environment variable is completely ignored when Perl 1152is run in taint mode. 1153 1154=item PERLIO_DEBUG 1155X<PERLIO_DEBUG> 1156 1157If set to the name of a file or device when Perl is run with the 1158L<-Di|/-Dletters> command-line switch, the logging of certain operations 1159of the PerlIO subsystem will be redirected to the specified file rather 1160than going to stderr, which is the default. The file is opened in append 1161mode. Typical uses are in Unix: 1162 1163 % env PERLIO_DEBUG=/tmp/perlio.log perl -Di script ... 1164 1165and under Win32, the approximately equivalent: 1166 1167 > set PERLIO_DEBUG=CON 1168 perl -Di script ... 1169 1170This functionality is disabled for setuid scripts, for scripts run 1171with L</-T>, and for scripts run on a Perl built without C<-DDEBUGGING> 1172support. 1173 1174=item PERLLIB 1175X<PERLLIB> 1176 1177A list of directories in which to look for Perl library 1178files before looking in the standard library. 1179If L</PERL5LIB> is defined, PERLLIB is not used. 1180 1181The PERLLIB environment variable is completely ignored when Perl 1182is run in taint mode. 1183 1184=item PERL5DB 1185X<PERL5DB> 1186 1187The command used to load the debugger code. The default is: 1188 1189 BEGIN { require "perl5db.pl" } 1190 1191The PERL5DB environment variable is only used when Perl is started with 1192a bare L</-d> switch. 1193 1194=item PERL5DB_THREADED 1195X<PERL5DB_THREADED> 1196 1197If set to a true value, indicates to the debugger that the code being 1198debugged uses threads. 1199 1200=item PERL5SHELL (specific to the Win32 port) 1201X<PERL5SHELL> 1202 1203On Win32 ports only, may be set to an alternative shell that Perl must use 1204internally for executing "backtick" commands or system(). Default is 1205C<cmd.exe /x/d/c> on WindowsNT and C<command.com /c> on Windows95. The 1206value is considered space-separated. Precede any character that 1207needs to be protected, like a space or backslash, with another backslash. 1208 1209Note that Perl doesn't use COMSPEC for this purpose because 1210COMSPEC has a high degree of variability among users, leading to 1211portability concerns. Besides, Perl can use a shell that may not be 1212fit for interactive use, and setting COMSPEC to such a shell may 1213interfere with the proper functioning of other programs (which usually 1214look in COMSPEC to find a shell fit for interactive use). 1215 1216Before Perl 5.10.0 and 5.8.8, PERL5SHELL was not taint checked 1217when running external commands. It is recommended that 1218you explicitly set (or delete) C<$ENV{PERL5SHELL}> when running 1219in taint mode under Windows. 1220 1221=item PERL_ALLOW_NON_IFS_LSP (specific to the Win32 port) 1222X<PERL_ALLOW_NON_IFS_LSP> 1223 1224Set to 1 to allow the use of non-IFS compatible LSPs (Layered Service Providers). 1225Perl normally searches for an IFS-compatible LSP because this is required 1226for its emulation of Windows sockets as real filehandles. However, this may 1227cause problems if you have a firewall such as I<McAfee Guardian>, which requires 1228that all applications use its LSP but which is not IFS-compatible, because clearly 1229Perl will normally avoid using such an LSP. 1230 1231Setting this environment variable to 1 means that Perl will simply use the 1232first suitable LSP enumerated in the catalog, which keeps I<McAfee Guardian> 1233happy--and in that particular case Perl still works too because I<McAfee 1234Guardian>'s LSP actually plays other games which allow applications 1235requiring IFS compatibility to work. 1236 1237=item PERL_DEBUG_MSTATS 1238X<PERL_DEBUG_MSTATS> 1239 1240Relevant only if Perl is compiled with the C<malloc> included with the Perl 1241distribution; that is, if C<perl -V:d_mymalloc> is "define". 1242 1243If set, this dumps out memory statistics after execution. If set 1244to an integer greater than one, also dumps out memory statistics 1245after compilation. 1246 1247=item PERL_DESTRUCT_LEVEL 1248X<PERL_DESTRUCT_LEVEL> 1249 1250Controls the behaviour of global destruction of objects and other 1251references. See L<perlhacktips/PERL_DESTRUCT_LEVEL> for more information. 1252 1253=item PERL_DL_NONLAZY 1254X<PERL_DL_NONLAZY> 1255 1256Set to C<"1"> to have Perl resolve I<all> undefined symbols when it loads 1257a dynamic library. The default behaviour is to resolve symbols when 1258they are used. Setting this variable is useful during testing of 1259extensions, as it ensures that you get an error on misspelled function 1260names even if the test suite doesn't call them. 1261 1262=item PERL_ENCODING 1263X<PERL_ENCODING> 1264 1265If using the C<use encoding> pragma without an explicit encoding name, the 1266PERL_ENCODING environment variable is consulted for an encoding name. 1267 1268=item PERL_HASH_SEED 1269X<PERL_HASH_SEED> 1270 1271(Since Perl 5.8.1, new semantics in Perl 5.18.0) Used to override 1272the randomization of Perl's internal hash function. The value is expressed 1273in hexadecimal, and may include a leading 0x. Truncated patterns 1274are treated as though they are suffixed with sufficient 0's as required. 1275 1276If the option is provided, and C<PERL_PERTURB_KEYS> is NOT set, then 1277a value of '0' implies C<PERL_PERTURB_KEYS=0>/C<PERL_PERTURB_KEYS=NO> 1278and any other value implies 1279C<PERL_PERTURB_KEYS=2>/C<PERL_PERTURB_KEYS=DETERMINISTIC>. See the 1280documentation for L<PERL_PERTURB_KEYS|/PERL_PERTURB_KEYS> for important 1281caveats regarding the C<DETERMINISTIC> mode. 1282 1283B<PLEASE NOTE: The hash seed is sensitive information>. Hashes are 1284randomized to protect against local and remote attacks against Perl 1285code. By manually setting a seed, this protection may be partially or 1286completely lost. 1287 1288See L<perlsec/"Algorithmic Complexity Attacks">, L</PERL_PERTURB_KEYS>, and 1289L</PERL_HASH_SEED_DEBUG> for more information. 1290 1291=item PERL_PERTURB_KEYS 1292X<PERL_PERTURB_KEYS> 1293 1294(Since Perl 5.18.0) Set to C<"0"> or C<"NO"> then traversing keys 1295will be repeatable from run to run for the same C<PERL_HASH_SEED>. 1296Insertion into a hash will not change the order, except to provide 1297for more space in the hash. When combined with setting PERL_HASH_SEED 1298this mode is as close to pre 5.18 behavior as you can get. 1299 1300When set to C<"1"> or C<"RANDOM"> then traversing keys will be randomized. 1301Every time a hash is inserted into the key order will change in a random 1302fashion. The order may not be repeatable in a following program run 1303even if the PERL_HASH_SEED has been specified. This is the default 1304mode for perl when no PERL_HASH_SEED has been explicitly provided. 1305 1306When set to C<"2"> or C<"DETERMINISTIC"> then inserting keys into a hash 1307will cause the key order to change, but in a way that is repeatable from 1308program run to program run, provided that the same hash seed is used, 1309and that the code does not itself perform any non-deterministic 1310operations and also provided exactly the same environment context. 1311Adding or removing an environment variable may and likely will change 1312the key order. If any part of the code builds a hash using non- 1313deterministic keys, for instance a hash keyed by the stringified form of 1314a reference, or the address of the objects it contains, then this may 1315and likely will have a global effect on the key order of *every* hash in 1316the process. To work properly this setting MUST be coupled with the 1317L<PERL_HASH_SEED|/"PERL_HASH_SEED"> to produce deterministic results, 1318and in fact, if you do set the C<PERL_HASH_SEED> explicitly you do not 1319need to set this as well, it will be automatically set to this mode. 1320 1321B<NOTE:> Use of this option is considered insecure, and is intended only 1322for debugging non-deterministic behavior in Perl's hash function. Do 1323not use it in production. 1324 1325See L<perlsec/"Algorithmic Complexity Attacks"> and L</PERL_HASH_SEED> 1326and L</PERL_HASH_SEED_DEBUG> for more information. You can get and set the 1327key traversal mask for a specific hash by using the C<hash_traversal_mask()> 1328function from L<Hash::Util>. 1329 1330=item PERL_HASH_SEED_DEBUG 1331X<PERL_HASH_SEED_DEBUG> 1332 1333(Since Perl 5.8.1.) Set to C<"1"> to display (to STDERR) information 1334about the hash function, seed, and what type of key traversal 1335randomization is in effect at the beginning of execution. This, combined 1336with L</PERL_HASH_SEED> and L</PERL_PERTURB_KEYS> is intended to aid in 1337debugging nondeterministic behaviour caused by hash randomization. 1338 1339B<Note> that any information about the hash function, especially the hash 1340seed is B<sensitive information>: by knowing it, one can craft a denial-of-service 1341attack against Perl code, even remotely; see L<perlsec/"Algorithmic Complexity Attacks"> 1342for more information. B<Do not disclose the hash seed> to people who 1343don't need to know it. See also L<C<hash_seed()>|Hash::Util/hash_seed> and 1344L<C<hash_traversal_mask()>|Hash::Util/hash_traversal_mask>. 1345 1346An example output might be: 1347 1348 HASH_FUNCTION = ONE_AT_A_TIME_HARD HASH_SEED = 0x652e9b9349a7a032 PERTURB_KEYS = 1 (RANDOM) 1349 1350=item PERL_MEM_LOG 1351X<PERL_MEM_LOG> 1352 1353If your Perl was configured with B<-Accflags=-DPERL_MEM_LOG>, setting 1354the environment variable C<PERL_MEM_LOG> enables logging debug 1355messages. The value has the form C<< <I<number>>[m][s][t] >>, where 1356C<I<number>> is the file descriptor number you want to write to (2 is 1357default), and the combination of letters specifies that you want 1358information about (m)emory and/or (s)v, optionally with 1359(t)imestamps. For example, C<PERL_MEM_LOG=1mst> logs all 1360information to stdout. You can write to other opened file descriptors 1361in a variety of ways: 1362 1363 $ 3>foo3 PERL_MEM_LOG=3m perl ... 1364 1365=item PERL_ROOT (specific to the VMS port) 1366X<PERL_ROOT> 1367 1368A translation-concealed rooted logical name that contains Perl and the 1369logical device for the @INC path on VMS only. Other logical names that 1370affect Perl on VMS include PERLSHR, PERL_ENV_TABLES, and 1371SYS$TIMEZONE_DIFFERENTIAL, but are optional and discussed further in 1372L<perlvms> and in F<README.vms> in the Perl source distribution. 1373 1374=item PERL_SIGNALS 1375X<PERL_SIGNALS> 1376 1377Available in Perls 5.8.1 and later. If set to C<"unsafe">, the pre-Perl-5.8.0 1378signal behaviour (which is immediate but unsafe) is restored. If set 1379to C<safe>, then safe (but deferred) signals are used. See 1380L<perlipc/"Deferred Signals (Safe Signals)">. 1381 1382=item PERL_UNICODE 1383X<PERL_UNICODE> 1384 1385Equivalent to the L<-C|/-C [numberE<sol>list]> command-line switch. Note 1386that this is not a boolean variable. Setting this to C<"1"> is not the 1387right way to "enable Unicode" (whatever that would mean). You can use 1388C<"0"> to "disable Unicode", though (or alternatively unset PERL_UNICODE 1389in your shell before starting Perl). See the description of the 1390L<-C|/-C [numberE<sol>list]> switch for more information. 1391 1392=item PERL_USE_UNSAFE_INC 1393X<PERL_USE_UNSAFE_INC> 1394 1395If perl has been configured to not have the current directory in 1396L<C<@INC>|perlvar/@INC> by default, this variable can be set to C<"1"> 1397to reinstate it. It's primarily intended for use while building and 1398testing modules that have not been updated to deal with "." not being in 1399C<@INC> and should not be set in the environment for day-to-day use. 1400 1401=item SYS$LOGIN (specific to the VMS port) 1402X<SYS$LOGIN> 1403 1404Used if chdir has no argument and L</HOME> and L</LOGDIR> are not set. 1405 1406=item PERL_INTERNAL_RAND_SEED 1407X<PERL_INTERNAL_RAND_SEED> 1408 1409Set to a non-negative integer to seed the random number generator used 1410internally by perl for a variety of purposes. 1411 1412Ignored if perl is run setuid or setgid. Used only for some limited 1413startup randomization (hash keys) if C<-T> or C<-t> perl is started 1414with tainting enabled. 1415 1416Perl may be built to ignore this variable. 1417 1418=item PERL_RAND_SEED 1419X<PERL_RAND_SEED> 1420 1421When set to an integer value this value will be used to seed the perl 1422internal random number generator used for C<rand()> when it is used 1423without an explicit C<srand()> call or for when an explicit no-argument 1424C<srand()> call is made. 1425 1426Normally calling C<rand()> prior to calling C<srand()> or calling 1427C<srand()> explicitly with no arguments should result in the random 1428number generator using "best efforts" to seed the generator state with a 1429relatively high quality random seed. When this environment variable is 1430set then the seeds used will be deterministically computed from the 1431value provided in the env var in such a way that the application process 1432and any forks or threads should continue to have their own unique seed but 1433that the program may be run twice with identical results as far as 1434C<rand()> goes (assuming all else is equal). 1435 1436PERL_RAND_SEED is intended for performance measurements and debugging 1437and is explicitly NOT intended for stable testing. The only guarantee is 1438that a specific perl executable will produce the same results twice in a 1439row, there is no guarantee that the results will be the same between 1440perl releases or on different architectures. 1441 1442Ignored if perl is run setuid or setgid. 1443 1444=back 1445 1446Perl also has environment variables that control how Perl handles data 1447specific to particular natural languages; see L<perllocale>. 1448 1449Perl and its various modules and components, including its test frameworks, 1450may sometimes make use of certain other environment variables. Some of 1451these are specific to a particular platform. Please consult the 1452appropriate module documentation and any documentation for your platform 1453(like L<perlsolaris>, L<perllinux>, L<perlmacosx>, L<perlwin32>, etc) for 1454variables peculiar to those specific situations. 1455 1456Perl makes all environment variables available to the program being 1457executed, and passes these along to any child processes it starts. 1458However, programs running setuid would do well to execute the following 1459lines before doing anything else, just to keep people honest: 1460 1461 $ENV{PATH} = "/bin:/usr/bin"; # or whatever you need 1462 $ENV{SHELL} = "/bin/sh" if exists $ENV{SHELL}; 1463 delete @ENV{qw(IFS CDPATH ENV BASH_ENV)}; 1464 1465=head1 ORDER OF APPLICATION 1466 1467Some options, in particular C<-I>, C<-M>, C<PERL5LIB> and C<PERL5OPT> can 1468interact, and the order in which they are applied is important. 1469 1470Note that this section does not document what I<actually> happens inside the 1471perl interpreter, it documents what I<effectively> happens. 1472 1473=over 1474 1475=item -I 1476 1477The effect of multiple C<-I> options is to C<unshift> them onto C<@INC> 1478from right to left. So for example: 1479 1480 perl -I 1 -I 2 -I 3 1481 1482will first prepend C<3> onto the front of C<@INC>, then prepend C<2>, and 1483then prepend C<1>. The result is that C<@INC> begins with: 1484 1485 qw(1 2 3) 1486 1487=item -M 1488 1489Multiple C<-M> options are processed from left to right. So this: 1490 1491 perl -Mlib=1 -Mlib=2 -Mlib=3 1492 1493will first use the L<lib> pragma to prepend C<1> to C<@INC>, then 1494it will prepend C<2>, then it will prepend C<3>, resulting in an C<@INC> 1495that begins with: 1496 1497 qw(3 2 1) 1498 1499=item the PERL5LIB environment variable 1500 1501This contains a list of directories, separated by colons. The entire list 1502is prepended to C<@INC> in one go. This: 1503 1504 PERL5LIB=1:2:3 perl 1505 1506will result in an C<@INC> that begins with: 1507 1508 qw(1 2 3) 1509 1510=item combinations of -I, -M and PERL5LIB 1511 1512C<PERL5LIB> is applied first, then all the C<-I> arguments, then all the 1513C<-M> arguments. This: 1514 1515 PERL5LIB=e1:e2 perl -I i1 -Mlib=m1 -I i2 -Mlib=m2 1516 1517will result in an C<@INC> that begins with: 1518 1519 qw(m2 m1 i1 i2 e1 e2) 1520 1521=item the PERL5OPT environment variable 1522 1523This contains a space separated list of switches. We only consider the 1524effects of C<-M> and C<-I> in this section. 1525 1526After normal processing of C<-I> switches from the command line, all 1527the C<-I> switches in C<PERL5OPT> are extracted. They are processed from 1528left to right instead of from right to left. Also note that while 1529whitespace is allowed between a C<-I> and its directory on the command 1530line, it is not allowed in C<PERL5OPT>. 1531 1532After normal processing of C<-M> switches from the command line, all 1533the C<-M> switches in C<PERL5OPT> are extracted. They are processed from 1534left to right, I<i.e.> the same as those on the command line. 1535 1536An example may make this clearer: 1537 1538 export PERL5OPT="-Mlib=optm1 -Iopti1 -Mlib=optm2 -Iopti2" 1539 export PERL5LIB=e1:e2 1540 perl -I i1 -Mlib=m1 -I i2 -Mlib=m2 1541 1542will result in an C<@INC> that begins with: 1543 1544 qw( 1545 optm2 1546 optm1 1547 1548 m2 1549 m1 1550 1551 opti2 1552 opti1 1553 1554 i1 1555 i2 1556 1557 e1 1558 e2 1559 ) 1560 1561=item Other complications 1562 1563There are some complications that are ignored in the examples above: 1564 1565=over 1566 1567=item arch and version subdirs 1568 1569All of C<-I>, C<PERL5LIB> and C<use lib> will also prepend arch and version 1570subdirs if they are present 1571 1572=item sitecustomize.pl 1573 1574=back 1575 1576=back 1577