• No results found

PERL(1) Perl Programmers Reference Guide PERL(1)

N/A
N/A
Protected

Academic year: 2021

Share "PERL(1) Perl Programmers Reference Guide PERL(1)"

Copied!
286
0
0

Loading.... (view fulltext now)

Full text

(1)

NAME

perl − The Perl 5 language interpreter SYNOPSIS

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 ]...

For more information on these options, you can runperldoc perlrun. GETTING HELP

The perldoc program gives you access to all the documentation that comes with Perl. You can get more documentation, tutorials and community support online at <http://www.perl.org/>.

If you’re new to Perl, you should start by runningperldoc perlintro, which is a general intro for beginners and provides some background to help you navigate the rest of Perl’s extensive documentation. Runperldoc perldoc to learn more things you can do with perldoc.

For ease of access, the Perl manual has been split up into several sections. Overview

perl Perl overview (this section) perlintro Perl introduction for beginners perlrun Perl execution and options

perltoc Perl documentation table of contents Tutorials

perlreftut Perl references short introduction perldsc Perl data structures intro

perllol Perl data structures: arrays of arrays perlrequick Perl regular expressions quick start perlretut Perl regular expressions tutorial perlootut Perl OO tutorial for beginners

perlperf Perl Performance and Optimization Techniques perlstyle Perl style guide

perlcheat Perl cheat sheet

perltrap Perl traps for the unwary perldebtut Perl debugging tutorial

perlfaq Perl frequently asked questions perlfaq1 General Questions About Perl perlfaq2 Obtaining and Learning about Perl perlfaq3 Programming Tools

perlfaq4 Data Manipulation perlfaq5 Files and Formats perlfaq6 Regexes

perlfaq7 Perl Language Issues perlfaq8 System Interaction perlfaq9 Networking

(2)

perlsyn Perl syntax

perldata Perl data structures

perlop Perl operators and precedence perlsub Perl subroutines

perlfunc Perl built−in functions perlopentut Perl open() tutorial

perlpacktut Perl pack() and unpack() tutorial perlpod Perl plain old documentation

perlpodspec Perl plain old documentation format specification perlpodstyle Perl POD style guide

perldiag Perl diagnostic messages perldeprecation Perl deprecations

perllexwarn Perl warnings and their control perldebug Perl debugging

perlvar Perl predefined variables

perlre Perl regular expressions, the rest of the story perlrebackslash Perl regular expression backslash sequences perlrecharclass Perl regular expression character classes perlreref Perl regular expressions quick reference perlref Perl references, the rest of the story perlform Perl formats

perlobj Perl objects

perltie Perl objects hidden behind simple variables perldbmfilter Perl DBM filters

perlipc Perl interprocess communication perlfork Perl fork() information

perlnumber Perl number semantics perlthrtut Perl threads tutorial perlport Perl portability guide perllocale Perl locale support

perluniintro Perl Unicode introduction perlunicode Perl Unicode support perlunicook Perl Unicode cookbook perlunifaq Perl Unicode FAQ

perluniprops Index of Unicode properties in Perl perlunitut Perl Unicode tutorial

perlebcdic Considerations for running Perl on EBCDIC platforms perlsec Perl security

perlmod Perl modules: how they work

perlmodlib Perl modules: how to write and use

perlmodstyle Perl modules: how to write modules with style perlmodinstall Perl modules: how to install from CPAN

perlnewmod Perl modules: preparing a new module for distribution perlpragma Perl modules: writing a user pragma

perlutil utilities packaged with the Perl distribution perlfilter Perl source filters

perldtrace Perl's support for DTrace perlglossary Perl Glossary

(3)

Internals and C Language Interface

perlembed Perl ways to embed perl in your C or C++ application perldebguts Perl debugging guts and tips

perlxstut Perl XS tutorial

perlxs Perl XS application programming interface perlxstypemap Perl XS C/Perl type conversion tools

perlclib Internal replacements for standard C library functions perlguts Perl internal functions for those doing extensions perlcall Perl calling conventions from C

perlmroapi Perl method resolution plugin interface perlreapi Perl regular expression plugin interface perlreguts Perl regular expression engine internals perlapi Perl API listing (autogenerated)

perlintern Perl internal functions (autogenerated)

perliol C API for Perl's implementation of IO in Layers perlapio Perl internal IO abstraction interface

perlhack Perl hackers guide

perlsource Guide to the Perl source tree

perlinterp Overview of the Perl interpreter source and how it works perlhacktut Walk through the creation of a simple C code patch

perlhacktips Tips for Perl core C code hacking perlpolicy Perl development policies

perlgit Using git with the Perl repository Miscellaneous

perlbook Perl book information perlcommunity Perl community information

perldoc Look up Perl documentation in Pod format perlhist Perl history records

perldelta Perl changes since previous version perl5260delta Perl changes in version 5.26.0 perl5242delta Perl changes in version 5.24.2 perl5241delta Perl changes in version 5.24.1 perl5240delta Perl changes in version 5.24.0 perl5224delta Perl changes in version 5.22.4 perl5223delta Perl changes in version 5.22.3 perl5222delta Perl changes in version 5.22.2 perl5221delta Perl changes in version 5.22.1 perl5220delta Perl changes in version 5.22.0 perl5203delta Perl changes in version 5.20.3 perl5202delta Perl changes in version 5.20.2 perl5201delta Perl changes in version 5.20.1 perl5200delta Perl changes in version 5.20.0 perl5184delta Perl changes in version 5.18.4 perl5182delta Perl changes in version 5.18.2 perl5181delta Perl changes in version 5.18.1 perl5180delta Perl changes in version 5.18.0 perl5163delta Perl changes in version 5.16.3 perl5162delta Perl changes in version 5.16.2 perl5161delta Perl changes in version 5.16.1 perl5160delta Perl changes in version 5.16.0 perl5144delta Perl changes in version 5.14.4 perl5143delta Perl changes in version 5.14.3 perl5142delta Perl changes in version 5.14.2 perl5141delta Perl changes in version 5.14.1

(4)

perl5140delta Perl changes in version 5.14.0 perl5125delta Perl changes in version 5.12.5 perl5124delta Perl changes in version 5.12.4 perl5123delta Perl changes in version 5.12.3 perl5122delta Perl changes in version 5.12.2 perl5121delta Perl changes in version 5.12.1 perl5120delta Perl changes in version 5.12.0 perl5101delta Perl changes in version 5.10.1 perl5100delta Perl changes in version 5.10.0 perl589delta Perl changes in version 5.8.9 perl588delta Perl changes in version 5.8.8 perl587delta Perl changes in version 5.8.7 perl586delta Perl changes in version 5.8.6 perl585delta Perl changes in version 5.8.5 perl584delta Perl changes in version 5.8.4 perl583delta Perl changes in version 5.8.3 perl582delta Perl changes in version 5.8.2 perl581delta Perl changes in version 5.8.1 perl58delta Perl changes in version 5.8.0 perl561delta Perl changes in version 5.6.1 perl56delta Perl changes in version 5.6 perl5005delta Perl changes in version 5.005 perl5004delta Perl changes in version 5.004

perlexperiment A listing of experimental features in Perl perlartistic Perl Artistic License

perlgpl GNU General Public License Language-Specific

perlcn Perl for Simplified Chinese (in EUC−CN) perljp Perl for Japanese (in EUC−JP)

perlko Perl for Korean (in EUC−KR)

perltw Perl for Traditional Chinese (in Big5) Platform-Specific

perlaix Perl notes for AIX perlamiga Perl notes for AmigaOS perlandroid Perl notes for Android

perlbs2000 Perl notes for POSIX−BC BS2000 perlce Perl notes for WinCE

perlcygwin Perl notes for Cygwin perldos Perl notes for DOS perlfreebsd Perl notes for FreeBSD perlhaiku Perl notes for Haiku perlhpux Perl notes for HP−UX perlhurd Perl notes for Hurd perlirix Perl notes for Irix perllinux Perl notes for Linux

perlmacos Perl notes for Mac OS (Classic) perlmacosx Perl notes for Mac OS X

perlnetware Perl notes for NetWare perlopenbsd Perl notes for OpenBSD perlos2 Perl notes for OS/2 perlos390 Perl notes for OS/390 perlos400 Perl notes for OS/400 perlplan9 Perl notes for Plan 9 perlqnx Perl notes for QNX perlriscos Perl notes for RISC OS perlsolaris Perl notes for Solaris

(5)

perlsymbian Perl notes for Symbian perlsynology Perl notes for Synology perltru64 Perl notes for Tru64 perlvms Perl notes for VMS

perlvos Perl notes for Stratus VOS perlwin32 Perl notes for Windows Stubs for Deleted Documents

perlboot perlbot perlrepository perltodo perltooc perltoot

On Debian systems, you need to install the perl-doc package which contains the majority of the standard Perl documentation and the perldoc program.

Extensive additional documentation for Perl modules is available, both those distributed with Perl and third-party modules which are packaged or locally installed.

You should be able to view Perl’s documentation with your man (1) program or perldoc (1).

Some documentation is not available as man pages, so if a cross-reference is not found by man, try it with perldoc. Perldoc can also take you directly to documentation for functions (with the −f switch). See perldoc −−help (or perldoc perldoc or man perldoc) for other helpful options perldoc has to offer.

In general, if something strange has gone wrong with your program and you’re not sure where you should look for help, try making your code comply with use strict and use warnings. These will often point out exactly where the trouble is.

DESCRIPTION

Perl officially stands for Practical Extraction and Report Language, except when it doesn’t.

Perl 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 (in the author’s opinion, anyway) 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 the long haul on major projects, whatever your bent. 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. When they haven’t set the standard themselves, they steal from the best — just like Perl itself.

AV AILABILITY

Perl is available for most operating systems, including virtually all Unix-like platforms. See ‘‘Supported Platforms’’ in perlport for a listing.

ENVIRONMENT See perlrun. AUTHOR

Larry Wall <[email protected]>, with the help of oodles of other folks.

If your Perl success stories and testimonials may be of help to others who wish to advocate the use of Perl in their applications, or if you wish to simply express your gratitude to Larry and the Perl developers, please write to perl−[email protected] .

(6)

FILES

"@INC" locations of perl libraries

‘‘@INC’’ above is a reference to the built-in variable of the same name; see perlvar for more information. SEE ALSO

http://www.perl.org/ the Perl homepage

http://www.perl.com/ Perl articles (O'Reilly)

http://www.cpan.org/ the Comprehensive Perl Archive http://www.pm.org/ the Perl Mongers

DIAGNOSTICS

Using the use strict pragma ensures that all variables are properly declared and prevents other misuses of legacy Perl features.

Theuse warnings pragma produces some lovely diagnostics. One can also use the −w flag, but its use is normally discouraged, because it gets applied to all executed Perl code, including that not under your control.

See perldiag for explanations of all Perl’s diagnostics. The use diagnostics pragma automatically turns Perl’s normally terse warnings and errors into these longer forms.

Compilation errors will tell you the line number of the error, with an indication of the next token or token type that was to be examined. (In a script passed to Perl via −e switches, each −e is counted as one line.) Setuid scripts have additional constraints that can produce error messages such as ‘‘Insecure dependency’’. See perlsec.

Did we mention that you should definitely consider using the use warnings pragma? BUGS

The behavior implied by the use warnings pragma is not mandatory.

Perl is at the mercy of your machine’s definitions of various operations such as type casting, atof(), and floating-point output with sprintf().

If your stdio requires a seek or eof between reads and writes on a particular stream, so does Perl. (This doesn’t apply to sysread() and syswrite().)

While none of the built-in data types have any arbitrary size limits (apart from memory size), there are still a few arbitrary limits: a giv en variable name may not be longer than 251 characters. Line numbers displayed by diagnostics are internally stored as short integers, so they are limited to a maximum of 65535 (higher numbers usually being affected by wraparound).

You may mail your bug reports (be sure to include full configuration information as output by the myconfig program in the perl source tree, or byperl −V) to [email protected] . If you’ve succeeded in compiling perl, the perlbug script in the utils/ subdirectory can be used to help mail in a bug report.

Perl actually stands for Pathologically Eclectic Rubbish Lister, but don’t tell anyone I said that. NOTES

The Perl motto is ‘‘There’s more than one way to do it.’’ Divining how many more is left as an exercise to the reader.

The three principal virtues of a programmer are Laziness, Impatience, and Hubris. See the Camel Book for why.

(7)

NAME

perlsyn − Perl syntax DESCRIPTION

A Perl program consists of a sequence of declarations and statements which run from the top to the bottom. Loops, subroutines, and other control structures allow you to jump around within the code.

Perl is a free-form language: you can format and indent it however you like. Whitespace serves mostly to separate tokens, unlike languages like Python where it is an important part of the syntax, or Fortran where it is immaterial.

Many of Perl’s syntactic elements are optional. Rather than requiring you to put parentheses around every function call and declare every variable, you can often leave such explicit elements off and Perl will figure out what you meant. This is known as Do What I Mean, abbreviatedDWIM. It allows programmers to be lazy and to code in a style with which they are comfortable.

Perl borrows syntax and concepts from many languages: awk, sed, C, Bourne Shell, Smalltalk, Lisp and ev en English. Other languages have borrowed syntax from Perl, particularly its regular expression extensions. So if you have programmed in another language you will see familiar pieces in Perl. They often work the same, but see perltrap for information about how they differ.

Declarations

The only things you need to declare in Perl are report formats and subroutines (and sometimes not even subroutines). A scalar variable holds the undefined value (undef) until it has been assigned a defined value, which is anything other thanundef. When used as a number, undef is treated as 0; when used as a string, it is treated as the empty string,""; and when used as a reference that isn’t being assigned to, it is treated as an error. If you enable warnings, you’ll be notified of an uninitialized value whenever you treat undef as a string or a number. Well, usually. Boolean contexts, such as:

if ($a) {}

are exempt from warnings (because they care about truth rather than definedness). Operators such as++, −−, +=, −=, and .=, that operate on undefined variables such as:

undef $a; $a++;

are also always exempt from such warnings.

A declaration can be put anywhere a statement can, but has no effect on the execution of the primary sequence of statements: declarations all take effect at compile time. All declarations are typically put at the beginning or the end of the script. However, if you’re using lexically-scoped private variables created with my(), state(), or our(), you’ll have to make sure your format or subroutine definition is within the same block scope as the my if you expect to be able to access those private variables.

Declaring a subroutine allows a subroutine name to be used as if it were a list operator from that point forward in the program. You can declare a subroutine without defining it by sayingsub name, thus:

sub myname;

$me = myname $0 or die "can't get myname";

A bare declaration like that declares the function to be a list operator, not a unary operator, so you have to be careful to use parentheses (or or instead of ||.) The || operator binds too tightly to use after list operators; it becomes part of the last element. You can always use parentheses around the list operators arguments to turn the list operator back into something that behaves more like a function call. Alternatively, you can use the prototype($) to turn the subroutine into a unary operator:

sub myname ($);

$me = myname $0 || die "can't get myname";

That now parses as you’d expect, but you still ought to get in the habit of using parentheses in that situation. For more on prototypes, see perlsub.

Subroutines declarations can also be loaded up with therequire statement or both loaded and imported into your namespace with ause statement. See perlmod for details on this.

A statement sequence may contain declarations of lexically-scoped variables, but apart from declaring a variable name, the declaration acts like an ordinary statement, and is elaborated within the sequence of statements as if it were an ordinary statement. That means it actually has both compile-time and run-time

(8)

effects. Comments

Te xt from a"#" character until the end of the line is a comment, and is ignored. Exceptions include "#" inside a string or regular expression.

Simple Statements

The only kind of simple statement is an expression evaluated for its side-effects. Every simple statement must be terminated with a semicolon, unless it is the final statement in a block, in which case the semicolon is optional. But put the semicolon in anyway if the block takes up more than one line, because you may ev entually add another line. Note that there are operators likeeval {}, sub {}, and do {} that look like compound statements, but aren’t — they’re just TERMs in an expression — and thus need an explicit termination when used as the last item in a statement.

Truth and Falsehood

The number 0, the strings'0' and "", the empty list (), and undef are all false in a boolean context. All other values are true. Negation of a true value by ! or not returns a special false value. When evaluated as a string it is treated as"", but as a number, it is treated as 0. Most Perl operators that return true or false behave this way.

Statement Modifiers

Any simple statement may optionally be followed by a SINGLE modifier, just before the terminating semicolon (or block ending). The possible modifiers are:

if EXPR unless EXPR while EXPR until EXPR for LIST foreach LIST when EXPR

TheEXPR following the modifier is referred to as the ‘‘condition’’. Its truth or falsehood determines how the modifier will behave.

if executes the statement once if and only if the condition is true. unless is the opposite, it executes the statement unless the condition is true (that is, if the condition is false).

print "Basset hounds got long ears" if length $ear >= 10; go_outside() and play() unless $is_raining;

Thefor(each) modifier is an iterator: it executes the statement once for each item in theLIST(with$_ aliased to each item in turn).

print "Hello $_!\n" for qw(world Dolly nurse);

while repeats the statement while the condition is true. until does the opposite, it repeats the statement until the condition is true (or while the condition is false):

# Both of these count from 0 to 10. print $i++ while $i <= 10;

print $j++ until $j > 10;

The while and until modifiers have the usual "while loop" semantics (conditional evaluated first), except when applied to ado−BLOCK (or to the Perl4 do−SUBROUTINE statement), in which case the block executes once before the conditional is evaluated.

This is so that you can write loops like: do {

$line = <STDIN>; ...

} until !defined($line) || $line eq ".\n"

See ‘‘do’’ in perlfunc. Note also that the loop control statements described later will NOTwork in this construct, because modifiers don’t take loop labels. Sorry. You can always put another block inside of it (fornext/redo) or around it (for last) to do that sort of thing.

(9)

do {{

next if $x == $y; # do something here }} until $x++ > $z;

Forlast, you have to be more elaborate and put braces around it: { do { last if $x == $y**2; # do something here } while $x++ <= $z; }

If you need bothnext and last, you have to do both and also use a loop label: LOOP: {

do {{

next if $x == $y;

last LOOP if $x == $y**2; # do something here

}} until $x++ > $z; }

NOTE: The behaviour of a my, state, or our modified with a statement modifier conditional or loop construct (for example,my $x if ...) is undefined. The value of the my variable may be undef, any previously assigned value, or possibly anything else. Don’t rely on it. Future versions of perl might do something different from the version of perl you try it out on. Here be dragons.

Thewhen modifier is an experimental feature that first appeared in Perl 5.14. To use it, you should include ause v5.14 declaration. (Technically, it requires only the switch feature, but that aspect of it was not available before 5.14.) Operative only from within a foreach loop or a given block, it executes the statement only if the smartmatch$_ ˜˜ EXPR is true. If the statement executes, it is followed by a next from inside aforeach and break from inside a given.

Under the current implementation, the foreach loop can be anywhere within the when modifier’s dynamic scope, but must be within thegiven block’s lexical scope. This restriction may be relaxed in a future release. See ‘‘Switch Statements’’ below.

Compound Statements

In Perl, a sequence of statements that defines a scope is called a block. Sometimes a block is delimited by the file containing it (in the case of a required file, or the program as a whole), and sometimes a block is delimited by the extent of a string (in the case of an eval).

But generally, a block is delimited by curly brackets, also known as braces. We will call this syntactic construct aBLOCK.

The following compound statements may be used to control flow: if (EXPR) BLOCK

if (EXPR) BLOCK else BLOCK

if (EXPR) BLOCK elsif (EXPR) BLOCK ...

if (EXPR) BLOCK elsif (EXPR) BLOCK ... else BLOCK unless (EXPR) BLOCK

unless (EXPR) BLOCK else BLOCK

unless (EXPR) BLOCK elsif (EXPR) BLOCK ...

unless (EXPR) BLOCK elsif (EXPR) BLOCK ... else BLOCK given (EXPR) BLOCK

LABEL while (EXPR) BLOCK

LABEL while (EXPR) BLOCK continue BLOCK LABEL until (EXPR) BLOCK

(10)

LABEL until (EXPR) BLOCK continue BLOCK LABEL for (EXPR; EXPR; EXPR) BLOCK LABEL for VAR (LIST) BLOCK

LABEL for VAR (LIST) BLOCK continue BLOCK LABEL foreach (EXPR; EXPR; EXPR) BLOCK LABEL foreach VAR (LIST) BLOCK

LABEL foreach VAR (LIST) BLOCK continue BLOCK LABEL BLOCK

LABEL BLOCK continue BLOCK PHASE BLOCK

The experimentalgiven statement is not automatically enabled; see ‘‘Switch Statements’’ below for how to do so, and the attendant caveats.

Unlike in C and Pascal, in Perl these are all defined in terms of BLOCKs, not statements. This means that the curly brackets are required−−no dangling statements allowed. If you want to write conditionals without curly brackets, there are several other ways to do it. The following all do the same thing:

if (!open(FOO)) { die "Can't open $FOO: $!" } die "Can't open $FOO: $!" unless open(FOO); open(FOO) || die "Can't open $FOO: $!"; open(FOO) ? () : die "Can't open $FOO: $!";

# a bit exotic, that last one

The if statement is straightforward. Because BLOCKs are always bounded by curly brackets, there is never any ambiguity about whichif an else goes with. If you use unless in place of if, the sense of the test is reversed. Likeif, unless can be followed by else. unless can even be followed by one or more elsif statements, though you may want to think twice before using that particular language construct, as everyone reading your code will have to think at least twice before they can understand what’s going on.

Thewhile statement executes the block as long as the expression is true. The until statement executes the block as long as the expression is false. TheLABELis optional, and if present, consists of an identifier followed by a colon. The LABEL identifies the loop for the loop control statements next, last, and redo. If theLABEL is omitted, the loop control statement refers to the innermost enclosing loop. This may include dynamically looking back your call-stack at run time to find the LABEL. Such desperate behavior triggers a warning if you use theuse warnings pragma or the −w flag.

If there is acontinueBLOCK,it is always executed just before the conditional is about to be evaluated again. Thus it can be used to increment a loop variable, even when the loop has been continued via the next statement.

When a block is preceding by a compilation phase keyword such as BEGIN, END, INIT, CHECK, or UNITCHECK, then the block will run only during the corresponding phase of execution. See perlmod for more details.

Extension modules can also hook into the Perl parser to define new kinds of compound statements. These are introduced by a keyword which the extension recognizes, and the syntax following the keyword is defined entirely by the extension. If you are an implementor, see ‘‘PL_keyword_plugin’’ in perlapi for the mechanism. If you are using such a module, see the module’s documentation for details of the syntax that it defines.

Loop Control

Thenext command starts the next iteration of the loop: LINE: while (<STDIN>) {

next LINE if /ˆ#/; # discard comments ...

}

(11)

executed:

LINE: while (<STDIN>) {

last LINE if /ˆ$/; # exit when done with header ...

}

The redo command restarts the loop block without evaluating the conditional again. The continue block, if any, is not executed. This command is normally used by programs that want to lie to themselves about what was just input.

For example, when processing a file like /etc/termcap. If your input lines might end in backslashes to indicate continuation, you want to skip ahead and get the next record.

while (<>) { chomp;

if (s/\\$//) { $_ .= <>;

redo unless eof(); }

# now process $_ }

which is Perl shorthand for the more explicitly written version: LINE: while (defined($line = <ARGV>)) {

chomp($line);

if ($line =˜ s/\\$//) { $line .= <ARGV>;

redo LINE unless eof(); # not eof(ARGV)! }

# now process $line }

Note that if there were a continue block on the above code, it would get executed only on lines discarded by the regex (since redo skips the continue block). A continue block is often used to reset line counters orm?pat? one-time matches:

# inspired by :1,$g/fred/s//WILMA/ while (<>) {

m?(fred)? && s//WILMA $1 WILMA/; m?(barney)? && s//BETTY $1 BETTY/; m?(homer)? && s//MARGE $1 MARGE/; } continue {

print "$ARGV $.: $_";

close ARGV if eof; # reset $.

reset if eof; # reset ?pat? }

If the wordwhile is replaced by the word until, the sense of the test is reversed, but the conditional is still tested before the first iteration.

Loop control statements don’t work in an if or unless, since they aren’t loops. You can double the braces to make them such, though.

if (/pattern/) {{ last if /fred/;

next if /barney/; # same effect as "last",

# but doesn't document as well # do something here

}}

This is caused by the fact that a block by itself acts as a loop that executes once, see ‘‘Basic BLOCKs’’. The form while/if BLOCK BLOCK, available in Perl 4, is no longer available. Replace any occurrence ofif BLOCK by if (do BLOCK).

(12)

For Loops

Perl’s C−stylefor loop works like the corresponding while loop; that means that this: for ($i = 1; $i < 10; $i++) {

... }

is the same as this: $i = 1; while ($i < 10) { ... } continue { $i++; }

There is one minor difference: if variables are declared withmy in the initialization section of the for, the lexical scope of those variables is exactly thefor loop (the body of the loop and the control sections). As a special case, if the test in thefor loop (or the corresponding while loop) is empty, it is treated as true. That is, both

for (;;) { ... } and while () { ... }

are treated as infinite loops.

Besides the normal array index looping,for can lend itself to many other interesting applications. Here’s one that avoids the problem you get into if you explicitly test for end-of-file on an interactive file descriptor causing your program to appear to hang.

$on_a_tty = −t STDIN && −t STDOUT;

sub prompt { print "yes? " if $on_a_tty } for ( prompt(); <STDIN>; prompt() ) {

# do something }

Usingreadline (or the operator form, <EXPR>) as the conditional of a for loop is shorthand for the following. This behaviour is the same as awhile loop conditional.

for ( prompt(); defined( $_ = <STDIN> ); prompt() ) { # do something

} Foreach Loops

Theforeach loop iterates over a normal list value and sets the scalar variableVARto be each element of the list in turn. If the variable is preceded with the keywordmy, then it is lexically scoped, and is therefore visible only within the loop. Otherwise, the variable is implicitly local to the loop and regains its former value upon exiting the loop. If the variable was previously declared withmy, it uses that variable instead of the global one, but it’s still localized to the loop. This implicit localization occurs only in aforeach loop. The foreach keyword is actually a synonym for the for keyword, so you can use either. If VAR is omitted,$_ is set to each value.

If any element ofLISTis an lvalue, you can modify it by modifyingVARinside the loop. Conversely, if any element of LIST is NOT an lvalue, any attempt to modify that element will fail. In other words, the foreach loop index variable is an implicit alias for each item in the list that you’re looping over.

If any part ofLISTis an array,foreach will get very confused if you add or remove elements within the loop body, for example withsplice. So don’t do that.

(13)

either.

As of Perl 5.22, there is an experimental variant of this loop that accepts a variable preceded by a backslash forVAR,in which case the items in theLISTmust be references. The backslashed variable will become an alias to each referenced item in theLIST, which must be of the correct type. The variable needn’t be a scalar in this case, and the backslash may be followed by my. To use this form, you must enable the refaliasing feature via use feature. (See feature. See also ‘‘Assigning to References’’ in perlref.)

Examples:

for (@ary) { s/foo/bar/ } for my $elem (@elements) {

$elem *= 2; }

for $count (reverse(1..10), "BOOM") { print $count, "\n";

sleep(1); }

for (1..15) { print "Merry Christmas\n"; }

foreach $item (split(/:[\\\n:]*/, $ENV{TERMCAP})) { print "Item: $item\n";

}

use feature "refaliasing";

no warnings "experimental::refaliasing";

foreach \my %hash (@array_of_hash_references) { # do something which each %hash

}

Here’s how a C programmer might code up a particular algorithm in Perl: for (my $i = 0; $i < @ary1; $i++) {

for (my $j = 0; $j < @ary2; $j++) { if ($ary1[$i] > $ary2[$j]) {

last; # can't go to outer :−( }

$ary1[$i] += $ary2[$j]; }

# this is where that last takes me }

Whereas here’s how a Perl programmer more comfortable with the idiom might do it: OUTER: for my $wid (@ary1) {

INNER: for my $jet (@ary2) {

next OUTER if $wid > $jet; $wid += $jet;

} }

See how much easier this is? It’s cleaner, safer, and faster. It’s cleaner because it’s less noisy. It’s safer because if code gets added between the inner and outer loops later on, the new code won’t be accidentally executed. Thenext explicitly iterates the other loop rather than merely terminating the inner one. And it’s faster because Perl executes aforeach statement more rapidly than it would the equivalent for loop. Perceptive Perl hackers may have noticed that afor loop has a return value, and that this value can be captured by wrapping the loop in ado block. The reward for this discovery is this cautionary advice: The return value of afor loop is unspecified and may change without notice. Do not rely on it.

(14)

Basic BLOCKs

ABLOCKby itself (labeled or not) is semantically equivalent to a loop that executes once. Thus you can use any of the loop control statements in it to leave or restart the block. (Note that this is NOTtrue in eval{}, sub{}, or contrary to popular belief do{} blocks, which do NOT count as loops.) The continue block is optional.

TheBLOCKconstruct can be used to emulate case structures. SWITCH: {

if (/ˆabc/) { $abc = 1; last SWITCH; } if (/ˆdef/) { $def = 1; last SWITCH; } if (/ˆxyz/) { $xyz = 1; last SWITCH; } $nothing = 1;

}

You’ll also find thatforeach loop used to create a topicalizer and a switch: SWITCH:

for ($var) {

if (/ˆabc/) { $abc = 1; last SWITCH; } if (/ˆdef/) { $def = 1; last SWITCH; } if (/ˆxyz/) { $xyz = 1; last SWITCH; } $nothing = 1;

}

Such constructs are quite frequently used, both because older versions of Perl had no official switch statement, and also because the new version described immediately below remains experimental and can sometimes be confusing.

Switch Statements

Starting from Perl 5.10.1 (well, 5.10.0, but it didn’t work right), you can say use feature "switch";

to enable an experimental switch feature. This is loosely based on an old version of a Perl 6 proposal, but it no longer resembles the Perl 6 construct. You also get the switch feature whenever you declare that your code prefers to run under a version of Perl that is 5.10 or later. For example:

use v5.14;

Under the ‘‘switch’’ feature, Perl gains the experimental keywordsgiven, when, default, continue, and break. Starting from Perl 5.16, one can prefix the switch keywords with CORE:: to access the feature without ause feature statement. The keywords given and when are analogous to switch andcase in other languages — though continue is not — so the code in the previous section could be rewritten as

use v5.10.1; for ($var) {

when (/ˆabc/) { $abc = 1 } when (/ˆdef/) { $def = 1 } when (/ˆxyz/) { $xyz = 1 } default { $nothing = 1 } }

Theforeach is the non-experimental way to set a topicalizer. If you wish to use the highly experimental given, that could be written like this:

use v5.10.1; given ($var) {

when (/ˆabc/) { $abc = 1 } when (/ˆdef/) { $def = 1 } when (/ˆxyz/) { $xyz = 1 } default { $nothing = 1 } }

(15)

use v5.14; for ($var) {

$abc = 1 when /ˆabc/; $def = 1 when /ˆdef/; $xyz = 1 when /ˆxyz/; default { $nothing = 1 } }

Or if you don’t care to play it safe, like this: use v5.14;

given ($var) {

$abc = 1 when /ˆabc/; $def = 1 when /ˆdef/; $xyz = 1 when /ˆxyz/; default { $nothing = 1 } }

The arguments togiven and when are in scalar context, and given assigns the $_ variable its topic value.

Exactly what theEXPRargument towhen does is hard to describe precisely, but in general, it tries to guess what you want done. Sometimes it is interpreted as $_ ˜˜ EXPR, and sometimes it is not. It also behaves differently when lexically enclosed by agiven block than it does when dynamically enclosed by aforeach loop. The rules are far too difficult to understand to be described here. See ‘‘Experimental Details on given and when’’ later on.

Due to an unfortunate bug in how given was implemented between Perl 5.10 and 5.16, under those implementations the version of$_ governed by given is merely a lexically scoped copy of the original, not a dynamically scoped alias to the original, as it would be if it were a foreach or under both the original and the current Perl 6 language specification. This bug was fixed in Perl 5.18 (and lexicalized$_ itself was removed in Perl 5.24).

If your code still needs to run on older versions, stick toforeach for your topicalizer and you will be less unhappy.

Goto

Although not for the faint of heart, Perl does support a goto statement. There are three forms: goto−LABEL, goto−EXPR, and goto−&NAME. A loop’sLABEL is not actually a valid target for a goto; it’s just the name of the loop.

Thegoto−LABEL form finds the statement labeled withLABELand resumes execution there. It may not be used to go into any construct that requires initialization, such as a subroutine or aforeach loop. It also can’t be used to go into a construct that is optimized away. It can be used to go almost anywhere else within the dynamic scope, including out of subroutines, but it’s usually better to use some other construct such aslast or die. The author of Perl has never felt the need to use this form of goto (in Perl, that is — C is another matter).

Thegoto−EXPR form expects a label name, whose scope will be resolved dynamically. This allows for computed gotos per FORTRAN, but isn’t necessarily recommended if you’re optimizing for maintainability:

goto(("FOO", "BAR", "GLARCH")[$i]);

The goto−&NAME form is highly magical, and substitutes a call to the named subroutine for the currently running subroutine. This is used by AUTOLOAD() subroutines that wish to load another subroutine and then pretend that the other subroutine had been called in the first place (except that any modifications to@_ in the current subroutine are propagated to the other subroutine.) After the goto, not ev encaller() will be able to tell that this routine was called first.

In almost all cases like this, it’s usually a far, far better idea to use the structured control flow mechanisms ofnext, last, or redo instead of resorting to a goto. For certain applications, the catch and throw pair ofeval{} and die() for exception processing can also be a prudent approach.

(16)

The Ellipsis Statement

Beginning in Perl 5.12, Perl accepts an ellipsis, "...", as a placeholder for code that you haven’t implemented yet. This form of ellipsis, the unimplemented statement, should not be confused with the binary flip-flop... operator. One is a statement and the other an operator. (Perl doesn’t usually confuse them because usually Perl can tell whether it wants an operator or a statement, but see below for exceptions.)

When Perl 5.12 or later encounters an ellipsis statement, it parses this without error, but if and when you should actually try to execute it, Perl throws an exception with the textUnimplemented:

use v5.12;

sub unimplemented { ... } eval { unimplemented() };

if ($@ =˜ /ˆUnimplemented at /) { say "I found an ellipsis!"; }

You can only use the elliptical statement to stand in for a complete statement. These examples of how the ellipsis works: use v5.12; { ... } sub foo { ... } ...; eval { ... }; sub somemeth { my $self = shift; ...; } $x = do { my $n; ...; say "Hurrah!"; $n; };

The elliptical statement cannot stand in for an expression that is part of a larger statement, since the... is also the three-dot version of the flip-flop operator (see ‘‘Range Operators’’ in perlop).

These examples of attempts to use an ellipsis are syntax errors: use v5.12;

print ...;

open(my $fh, ">", "/dev/passwd") or ...; if ($condition && ... ) { say "Howdy" };

There are some cases where Perl can’t immediately tell the difference between an expression and a statement. For instance, the syntax for a block and an anonymous hash reference constructor look the same unless there’s something in the braces to give Perl a hint. The ellipsis is a syntax error if Perl doesn’t guess that the{ ... } is a block. In that case, it doesn’t think the ... is an ellipsis because it’s expecting an expression instead of a statement:

@transformed = map { ... } @input; # syntax error

Inside your block, you can use a; before the ellipsis to denote that the { ... } is a block and not a hash reference constructor. Now the ellipsis works:

@transformed = map {; ... } @input; # ';' disambiguates

Note: Some folks colloquially refer to this bit of punctuation as a ‘‘yada-yada’’ or ‘‘triple-dot’’, but its true name is actually an ellipsis.

PODs: Embedded Documentation

Perl has a mechanism for intermixing documentation with source code. While it’s expecting the beginning of a new statement, if the compiler encounters a line that begins with an equal sign and a word, like this

(17)

=head1 Here There Be Pods!

Then that text and all remaining text up through and including a line beginning with=cut will be ignored. The format of the intervening text is described in perlpod.

This allows you to intermix your source code and your documentation text freely, as in =item snazzle($)

The snazzle() function will behave in the most spectacular form that you can possibly imagine, not even excepting cybernetic pyrotechnics.

=cut back to the compiler, nuff of this pod stuff! sub snazzle($) {

my $thingie = shift; ...

}

Note that pod translators should look at only paragraphs beginning with a pod directive (it makes parsing easier), whereas the compiler actually knows to look for pod escapes even in the middle of a paragraph. This means that the following secret stuff will be ignored by both the compiler and the translators.

$a=3;

=secret stuff

warn "Neither POD nor CODE!?" =cut back

print "got $a\n";

You probably shouldn’t rely upon thewarn() being podded out forever. Not all pod translators are well-behaved in this regard, and perhaps the compiler will become pickier.

One may also use pod directives to quickly comment out a section of code. Plain Old Comments (Not!)

Perl can process line directives, much like the C preprocessor. Using this, one can control Perl’s idea of filenames and line numbers in error or warning messages (especially for strings that are processed with eval()). The syntax for this mechanism is almost the same as for most C preprocessors: it matches the regular expression

# example: '# line 42 "new_filename.plx"' /ˆ\# \s*

line \s+ (\d+) \s* (?:\s("?)([ˆ"]+)\g2)? \s* $/x

with $1 being the line number for the next line, and $3 being the optional filename (specified with or without quotes). Note that no whitespace may precede the#, unlike modern C preprocessors.

There is a fairly obvious gotcha included with the line directive: Debuggers and profilers will only show the last source line to appear at a particular line number in a given file. Care should be taken not to cause line number collisions in code you’d like to debug later.

Here are some examples that you should be able to type into your command shell: % perl

# line 200 "bzzzt"

# the '#' on the previous line must be the first char on line die 'foo';

_ _END_ _

foo at bzzzt line 201. % perl

# line 200 "bzzzt"

(18)

_ _END_ _

foo at − line 2001. % perl

eval qq[\n#line 200 "foo bar"\ndie 'foo']; print $@; _ _END_ _

foo at foo bar line 200. % perl

# line 345 "goop"

eval "\n#line " . _ _LINE_ _ . ' "' . _ _FILE_ _ ."\"\ndie 'foo'"; print $@;

_ _END_ _

foo at goop line 345. Experimental Details on given and when

As previously mentioned, the ‘‘switch’’ feature is considered highly experimental; it is subject to change with little notice. In particular, when has tricky behaviours that are expected to change to become less tricky in the future. Do not rely upon its current (mis)implementation. Before Perl 5.18,given also had tricky behaviours that you should still beware of if your code must run on older versions of Perl.

Here is a longer example ofgiven: use feature ":5.10"; given ($foo) {

when (undef) {

say '$foo is undefined'; }

when ("foo") {

say '$foo is the string "foo"'; }

when ([1,3,5,7,9]) {

say '$foo is an odd digit'; continue; # Fall through }

when ($_ < 100) {

say '$foo is numerically less than 100'; }

when (\&complicated_check) {

say 'a complicated check for $foo is true'; }

default {

die q(I don't know what to do with $foo); }

}

Before Perl 5.18,given(EXPR) assigned the value ofEXPRto merely a lexically scoped copy (!) of$_, not a dynamically scoped alias the wayforeach does. That made it similar to

do { my $_ = EXPR; ... }

except that the block was automatically broken out of by a successful when or an explicit break. Because it was only a copy, and because it was only lexically scoped, not dynamically scoped, you could not do the things with it that you are used to in aforeach loop. In particular, it did not work for arbitrary function calls if those functions might try to access$_. Best stick to foreach for that.

Most of the power comes from the implicit smartmatching that can sometimes apply. Most of the time, when(EXPR) is treated as an implicit smartmatch of $_, that is, $_ ˜˜ EXPR. (See ‘‘Smartmatch Operator’’ in perlop for more information on smartmatching.) But whenEXPRis one of the 10 exceptional cases (or things like them) listed below, it is used directly as a boolean.

(19)

2. A regular expression match in the form of/REGEX/, $foo =˜ /REGEX/, or $foo =˜ EXPR. Also, a negated regular expression match in the form !/REGEX/, $foo !˜ /REGEX/, or $foo !˜ EXPR.

3. A smart match that uses an explicit˜˜ operator, such as EXPR ˜˜ EXPR.

NOTE:You will often have to use$c ˜˜ $_ because the default case uses $_ ˜˜ $c , which is frequentlythe opposite of what you want.

4. A boolean comparison operator such as$_ < 10 or $x eq "abc". The relational operators that this applies to are the six numeric comparisons (<, >, <=, >=, ==, and !=), and the six string comparisons (lt, gt, le, ge, eq, and ne).

5. At least the three builtin functionsdefined(...), exists(...), and eof(...). We might someday add more of these later if we think of them.

6. A neg ated expression, whether!(EXPR) or not(EXPR), or a logical exclusive-or, (EXPR1) xor (EXPR2). The bitwise versions (˜ and ˆ) are not included.

7. A filetest operator, with exactly 4 exceptions:−s, −M, −A, and −C, as these return numerical values, not boolean ones. The−z filetest operator is not included in the exception list.

8. The.. and ... flip-flop operators. Note that the ... flip-flop operator is completely different from the... elliptical statement just described.

In those 8 cases above, the value ofEXPRis used directly as a boolean, so no smartmatching is done. You may think ofwhen as a smartsmartmatch.

Furthermore, Perl inspects the operands of logical operators to decide whether to use smartmatching for each one by applying the above test to the operands:

9. If EXPR is EXPR1 && EXPR2 or EXPR1 and EXPR2, the test is applied recursively to both EXPR1andEXPR2. Only if both operands also pass the test, recursively, will the expression be treated as boolean. Otherwise, smartmatching is used.

10. If EXPRis EXPR1 || EXPR2, EXPR1 // EXPR2, or EXPR1 or EXPR2, the test is applied recursively toEXPR1only (which might itself be a higher-precedenceANDoperator, for example, and thus subject to the previous rule), not toEXPR2. IfEXPR1is to use smartmatching, thenEXPR2also does so, no matter whatEXPR2 contains. But ifEXPR2 does not get to use smartmatching, then the second argument will not be either. This is quite different from the && case just described, so be careful.

These rules are complicated, but the goal is for them to do what you want (even if you don’t quite understand why they are doing it). For example:

when (/ˆ\d+$/ && $_ < 75) { ... }

will be treated as a boolean match because the rules say both a regex match and an explicit test on$_ will be treated as boolean.

Also:

when ([qw(foo bar)] && /baz/) { ... }

will use smartmatching because only one of the operands is a boolean: the other uses smartmatching, and that wins.

Further:

when ([qw(foo bar)] || /ˆbaz/) { ... } will use smart matching (only the first operand is considered), whereas

when (/ˆbaz/ || [qw(foo bar)]) { ... }

will test only the regex, which causes both operands to be treated as boolean. Watch out for this one, then, because an arrayref is always a true value, which makes it effectively redundant. Not a good idea.

Tautologous boolean operators are still going to be optimized away. Don’t be tempted to write when ("foo" or "bar") { ... }

This will optimize down to"foo", so "bar" will never be considered (even though the rules say to use a smartmatch on "foo"). For an alternation like this, an array ref will work, because this will instigate

(20)

smartmatching:

when ([qw(foo bar)] { ... }

This is somewhat equivalent to the C−style switch statement’s fallthrough functionality (not to be confused with Perl’s fallthrough functionality — see below), wherein the same block is used for several case statements.

Another useful shortcut is that, if you use a literal array or hash as the argument togiven, it is turned into a reference. Sogiven(@foo) is the same as given(\@foo), for example.

default behaves exactly like when(1 == 1), which is to say that it always matches. Breaking out

You can use the break keyword to break out of the enclosing given block. Every when block is implicitly ended with abreak.

Fall-through

You can use the continue keyword to fall through from one case to the next immediate when or default:

given($foo) {

when (/x/) { say '$foo contains an x'; continue }

when (/y/) { say '$foo contains a y' }

default { say '$foo does not contain a y' } }

Return value

When agiven statement is also a valid expression (for example, when it’s the last statement of a block), it evaluates to:

• An empty list as soon as an explicitbreak is encountered.

• The value of the last evaluated expression of the successfulwhen/default clause, if there happens to be one.

• The value of the last evaluated expression of thegiven block if no condition is true.

In both last cases, the last expression is evaluated in the context that was applied to thegiven block. Note that, unlikeif and unless, failed when statements always evaluate to an empty list.

my $price = do { given ($item) {

when (["pear", "apple"]) { 1 }

break when "vote"; # My vote cannot be bought 1e10 when /Mona Lisa/;

"unknown"; }

};

Currently,given blocks can’t always be used as proper expressions. This may be addressed in a future version of Perl.

Switching in a loop

Instead of usinggiven(), you can use a foreach() loop. For example, here’s one way to count how many times a particular string occurs in an array:

use v5.10.1; my $count = 0; for (@array) {

when ("foo") { ++$count } }

print "\@array contains $count copies of 'foo'\n"; Or in a more recent version:

(21)

use v5.14; my $count = 0; for (@array) {

++$count when "foo"; }

print "\@array contains $count copies of 'foo'\n";

At the end of allwhen blocks, there is an implicit next. You can override that with an explicit last if you’re interested in only the first match alone.

This doesn’t work if you explicitly specify a loop variable, as infor $item (@array). You have to use the default variable$_.

Differences from Perl 6

The Perl 5 smartmatch andgiven/when constructs are not compatible with their Perl 6 analogues. The most visible difference and least important difference is that, in Perl 5, parentheses are required around the argument to given() and when() (except when this last one is used as a statement modifier). Parentheses in Perl 6 are always optional in a control construct such asif(), while(), or when(); they can’t be made optional in Perl 5 without a great deal of potential confusion, because Perl 5 would parse the expression

given $foo { ... }

as though the argument to given were an element of the hash %foo, interpreting the braces as hash-element syntax.

However, their are many, many other differences. For example, this works in Perl 5: use v5.12;

my @primary = ("red", "blue", "green"); if (@primary ˜˜ "red") {

say "primary smartmatches red"; }

if ("red" ˜˜ @primary) {

say "red smartmatches primary"; }

say "that's all, folks!";

But it doesn’t work at all in Perl 6. Instead, you should use the (parallelizable)any operator: if any(@primary) eq "red" {

say "primary smartmatches red"; }

if "red" eq any(@primary) {

say "red smartmatches primary"; }

The table of smartmatches in ‘‘Smartmatch Operator’’ in perlop is not identical to that proposed by the Perl 6 specification, mainly due to differences between Perl 6’s and Perl 5’s data models, but also because the Perl 6 spec has changed since Perl 5 rushed into early adoption.

In Perl 6,when() will always do an implicit smartmatch with its argument, while in Perl 5 it is convenient (albeit potentially confusing) to suppress this implicit smartmatch in various rather loosely-defined situations, as roughly outlined above. (The difference is largely because Perl 5 does not have, even internally, a boolean type.)

(22)

NAME

perldata − Perl data types DESCRIPTION

Variable names

Perl has three built-in data types: scalars, arrays of scalars, and associative arrays of scalars, known as ‘‘hashes’’. A scalar is a single string (of any size, limited only by the available memory), number, or a reference to something (which will be discussed in perlref). Normal arrays are ordered lists of scalars indexed by number, starting with 0. Hashes are unordered collections of scalar values indexed by their associated string key.

Values are usually referred to by name, or through a named reference. The first character of the name tells you to what sort of data structure it refers. The rest of the name tells you the particular value to which it refers. Usually this name is a single identifier, that is, a string beginning with a letter or underscore, and containing letters, underscores, and digits. In some cases, it may be a chain of identifiers, separated by:: (or by the slightly archaic'); all but the last are interpreted as names of packages, to locate the namespace in which to look up the final identifier (see ‘‘Packages’’ in perlmod for details). For a more in-depth discussion on identifiers, see ‘‘Identifier parsing’’. It’s possible to substitute for a simple identifier, an expression that produces a reference to the value at runtime. This is described in more detail below and in perlref.

Perl also has its own built-in variables whose names don’t follow these rules. They hav e strange names so they don’t accidentally collide with one of your normal variables. Strings that match parenthesized parts of a regular expression are saved under names containing only digits after the$ (see perlop and perlre). In addition, several special variables that provide windows into the inner working of Perl have names containing punctuation characters. These are documented in perlvar.

Scalar values are always named with ’$’, even when referring to a scalar that is part of an array or a hash. The ’$’ symbol works semantically like the English word ‘‘the’’ in that it indicates a single value is expected.

$days # the simple scalar value "days" $days[28] # the 29th element of array @days $days{'Feb'} # the 'Feb' value from hash %days $#days # the last index of array @days

Entire arrays (and slices of arrays and hashes) are denoted by ’@’, which works much as the word ‘‘these’’ or ‘‘those’’ does in English, in that it indicates multiple values are expected.

@days # ($days[0], $days[1],... $days[n]) @days[3,4,5] # same as ($days[3],$days[4],$days[5]) @days{'a','c'} # same as ($days{'a'},$days{'c'}) Entire hashes are denoted by ’%’:

%days # (key1, val1, key2, val2 ...)

In addition, subroutines are named with an initial ’&’, though this is optional when unambiguous, just as the word ‘‘do’’ is often redundant in English. Symbol table entries can be named with an initial ’*’, but you don’t really care about that yet (if ever :−).

Every variable type has its own namespace, as do several non-variable identifiers. This means that you can, without fear of conflict, use the same name for a scalar variable, an array, or a hash — or, for that matter, for a filehandle, a directory handle, a subroutine name, a format name, or a label. This means that$foo and @foo are two different variables. It also means that $foo[1] is a part of @foo, not a part of $foo. This may seem a bit weird, but that’s okay, because it is weird.

Because variable references always start with ’$’, ’@’, or ’%’, the ‘‘reserved’’ words aren’t in fact reserved with respect to variable names. They are reserved with respect to labels and filehandles, however, which don’t hav e an initial special character. You can’t hav e a filehandle named ‘‘log’’, for instance. Hint: you could say open(LOG,'logfile') rather than open(log,'logfile'). Using uppercase filehandles also improves readability and protects you from conflict with future reserved words. Case is significant−−‘‘FOO’’, ‘‘Foo’’, and ‘‘foo’’ are all different names. Names that start with a letter or underscore may also contain digits and underscores.

(23)

appropriate type. For a description of this, see perlref.

Names that start with a digit may contain only more digits. Names that do not start with a letter, underscore, digit or a caret are limited to one character, e.g., $% or $$. (Most of these one character names have a predefined significance to Perl. For instance, $$ is the current process id. And all such names are reserved for Perl’s possible use.)

Identifier parsing

Up until Perl 5.18, the actual rules of what a valid identifier was were a bit fuzzy. Howev er, in general, anything defined here should work on previous versions of Perl, while the opposite — edge cases that work in previous versions, but aren’t defined here — probably won’t work on newer versions. As an important side note, please note that the following only applies to bareword identifiers as found in Perl source code, not identifiers introduced through symbolic references, which have much fewer restrictions. If working under the effect of theuse utf8; pragma, the following rules apply:

/ (?[ ( \p{Word} & \p{XID_Start} ) + [_] ]) (?[ ( \p{Word} & \p{XID_Continue} ) ]) * /x

That is, a ‘‘start’’ character followed by any number of ‘‘continue’’ characters. Perl requires every character in an identifier to also match \w (this prevents some problematic cases); and Perl additionally accepts identfier names beginning with an underscore.

If not underuse utf8, the source is treated asASCII + 128extra generic characters, and identifiers should match

/ (?aa) (?!\d) \w+ /x

That is, any word character in theASCIIrange, as long as the first character is not a digit.

There are two package separators in Perl: A double colon (::) and a single quote ('). Normal identifiers can start or end with a double colon, and can contain several parts delimited by double colons. Single quotes have similar rules, but with the exception that they are not legal at the end of an identifier: That is, $'foo and $foo'bar are legal, but $foo'bar' is not.

Additionally, if the identifier is preceded by a sigil — that is, if the identifier is part of a variable name — it may optionally be enclosed in braces.

While you can mix double colons with singles quotes, the quotes must come after the colons:$::::'foo and$foo::'bar are legal, but $::'::foo and $foo'::bar are not.

Put together, a grammar to match a basic identifier becomes / (?(DEFINE) (?<variable> (?&sigil) (?: (?&normal_identifier) | \{ \s* (?&normal_identifier) \s* \} ) ) (?<normal_identifier> (?: :: )* '? (?&basic_identifier) (?: (?= (?: :: )+ '? | (?: :: )* ' ) (?&normal_identifier) )? (?: :: )* ) (?<basic_identifier> # is use utf8 on?

(?(?{ (caller(0))[8] & $utf8::hint_bits }) (?&Perl_XIDS) (?&Perl_XIDC)*

| (?aa) (?!\d) \w+ )

)

(?<sigil> [&*\$\@\%])

(24)

(?<Perl_XIDC> (?[ \p{Word} & \p{XID_Continue} ]) ) )

/x

Meanwhile, special identifiers don’t follow the above rules; For the most part, all of the identifiers in this category have a special meaning given by Perl. Because they hav e special parsing rules, these generally can’t be fully-qualified. They come in six forms (but don’t use forms 5 and 6):

1. A sigil, followed solely by digits matching\p{POSIX_Digit}, like $0, $1, or $10000.

2. A sigil followed by a single character matching the \p{POSIX_Punct} property, like $! or %+, except the character"{" doesn’t work.

3. A sigil, followed by a caret and any one of the characters[][A−Zˆ_?\], like $ˆV or $ˆ].

4. Similar to the above, a sigil, followed by bareword text in braces, where the first character is a caret. The next character is any one of the characters[][A−Zˆ_?\], followed by ASCIIword characters. An example is${ˆGLOBAL_PHASE}.

5. A sigil, followed by any single character in the range[\xA1−\xAC\xAE−\xFF] when not under "use utf8". (Under "use utf8", the normal identifier rules given earlier in this section apply.) Use of non-graphic characters (the C1 controls, the NO-BREAKSPACE,and theSOFT HYPHEN) has been disallowed since v5.26.0. The use of the other characters is unwise, as these are all reserved to have special meaning to Perl, and none of them currently do have special meaning, though this could change without notice.

Note that an implication of this form is that there are identifiers only legal under"use utf8", and vice-versa, for example the identifier $état is legal under "use utf8", but is otherwise considered to be the single character variable$é followed by the bareword "tat", the combination of which is a syntax error.

6. This is a combination of the previous two forms. It is valid only when not under "use utf8" (normal identifier rules apply when under"use utf8"). The form is a sigil, followed by text in braces, where the first character is any one of the characters in the range[\x80−\xFF] followed by ASCIIword characters up to the trailing brace.

The same caveats as the previous form apply: The non-graphic characters are no longer allowed with ‘‘use utf8’’, it is unwise to use this form at all, and utf8ness makes a big difference.

Prior to Perl v5.24, non-graphicalASCII control characters were also allowed in some situations; this had been deprecated since v5.20.

Context

The interpretation of operations and values in Perl sometimes depends on the requirements of the context around the operation or value. There are two major contexts: list and scalar. Certain operations return list values in contexts wanting a list, and scalar values otherwise. If this is true of an operation it will be mentioned in the documentation for that operation. In other words, Perl overloads certain operations based on whether the expected return value is singular or plural. Some words in English work this way, like ‘‘fish’’ and ‘‘sheep’’.

In a reciprocal fashion, an operation provides either a scalar or a list context to each of its arguments. For example, if you say

int( <STDIN> )

the integer operation provides scalar context for the <> operator, which responds by reading one line from STDINand passing it back to the integer operation, which will then find the integer value of that line and return that. If, on the other hand, you say

sort( <STDIN> )

then the sort operation provides list context for <>, which will proceed to read every line available up to the end of file, and pass that list of lines back to the sort routine, which will then sort those lines and return them as a list to whatever the context of the sort was.

Assignment is a little bit special in that it uses its left argument to determine the context for the right argument. Assignment to a scalar evaluates the right-hand side in scalar context, while assignment to an array or hash evaluates the righthand side in list context. Assignment to a list (or slice, which is just a list anyway) also evaluates the right-hand side in list context.

(25)

When you use theuse warnings pragma or Perl’s −w command-line option, you may see warnings about useless uses of constants or functions in ‘‘void context’’. Void context just means the value has been discarded, such as a statement containing only "fred"; or getpwuid(0);. It still counts as scalar context for functions that care whether or not they’re being called in list context.

User-defined subroutines may choose to care whether they are being called in a void, scalar, or list context. Most subroutines do not need to bother, though. That’s because both scalars and lists are automatically interpolated into lists. See ‘‘wantarray’’ in perlfunc for how you would dynamically discern your function’s calling context.

Scalar values

All data in Perl is a scalar, an array of scalars, or a hash of scalars. A scalar may contain one single value in any of three different flavors: a number, a string, or a reference. In general, conversion from one form to another is transparent. Although a scalar may not directly hold multiple values, it may contain a reference to an array or hash which in turn contains multiple values.

Scalars aren’t necessarily one thing or another. There’s no place to declare a scalar variable to be of type ‘‘string’’, type ‘‘number’’, type ‘‘reference’’, or anything else. Because of the automatic conversion of scalars, operations that return scalars don’t need to care (and in fact, cannot care) whether their caller is looking for a string, a number, or a reference. Perl is a contextually polymorphic language whose scalars can be strings, numbers, or references (which includes objects). Although strings and numbers are considered pretty much the same thing for nearly all purposes, references are strongly-typed, uncastable pointers with builtin reference-counting and destructor invocation.

A scalar value is interpreted asFALSEin the Boolean sense if it is undefined, the null string or the number 0 (or its string equivalent, ‘‘0’’), andTRUEif it is anything else. The Boolean context is just a special kind of scalar context where no conversion to a string or a number is ever performed.

There are actually two varieties of null strings (sometimes referred to as ‘‘empty’’ strings), a defined one and an undefined one. The defined version is just a string of length zero, such as "". The undefined version is the value that indicates that there is no real value for something, such as when there was an error, or at end of file, or when you refer to an uninitialized variable or element of an array or hash. Although in early versions of Perl, an undefined scalar could become defined when first used in a place expecting a defined value, this no longer happens except for rare cases of autovivification as explained in perlref. You can use the defined() operator to determine whether a scalar value is defined (this has no meaning on arrays or hashes), and the undef() operator to produce an undefined value.

To find out whether a given string is a valid non-zero number, it’s sometimes enough to test it against both numeric 0 and also lexical ‘‘0’’ (although this will cause noises if warnings are on). That’s because strings that aren’t numbers count as 0, just as they do in awk:

if ($str == 0 && $str ne "0") {

warn "That doesn't look like a number"; }

That method may be best because otherwise you won’t treat IEEE notations like NaN or Infinity properly. At other times, you might prefer to determine whether string data can be used numerically by calling the POSIX::strtod() function or by inspecting your string with a regular expression (as documented in perlre).

warn "has nondigits" if /\D/;

warn "not a natural number" unless /ˆ\d+$/; # rejects −3 warn "not an integer" unless /ˆ−?\d+$/; # rejects +3 warn "not an integer" unless /ˆ[+−]?\d+$/;

warn "not a decimal number" unless /ˆ−?\d+\.?\d*$/; # rejects .2 warn "not a decimal number" unless /ˆ−?(?:\d+(?:\.\d*)?|\.\d+)$/; warn "not a C float"

unless /ˆ([+−]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+−]?\d+))?$/;

The length of an array is a scalar value. You may find the length of array@days by evaluating $#days, as in csh. Howev er, this isn’t the length of the array; it’s the subscript of the last element, which is a different value since there is ordinarily a 0th element. Assigning to$#days actually changes the length of the array. Shortening an array this way destroys intervening values. Lengthening an array that was previously shortened does not recover values that were in those elements.

References

Outline

Related documents

Although the DSM-5 only describes symptoms of gambling disorder, the Appendix lists additional forms of behavioral addiction that should be explored (e.g., excessive use of social

25 and Table 22 seem to indicate that the best concentrations for both biomass production and PHAs accumulation process using slaughterhouse fatty waste were 1 g/L of

[ Library Home | Perl in a Nutshell | Learning Perl | Learning Perl on Win32 | Programming Perl | Advanced Perl Programming | Perl Cookbook ]... Previous: What's in This Book

The nice thing about using anonymous hashes and arrays is that you don't need to declare to perl that you're going to put an anonymous hash into another hash value, you just start

In our experiments, HyScaleII has lower packet loss ratio and higher average aggregate throughput by an average of 50% and 13.8% respectively as compared to HyScale [17]..

[ Library Home | Perl in a Nutshell | Learning Perl | Learning Perl on Win32 | Programming Perl | Advanced Perl Programming | Perl Cookbook ]... Previous: What This Book

– Member of the Canadian Investor Protection Fund, TD Waterhouse Private Investment Counsel Inc., TD Waterhouse Private Banking (offered by The Toronto-Dominion Bank) and

Svrha opisivanja položaja Kurda i njihovih političkih stranaka u Iraku, Iranu, Siriji i Turskoj je pokazati da je tradicionalna podjela na klanove zamijenjena političkim