Data Types and Objects Perl has three data types: scalars, arrays of scalars, and associative arrays of scalars. Normal arrays are indexed by number, and associative arrays by string. The interpretation of operations and values in perl some- times depends on the requirements of the context around the operation or value. There are three major contexts: string, numeric and array. Certain operations return array values in contexts wanting an array, and scalar val- ues otherwise. (If this is true of an operation it will be mentioned in the documentation for that operation.) Operations which return scalars don't care whether the context is looking for a string or a number, but scalar variables and values are interpreted as strings or numbers as appropriate to the context. A scalar is interpreted as TRUE in the boolean sense if it is not the null string or 0. Booleans returned by operators are 1 for true and 0 or '' (the null string) for false. There are actually two varieties of null string: defined and undefined. Undefined null strings are returned when 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. An unde- fined null string may become defined the first time you access it, but prior to that you can use the defined() operator to determine whether the value is defined or not. References to scalar variables always begin with '$', even when referring to a scalar that is part of an array. Thus: $days # a simple scalar variable $days[28] # 29th element of array @days $days{'Feb'} # one value from an associative array $#days # last index of array @days but entire arrays or array slices are denoted by '@': @days # ($days[0], $days[1],... $days[n]) @days[3,4,5] # same as @days[3..5] @days{'a','c'} # same as ($days{'a'},$days{'c'}) and entire associative arrays are denoted by '%': %days # (key1, val1, key2, val2 ...) Any of these eight constructs may serve as an lvalue, that is, may be assigned to. (It also turns out that an assignment is itself an lvalue in certain contexts--see examples under s, tr and chop.) Assignment to a scalar evaluates the righthand side in a scalar context, while assignment to an array or array slice evaluates the right- hand side in an array context. You may find the length of array @days by evaluating "$#days", as in csh. (Actually, it's not the length of the array, it's the subscript of the last element, since there is (ordinarily) a 0th element.) Assigning to $#days changes the length of the array. Shortening an array by this method does not actually destroy any values. Length- ening an array that was previously shortened recovers the values that were in those elements. You can also gain some measure of efficiency by preextending an array that is going to get big. (You can also extend an array by assigning to an element that is off the end of the array. This differs from assigning to $#whatever in that inter- vening values are set to null rather than recovered.) You can truncate an array down to nothing by assigning the null list () to it. The following are exactly equivalent @whatever = (); $#whatever = $[ - 1; If you evaluate an array in a scalar context, it returns the length of the array. The following is always true: scalar(@whatever) == $#whatever - $[ + 1; If you evaluate an associative array in a scalar context, it returns a value which is true if and only if the array contains any elements. (If there are any elements, the value returned is a string consisting of the number of used buckets and the number of allocated buckets, sepa- rated by a slash.) Multi-dimensional arrays are not directly supported, but see the discussion of the $; variable later for a means of emulating multiple subscripts with an associative array. You could also write a subroutine to turn multiple sub- scripts into a single subscript. Every data type has its own namespace. You can, without fear of conflict, use the same name for a scalar variable, an array, an associative array, a filehandle, a subroutine name, and/or a label. Since variable and array 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 have an initial special character. Case IS significant--"FOO", "Foo" and "foo" are all different names. Names which start with a letter may also contain digits and underscores. Names which do not start with a letter are limited to one char- acter, e.g. "$%" or "$$". (Most of the one character names have a predefined significance to perl. More later.) Numeric literals are specified in any of the usual float- ing point or integer formats: 12345 12345.67 .23E-10 0xffff # hex 0377 # octal 4_294_967_296 String literals are delimited by either single or double quotes. They work much like shell quotes: double-quoted string literals are subject to backslash and variable sub- stitution; single-quoted strings are not (except for \' and \\). The usual backslash rules apply for making char- acters such as newline, tab, etc., as well as some more exotic forms: \t tab \n newline \r return \f form feed \b backspace \a alarm (bell) \e escape \033 octal char \x1b hex char \c[ control char \l lowercase next char \u uppercase next char \L lowercase till \E \U uppercase till \E \E end case modification You can also embed newlines directly in your strings, i.e. they can end on a different line than they begin. This is nice, but if you forget your trailing quote, the error will not be reported until perl finds another line con- taining the quote character, which may be much further on in the script. Variable substitution inside strings is limited to scalar variables, normal array values, and array slices. (In other words, identifiers beginning with $ or @, followed by an optional bracketed expression as a subscript.) The following code segment prints out "The price is $100." $Price = '$100'; # not interpreted print "The price is $Price.\n";# interpreted Note that you can put curly brackets around the identifier to delimit it from following alphanumerics. Also note that a single quoted string must be separated from a pre- ceding word by a space, since single quote is a valid character in an identifier (see Packages). A word that doesn't have any other interpretation in the grammar will be treated as if it had single quotes around it. For this purpose, a word consists only of alphanu- meric characters and underline, and must start with an alphabetic character. As with filehandles and labels, a bare word that consists entirely of lowercase letters risks conflict with future reserved words, and if you use the -w switch, Perl will warn you about any such words. Array values are interpolated into double-quoted strings by joining all the elements of the array with the delim- iter specified in the $" variable, space by default. (Since in versions of perl prior to 3.0 the @ character was not a metacharacter in double-quoted strings, the interpolation of @array, $array[EXPR], @array[LIST], $array{EXPR}, or @array{LIST} only happens if array is referenced elsewhere in the program or is predefined.) The following are equivalent: $temp = join($",@ARGV); system "echo $temp"; system "echo @ARGV"; Within search patterns (which also undergo double-quotish substitution) there is a bad ambiguity: Is /$foo[bar]/ to be interpreted as /${foo}[bar]/ (where [bar] is a charac- ter class for the regular expression) or as /${foo[bar]}/ (where [bar] is the subscript to array @foo)? If @foo doesn't otherwise exist, then it's obviously a character class. If @foo exists, perl takes a good guess about [bar], and is almost always right. If it does guess wrong, or if you're just plain paranoid, you can force the correct interpretation with curly brackets as above. A line-oriented form of quoting is based on the shell here-is syntax. Following a << you specify a string to terminate the quoted material, and all lines following the current line down to the terminating string are the value of the item. The terminating string may be either an identifier (a word), or some quoted text. If quoted, the type of quotes you use determines the treatment of the text, just as in regular quoting. An unquoted identifier works like double quotes. There must be no space between the << and the identifier. (If you put a space it will be treated as a null identifier, which is valid, and matches the first blank line--see Merry Christmas example below.) The terminating string must appear by itself (unquoted and with no surrounding whitespace) on the terminating line. print <<EOF; # same as above The price is $Price. EOF print <<"EOF"; # same as above The price is $Price. EOF print << x 10; # null identifier is delimiter Merry Christmas! print <<`EOC`; # execute commands echo hi there echo lo there EOC print <<foo, <<bar; # you can stack them I said foo. foo I said bar. bar Array literals are denoted by separating individual values by commas, and enclosing the list in parentheses: (LIST) In a context not requiring an array value, the value of the array literal is the value of the final element, as in the C comma operator. For example, @foo = ('cc', '-E', $bar); assigns the entire array value to array foo, but $foo = ('cc', '-E', $bar); assigns the value of variable bar to variable foo. Note that the value of an actual array in a scalar context is the length of the array; the following assigns to $foo the value 3: @foo = ('cc', '-E', $bar); $foo = @foo; # $foo gets 3 You may have an optional comma before the closing paren- thesis of an array literal, so that you can say: @foo = ( 1, 2, 3, ); When a LIST is evaluated, each element of the list is evaluated in an array context, and the resulting array value is interpolated into LIST just as if each individual element were a member of LIST. Thus arrays lose their identity in a LIST--the list (@foo,@bar,&SomeSub) contains all the elements of @foo followed by all the ele- ments of @bar, followed by all the elements returned by the subroutine named SomeSub. A list value may also be subscripted like a normal array. Examples: $time = (stat($file))[8]; # stat returns array value $digit = ('a','b','c','d','e','f')[$digit-10]; return (pop(@foo),pop(@foo))[0]; Array lists may be assigned to if and only if each element of the list is an lvalue: ($a, $b, $c) = (1, 2, 3); ($map{'red'}, $map{'blue'}, $map{'green'}) = (0x00f, 0x0f0, 0xf00); The final element may be an array or an associative array: ($a, $b, @rest) = split; local($a, $b, %rest) = @_; You can actually put an array anywhere in the list, but the first array in the list will soak up all the values, and anything after it will get a null value. This may be useful in a local(). An associative array literal contains pairs of values to be interpreted as a key and a value: # same as map assignment above %map = ('red',0x00f,'blue',0x0f0,'green',0xf00); Array assignment in a scalar context returns the number of elements produced by the expression on the right side of the assignment: $x = (($foo,$bar) = (3,2,1)); # set $x to 3, not 2 Expressions Since perl expressions work almost exactly like C expres- sions, only the differences will be mentioned here. Here's what perl has that C doesn't: ** The exponentiation operator. **= The exponentiation assignment operator. () The null list, used to initialize an array to null. . Concatenation of two strings. .= The concatenation assignment operator. eq String equality (== is numeric equality). For a mnemonic just think of "eq" as a string. (If you are used to the awk behavior of using == for either string or numeric equality based on the current form of the comparands, beware! You must be explicit here.) ne String inequality (!= is numeric inequality). lt String less than. gt String greater than. le String less than or equal. ge String greater than or equal. cmp String comparison, returning -1, 0, or 1. <=> Numeric comparison, returning -1, 0, or 1. =~ Certain operations search or modify the string "$_" by default. This operator makes that kind of operation work on some other string. The right argument is a search pattern, substitution, or translation. The left argument is what is sup- posed to be searched, substituted, or translated instead of the default "$_". The return value indicates the success of the operation. (If the right argument is an expression other than a search pattern, substitution, or translation, it is interpreted as a search pattern at run time. This is less efficient than an explicit search, since the pattern must be compiled every time the expression is evaluated.) The precedence of this operator is lower than unary minus and autoincre- ment/decrement, but higher than everything else. !~ Just like =~ except the return value is negated. x The repetition operator. Returns a string con- sisting of the left operand repeated the number of times specified by the right operand. In an array context, if the left operand is a list in parens, it repeats the list. print '-' x 80; # print row of dashes print '-' x80; # illegal, x80 is identifier print "\t" x ($tab/8), ' ' x ($tab%8); # tab over @ones = (1) x 80; # an array of 80 1's @ones = (5) x @ones; # set all elements to 5 x= The repetition assignment operator. Only works on scalars. .. The range operator, which is really two different operators depending on the context. In an array context, returns an array of values counting (by ones) from the left value to the right value. This is useful for writing "for (1..10)" loops and for doing slice operations on arrays. In a scalar context, .. returns a boolean value. The operator is bistable, like a flip-flop, and emulates the line-range (comma) operator of sed, awk, and various editors. Each .. operator main- tains its own boolean state. It is false as long as its left operand is false. Once the left operand is true, the range operator stays true until the right operand is true, AFTER which the range operator becomes false again. (It doesn't become false till the next time the range operator is evaluated. It can test the right operand and become false on the same evaluation it became true (as in awk), but it still returns true once. If you don't want it to test the right operand till the next evaluation (as in sed), use three dots (...) instead of two.) The right operand is not evaluated while the operator is in the "false" state, and the left operand is not evaluated while the operator is in the "true" state. The prece- dence is a little lower than || and &&. The value returned is either the null string for false, or a sequence number (beginning with 1) for true. The sequence number is reset for each range encoun- tered. The final sequence number in a range has the string 'E0' appended to it, which doesn't affect its numeric value, but gives you something to search for if you want to exclude the endpoint. You can exclude the beginning point by waiting for the sequence number to be greater than 1. If either operand of scalar .. is static, that operand is implicitly compared to the $. variable, the current line number. Examples: As a scalar operator: if (101 .. 200) { print; } # print 2nd hundred lines next line if (1 .. /^$/); # skip header lines s/^/> / if (/^$/ .. eof()); # quote body As an array operator: for (101 .. 200) { print; } # print $_ 100 times @foo = @foo[$[ .. $#foo]; # an expensive no-op @foo = @foo[$#foo-4 .. $#foo]; # slice last 5 items Here is what C has that perl doesn't: unary & Address-of operator. unary * Dereference-address operator. (TYPE) Type casting operator. Like C, perl does a certain amount of expression evalua- tion at compile time, whenever it determines that all of the arguments to an operator are static and have no side effects. In particular, string concatenation happens at compile time between literals that don't do variable sub- stitution. Backslash interpretation also happens at com- pile time. You can say 'Now is the time for all' . "\n" . 'good men to come to.' and this all reduces to one string internally. The autoincrement operator has a little extra built-in magic to it. If you increment a variable that is numeric, or that has ever been used in a numeric context, you get a normal increment. If, however, the variable has only been used in string contexts since it was set, and has a value that is not null and matches the pattern /^[a-zA-Z]*[0-9]*$/, the increment is done as a string, preserving each character within its range, with carry. The range operator (in an array context) makes use of the magical autoincrement algorithm if the minimum and maximum are strings. You can say @alphabet = ('A' .. 'Z'); to get all the letters of the alphabet, or $hexdigit = (0 .. 9, 'a' .. 'f')[$num & 15]; to get a hexadecimal digit, or @z2 = ('01' .. '31'); print @z2[$mday]; to get dates with leading zeros. (If the final value specified is not in the sequence that the magical incre- ment would produce, the sequence goes until the next value would be longer than the final value specified.) The || and && operators differ from C's in that, rather than returning 0 or 1, they return the last value evalu- ated. Along with the literals and variables mentioned earlier, the operations in the following section can serve as terms in an expression. Some of these operations take a LIST as an argument. Such a list can consist of any combination of scalar arguments or array values; the array values will be included in the list as if each individual element were interpolated at that point in the list, forming a longer single-dimensional array value. Elements of the LIST should be separated by commas. If an operation is listed both with and without parentheses around its arguments, it means you can either use it as a unary operator or as a function call. To use it as a function call, the next token on the same line must be a left parenthesis. (There may be intervening white space.) Such a function then has highest precedence, as you would expect from a function. If any token other than a left parenthesis follows, then it is a unary operator, with a precedence depending only on whether it is a LIST operator or not. LIST operators have lowest precedence. All other unary operators have a precedence greater than relational operators but less than arithmetic operators. See the section on Precedence. For operators that can be used in either a scalar or array context, failure is generally indicated in a scalar context by returning the undefined value, and in an array context by returning the null list. Remember though that THERE IS NO GENERAL RULE FOR CONVERTING A LIST INTO A SCALAR. Each operator decides which sort of scalar it would be most appropriate to return. Some operators return the length of the list that would have been returned in an array context. Some operators return the first value in the list. Some operators return the last value in the list. Some operators return a count of suc- cessful operations. In general, they do what you want, unless you want consistency. Precedence OPERATORS: Perl operators have the following associativity and prece- dence: nonassoc print printf exec system sort reverse chmod chown kill unlink utime die return left , right = += -= *= etc. right ?: nonassoc .. left || left && left | ^ left & nonassoc == != <=> eq ne cmp nonassoc < > <= >= lt gt le ge nonassoc chdir exit eval reset sleep rand umask nonassoc -r -w -x etc. left << >> left + - . left * / % x left =~ !~ right ! ~ and unary minus right ** nonassoc ++ -- left '(' FUNCTIONS: atan2(Y,X) Returns the arctangent of Y/X in the range -PI to PI. chop(LIST) chop(VARIABLE) chop VARIABLE chop Chops off the last character of a string and returns the character chopped. It's used primar- ily to remove the newline from the end of an input record, but is much more efficient than s/\n// because it neither scans nor copies the string. If VARIABLE is omitted, chops $_. Example: If you chop a list, each element is chopped. Only the value of the last chop is returned. cos(EXPR) cos EXPR Returns the cosine of EXPR (expressed in radians). If EXPR is omitted takes cosine of $_. crypt(PLAINTEXT,SALT) Encrypts a string exactly like the crypt() func- tion in the C library. Useful for checking the password file for lousy passwords. Only the guys wearing white hats should do this. defined(EXPR) defined EXPR Returns a boolean value saying whether the lvalue EXPR has a real value or not. Many operations return the undefined value under exceptional con- ditions, such as end of file, uninitialized vari- able, system error and such. This function allows you to distinguish between an undefined null string and a defined null string with operations that might return a real null string, in particu- lar referencing elements of an array. You may also check to see if arrays or subroutines exist. Use on predefined variables is not guaranteed to produce intuitive results. Examples: print if defined $switch{'D'}; print "$val\n" while defined($val = pop(@ary)); die "Can't readlink $sym: $!" unless defined($value = readlink $sym); eval '@foo = ()' if defined(@foo); die "No XYZ package defined" unless defined %_XYZ; sub foo { defined &$bar ? &$bar(@_) : die "No bar"; } See also undef. delete $ASSOC{KEY} Deletes the specified value from the specified associative array. Returns the deleted value, or the undefined value if nothing was deleted. Deleting from $ENV{} modifies the environment. Deleting from an array bound to a dbm file deletes the entry from the dbm file. The following deletes all the values of an asso- ciative array: foreach $key (keys %ARRAY) { delete $ARRAY{$key}; } (But it would be faster to use the reset command. Saying undef %ARRAY is faster yet.) do SUBROUTINE (LIST) Executes a SUBROUTINE declared by a sub declara- tion, and returns the value of the last expression evaluated in SUBROUTINE. If there is no subrou- tine by that name, produces a fatal error. (You may use the "defined" operator to determine if a subroutine exists.) If you pass arrays as part of LIST you may wish to pass the length of the array in front of each array. (See the section on sub- routines later on.) The parentheses are required to avoid confusion with the "do EXPR" form. SUBROUTINE may also be a single scalar variable, in which case the name of the subroutine to exe- cute is taken from the variable. As an alternate (and preferred) form, you may call a subroutine by prefixing the name with an amper- sand: &foo(@args). If you aren't passing any arguments, you don't have to use parentheses. If you omit the parentheses, no @_ array is passed to the subroutine. The & form is also used to spec- ify subroutines to the defined and undef opera- tors: if (defined &$var) { &$var($parm); undef &$var; } exp(EXPR) exp EXPR Returns e to the power of EXPR. If EXPR is omit- ted, gives exp($_). gmtime(EXPR) gmtime EXPR Converts a time as returned by the time function to a 9-element array with the time analyzed for the Greenwich timezone. Typically used as fol- lows: ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = gmtime(time); All array elements are numeric, and come straight out of a struct tm. In particular this means that $mon has the range 0..11 and $wday has the range 0..6. If EXPR is omitted, does gmtime(time). grep(EXPR,LIST) Evaluates EXPR for each element of LIST (locally setting $_ to each element) and returns the array value consisting of those elements for which the expression evaluated to true. In a scalar con- text, returns the number of times the expression was true. @foo = grep(!/^#/, @bar); # weed out comments Note that, since $_ is a reference into the array value, it can be used to modify the elements of the array. While this is useful and supported, it can cause bizarre results if the LIST is not a named array. hex(EXPR) hex EXPR Returns the decimal value of EXPR interpreted as an hex string. (To interpret strings that might start with 0 or 0x see oct().) If EXPR is omit- ted, uses $_. index(STR,SUBSTR,POSITION) index(STR,SUBSTR) Returns the position of the first occurrence of SUBSTR in STR at or after POSITION. If POSITION is omitted, starts searching from the beginning of the string. The return value is based at 0, or whatever you've set the $[ variable to. If the substring is not found, returns one less than the base, ordinarily -1. int(EXPR) int EXPR Returns the integer portion of EXPR. If EXPR is omitted, uses $_. join(EXPR,LIST) join(EXPR,ARRAY) Joins the separate strings of LIST or ARRAY into a single string with fields separated by the value of EXPR, and returns the string. Example: $_ = join(':', $login,$passwd,$uid,$gid,$gcos,$home,$shell); See split. keys(ASSOC_ARRAY) keys ASSOC_ARRAY Returns a normal array consisting of all the keys of the named associative array. The keys are returned in an apparently random order, but it is the same order as either the values() or each() function produces (given that the associative array has not been modified). Here is yet another way to print your environment: @keys = keys %ENV; @values = values %ENV; while ($#keys >= 0) { print pop(@keys), '=', pop(@values), "\n"; } or how about sorted by key: foreach $key (sort(keys %ENV)) { print $key, '=', $ENV{$key}, "\n"; } length(EXPR) length EXPR Returns the length in characters of the value of EXPR. If EXPR is omitted, returns length of $_. local(LIST) Declares the listed variables to be local to the enclosing block, subroutine, eval or "do". All the listed elements must be legal lvalues. This operator works by saving the current values of those variables in LIST on a hidden stack and restoring them upon exiting the block, subroutine or eval. This means that called subroutines can also reference the local variable, but not the global one. The LIST may be assigned to if desired, which allows you to initialize your local variables. (If no initializer is given for a par- ticular variable, it is created with an undefined value.) Commonly this is used to name the parame- ters to a subroutine. localtime(EXPR) localtime EXPR Converts a time as returned by the time function to a 9-element array with the time analyzed for the local timezone. Typically used as follows: ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time); All array elements are numeric, and come straight out of a struct tm. In particular this means that $mon has the range 0..11 and $wday has the range 0..6. If EXPR is omitted, does localtime(time). log(EXPR) log EXPR Returns logarithm (base e) of EXPR. If EXPR is omitted, returns log of $_. m/PATTERN/gio /PATTERN/gio Searches a string for a pattern match, and returns true (1) or false (''). If no string is specified via the =~ or !~ operator, the $_ string is searched. (The string specified with =~ need not be an lvalue--it may be the result of an expres- sion evaluation, but remember the =~ binds rather tightly.) See also the section on regular expres- sions. If / is the delimiter then the initial 'm' is optional. With the 'm' you can use any pair of non-alphanumeric characters as delimiters. This is particularly useful for matching Unix path names that contain '/'. If the final delimiter is followed by the optional letter 'i', the matching is done in a case-insensitive manner. PATTERN may contain references to scalar variables, which will be interpolated (and the pattern recompiled) every time the pattern search is evaluated. (Note that $) and $| may not be interpolated because they look like end-of-string tests.) If you want such a pattern to be compiled only once, add an "o" after the trailing delimiter. This avoids expen- sive run-time recompilations, and is useful when the value you are interpolating won't change over the life of the script. If the PATTERN evaluates to a null string, the most recent successful regu- lar expression is used instead. If used in a context that requires an array value, a pattern match returns an array consisting of the subexpressions matched by the parentheses in the pattern, i.e. ($1, $2, $3...). It does NOT actu- ally set $1, $2, etc. in this case, nor does it set $+, $`, $& or $'. If the match fails, a null array is returned. If the match succeeds, but there were no parentheses, an array value of (1) is returned. The "g" modifier specifies global pattern match- ing--that is, matching as many times as possible within the string. How it behaves depends on the context. In an array context, it returns a list of all the substrings matched by all the parenthe- ses in the regular expression. If there are no parentheses, it returns a list of all the matched strings, as if there were parentheses around the whole pattern. In a scalar context, it iterates through the string, returning TRUE each time it matches, and FALSE when it eventually runs out of matches. (In other words, it remembers where it left off last time and restarts the search at that point.) It presumes that you have not modified the string since the last match. Modifying the string between matches may result in undefined behavior. (You can actually get away with in- place modifications via substr() that do not change the length of the entire string. In gen- eral, however, you should be using s///g for such modifications.) Examples: # array context ($one,$five,$fifteen) = (`uptime` =~ /(\d+\.\d+)/g); # scalar context $/ = ""; $* = 1; while ($paragraph = <>) { while ($paragraph =~ /[a-z]['")]*[.!?]+['")]*\s/g) { $sentences++; } } print "$sentences\n"; oct(EXPR) oct EXPR Returns the decimal value of EXPR interpreted as an octal string. (If EXPR happens to start off with 0x, interprets it as a hex string instead.) The following will handle decimal, octal and hex in the standard notation: $val = oct($val) if $val =~ /^0/; If EXPR is omitted, uses $_. ord(EXPR) ord EXPR Returns the numeric ascii value of the first char- acter of EXPR. If EXPR is omitted, uses $_. pack(TEMPLATE,LIST) Takes an array or list of values and packs it into a binary structure, returning the string contain- ing the structure. The TEMPLATE is a sequence of characters that give the order and type of values, as follows: A An ascii string, will be space padded. a An ascii string, will be null padded. c A signed char value. C An unsigned char value. s A signed short value. S An unsigned short value. i A signed integer value. I An unsigned integer value. l A signed long value. L An unsigned long value. n A short in "network" order. N A long in "network" order. f A single-precision float in the native format. d A double-precision float in the native format. p A pointer to a string. v A short in "VAX" (little-endian) order. V A long in "VAX" (little-endian) order. x A null byte. X Back up a byte. @ Null fill to absolute position. u A uuencoded string. b A bit string (ascending bit order, like vec()). B A bit string (descending bit order). h A hex string (low nybble first). H A hex string (high nybble first). Each letter may optionally be followed by a number which gives a repeat count. With all types except "a", "A", "b", "B", "h" and "H", the pack function will gobble up that many values from the LIST. A * for the repeat count means to use however many items are left. The "a" and "A" types gobble just one value, but pack it as a string of length count, padding with nulls or spaces as necessary. (When unpacking, "A" strips trailing spaces and nulls, but "a" does not.) Likewise, the "b" and "B" fields pack a string that many bits long. The "h" and "H" fields pack a string that many nybbles long. Real numbers (floats and doubles) are in the native machine format only; due to the multi- plicity of floating formats around, and the lack of a standard "network" representation, no facil- ity for interchange has been made. This means that packed floating point data written on one machine may not be readable on another - even if both use IEEE floating point arithmetic (as the endian-ness of the memory representation is not part of the IEEE spec). Note that perl uses dou- bles internally for all numeric calculation, and converting from double -> float -> double will lose precision (i.e. unpack("f", pack("f", $foo)) will not in general equal $foo). Examples: $foo = pack("cccc",65,66,67,68); # foo eq "ABCD" $foo = pack("c4",65,66,67,68); # same thing $foo = pack("ccxxcc",65,66,67,68); # foo eq "AB\0\0CD" $foo = pack("s2",1,2); # "\1\0\2\0" on little-endian # "\0\1\0\2" on big-endian $foo = pack("a4","abcd","x","y","z"); # "abcd" $foo = pack("aaaa","abcd","x","y","z"); # "axyz" $foo = pack("a14","abcdefg"); # "abcdefg\0\0\0\0\0\0\0" $foo = pack("i9pl", gmtime); # a real struct tm (on my system anyway) sub bintodec { unpack("N", pack("B32", substr("0" x 32 . shift, -32))); } The same template may generally also be used in the unpack function. pop(ARRAY) pop ARRAY Pops and returns the last value of the array, shortening the array by 1. Has the same effect as $tmp = $ARRAY[$#ARRAY--]; If there are no elements in the array, returns the undefined value. push(ARRAY,LIST) Treats ARRAY (@ is optional) as a stack, and pushes the values of LIST onto the end of ARRAY. The length of ARRAY increases by the length of LIST. Has the same effect as for $value (LIST) { $ARRAY[++$#ARRAY] = $value; } but is more efficient. q/STRING/ qq/STRING/ qx/STRING/ These are not really functions, but simply syntac- tic sugar to let you avoid putting too many back- slashes into quoted strings. The q operator is a generalized single quote, and the qq operator a generalized double quote. The qx operator is a generalized backquote. Any non-alphanumeric delimiter can be used in place of /, including newline. If the delimiter is an opening bracket or parenthesis, the final delimiter will be the corresponding closing bracket or parenthesis. (Embedded occurrences of the closing bracket need to be backslashed as usual.) Examples: $foo = q!I said, "You said, 'She said it.'"!; $bar = q('This is it.'); $today = qx{ date }; $_ .= qq *** The previous line contains the naughty word "$&".\n if /(ibm|apple|awk)/; # :-) rand(EXPR) rand EXPR rand Returns a random fractional number between 0 and the value of EXPR. (EXPR should be positive.) If EXPR is omitted, returns a value between 0 and 1. See also srand(). return LIST Returns from a subroutine with the value speci- fied. (Note that a subroutine can automatically return the value of the last expression evaluated. That's the preferred method--use of an explicit return is a bit slower.) reverse(LIST) reverse LIST In an array context, returns an array value con- sisting of the elements of LIST in the opposite order. In a scalar context, returns a string value consisting of the bytes of the first element of LIST in the opposite order. rindex(STR,SUBSTR,POSITION) rindex(STR,SUBSTR) Works just like index except that it returns the position of the LAST occurrence of SUBSTR in STR. If POSITION is specified, returns the last occur- rence at or before that position. s/PATTERN/REPLACEMENT/gieo Searches a string for a pattern, and if found, replaces that pattern with the replacement text and returns the number of substitutions made. Otherwise it returns false (0). The "g" is optional, and if present, indicates that all occurrences of the pattern are to be replaced. The "i" is also optional, and if present, indi- cates that matching is to be done in a case-insen- sitive manner. The "e" is likewise optional, and if present, indicates that the replacement string is to be evaluated as an expression rather than just as a double-quoted string. Any non-alphanu- meric delimiter may replace the slashes; if single quotes are used, no interpretation is done on the replacement string (the e modifier overrides this, however); if backquotes are used, the replacement string is a command to execute whose output will be used as the actual replacement text. If the PATTERN is delimited by bracketing quotes, the REPLACEMENT has its own pair of quotes, which may or may not be bracketing quotes, e.g. s(foo)(bar) or s<foo>/bar/. If no string is specified via the =~ or !~ operator, the $_ string is searched and modified. (The string specified with =~ must be a scalar variable, an array element, or an assign- ment to one of those, i.e. an lvalue.) If the pattern contains a $ that looks like a variable rather than an end-of-string test, the variable will be interpolated into the pattern at run-time. If you only want the pattern compiled once the first time the variable is interpolated, add an "o" at the end. If the PATTERN evaluates to a null string, the most recent successful regular expression is used instead. See also the section on regular expressions. Examples: s/\bgreen\b/mauve/g; # don't change wintergreen $path =~ s|/usr/bin|/usr/local/bin|; s/Login: $foo/Login: $bar/; # run-time pattern ($foo = $bar) =~ s/bar/foo/; $_ = 'abc123xyz'; s/\d+/$&*2/e; # yields 'abc246xyz' s/\d+/sprintf("%5d",$&)/e; # yields 'abc 246xyz' s/\w/$& x 2/eg; # yields 'aabbcc 224466xxyyzz' s/([^ ]*) *([^ ]*)/$2 $1/; # reverse 1st two fields (Note the use of $ instead of \ in the last exam- ple. See section on regular expressions.) scalar(EXPR) Forces EXPR to be interpreted in a scalar context and returns the value of EXPR. shift(ARRAY) shift ARRAY shift Shifts the first value of the array off and returns it, shortening the array by 1 and moving everything down. If there are no elements in the array, returns the undefined value. If ARRAY is omitted, shifts the @ARGV array in the main pro- gram, and the @_ array in subroutines. (This is determined lexically.) See also unshift(), push() and pop(). Shift() and unshift() do the same thing to the left end of an array that push() and pop() do to the right end. sin(EXPR) sin EXPR Returns the sine of EXPR (expressed in radians). If EXPR is omitted, returns sine of $_. sort(SUBROUTINE LIST) sort(LIST) sort SUBROUTINE LIST sort BLOCK LIST sort LIST Sorts the LIST and returns the sorted array value. Nonexistent values of arrays are stripped out. If SUBROUTINE or BLOCK is omitted, sorts in standard string comparison order. If SUBROUTINE is speci- fied, gives the name of a subroutine that returns an integer less than, equal to, or greater than 0, depending on how the elements of the array are to be ordered. (The <=> and cmp operators are extremely useful in such routines.) SUBROUTINE may be a scalar variable name, in which case the value provides the name of the subroutine to use. In place of a SUBROUTINE name, you can provide a BLOCK as an anonymous, in-line sort subroutine. In the interests of efficiency the normal calling code for subroutines is bypassed, with the follow- ing effects: the subroutine may not be a recursive subroutine, and the two elements to be compared are passed into the subroutine not via @_ but as $a and $b (see example below). They are passed by reference so don't modify $a and $b. Examples: # sort lexically @articles = sort @files; # same thing, but with explicit sort routine @articles = sort {$a cmp $b} @files; # same thing in reversed order @articles = sort {$b cmp $a} @files; # sort numerically ascending @articles = sort {$a <=> $b} @files; # sort numerically descending @articles = sort {$b <=> $a} @files; # sort using explicit subroutine name sub byage { $age{$a} <=> $age{$b}; # presuming integers } @sortedclass = sort byage @class; sub reverse { $b cmp $a; } @harry = ('dog','cat','x','Cain','Abel'); @george = ('gone','chased','yz','Punished','Axed'); print sort @harry; # prints AbelCaincatdogx print sort reverse @harry; # prints xdogcatCainAbel print sort @george, 'to', @harry; # prints AbelAxedCainPunishedcatchaseddoggonetoxyz splice(ARRAY,OFFSET,LENGTH,LIST) splice(ARRAY,OFFSET,LENGTH) splice(ARRAY,OFFSET) Removes the elements designated by OFFSET and LENGTH from an array, and replaces them with the elements of LIST, if any. Returns the elements removed from the array. The array grows or shrinks as necessary. If LENGTH is omitted, removes everything from OFFSET onward. The fol- lowing equivalencies hold (assuming $[ == 0): push(@a,$x,$y) splice(@a,$#a+1,0,$x,$y) pop(@a) splice(@a,-1) shift(@a) splice(@a,0,1) unshift(@a,$x,$y) splice(@a,0,0,$x,$y) $a[$x] = $y splice(@a,$x,1,$y); Example, assuming array lengths are passed before arrays: sub aeq { # compare two array values local(@a) = splice(@_,0,shift); local(@b) = splice(@_,0,shift); return 0 unless @a == @b; # same len? while (@a) { return 0 if pop(@a) ne pop(@b); } return 1; } if (&aeq($len,@foo[1..$len],0+@bar,@bar)) { ... } split(/PATTERN/,EXPR,LIMIT) split(/PATTERN/,EXPR) split(/PATTERN/) split Splits a string into an array of strings, and returns it. (If not in an array context, returns the number of fields found and splits into the @_ array. (In an array context, you can force the split into @_ by using ?? as the pattern delim- iters, but it still returns the array value.)) If EXPR is omitted, splits the $_ string. If PATTERN is also omitted, splits on whitespace (/[ \t\n]+/). Anything matching PATTERN is taken to be a delimiter separating the fields. (Note that the delimiter may be longer than one charac- ter.) If LIMIT is specified, splits into no more than that many fields (though it may split into fewer). If LIMIT is unspecified, trailing null fields are stripped (which potential users of pop() would do well to remember). A pattern matching the null string (not to be confused with a null pattern //, which is just one member of the set of patterns matching a null string) will split the value of EXPR into separate characters at each point it matches that way. For example: print join(':', split(/ */, 'hi there')); produces the output 'h:i:t:h:e:r:e'. The LIMIT parameter can be used to partially split a line ($login, $passwd, $remainder) = split(/:/, $_, 3); (When assigning to a list, if LIMIT is omitted, perl supplies a LIMIT one larger than the number of variables in the list, to avoid unnecessary work. For the list above LIMIT would have been 4 by default. In time critical applications it behooves you not to split into more fields than you really need.) If the PATTERN contains parentheses, additional array elements are created from each matching sub- string in the delimiter. split(/([,-])/,"1-10,20"); produces the array value (1,'-',10,',',20) The pattern /PATTERN/ may be replaced with an expression to specify patterns that vary at run- time. (To do runtime compilation only once, use /$variable/o.) As a special case, specifying a space (' ') will split on white space just as split with no arguments does, but leading white space does NOT produce a null first field. Thus, split(' ') can be used to emulate awk's default behavior, whereas split(/ /) will give you as many null initial fields as there are leading spaces. sprintf(FORMAT,LIST) Returns a string formatted by the usual printf conventions. The * character is not supported. sqrt(EXPR) sqrt EXPR Return the square root of EXPR. If EXPR is omit- ted, returns square root of $_. srand(EXPR) srand EXPR Sets the random number seed for the rand operator. If EXPR is omitted, does srand(time). substr(EXPR,OFFSET,LEN) substr(EXPR,OFFSET) Extracts a substring out of EXPR and returns it. First character is at offset 0, or whatever you've set $[ to. If OFFSET is negative, starts that far from the end of the string. If LEN is omitted, returns everything to the end of the string. You can use the substr() function as an lvalue, in which case EXPR must be an lvalue. If you assign something shorter than LEN, the string will shrink, and if you assign something longer than LEN, the string will grow to accommodate it. To keep the string the same length you may need to pad or chop your value using sprintf(). time Returns the number of non-leap seconds since 00:00:00 UTC, January 1, 1970. Suitable for feed- ing to gmtime() and localtime(). tr/SEARCHLIST/REPLACEMENTLIST/cds y/SEARCHLIST/REPLACEMENTLIST/cds Translates all occurrences of the characters found in the search list with the corresponding charac- ter in the replacement list. It returns the num- ber of characters replaced or deleted. If no string is specified via the =~ or !~ operator, the $_ string is translated. (The string specified with =~ must be a scalar variable, an array ele- ment, or an assignment to one of those, i.e. an lvalue.) For sed devotees, y is provided as a synonym for tr. If the SEARCHLIST is delimited by bracketing quotes, the REPLACEMENTLIST has its own pair of quotes, which may or may not be bracketing quotes, e.g. tr[A-Z][a-z] or tr(+-*/)/ABCD/. If the c modifier is specified, the SEARCHLIST character set is complemented. If the d modifier is specified, any characters specified by SEARCH- LIST that are not found in REPLACEMENTLIST are deleted. (Note that this is slightly more flexi- ble than the behavior of some tr programs, which delete anything they find in the SEARCHLIST, period.) If the s modifier is specified, sequences of characters that were translated to the same character are squashed down to 1 instance of the character. If the d modifier was used, the REPLACEMENTLIST is always interpreted exactly as specified. Other- wise, if the REPLACEMENTLIST is shorter than the SEARCHLIST, the final character is replicated till it is long enough. If the REPLACEMENTLIST is null, the SEARCHLIST is replicated. This latter is useful for counting characters in a class, or for squashing character sequences in a class. Examples: $ARGV[1] =~ y/A-Z/a-z/; # canonicalize to lower case $cnt = tr/*/*/; # count the stars in $_ $cnt = tr/0-9//; # count the digits in $_ tr/a-zA-Z//s; # bookkeeper -> bokeper ($HOST = $host) =~ tr/a-z/A-Z/; y/a-zA-Z/ /cs; # change non-alphas to single space tr/\200-\377/\0-\177/; # delete 8th bit undef(EXPR) undef EXPR undef Undefines the value of EXPR, which must be an lvalue. Use only on a scalar value, an entire array, or a subroutine name (using &). (Undef will probably not do what you expect on most pre- defined variables or dbm array values.) Always returns the undefined value. You can omit the EXPR, in which case nothing is undefined, but you still get an undefined value that you could, for instance, return from a subroutine. Examples: undef $foo; undef $bar{'blurfl'}; undef @ary; undef %assoc; undef &mysub; return (wantarray ? () : undef) if $they_blew_it; unpack(TEMPLATE,EXPR) Unpack does the reverse of pack: it takes a string representing a structure and expands it out into an array value, returning the array value. (In a scalar context, it merely returns the first value produced.) The TEMPLATE has the same format as in the pack function. Here's a subroutine that does substring: sub substr { local($what,$where,$howmuch) = @_; unpack("x$where a$howmuch", $what); } and then there's sub ord { unpack("c",$_[0]); } In addition, you may prefix a field with a %<num- ber> to indicate that you want a <number>-bit checksum of the items instead of the items them- selves. Default is a 16-bit checksum. For exam- ple, the following computes the same number as the System V sum program: while (<>) { $checksum += unpack("%16C*", $_); } $checksum %= 65536; unshift(ARRAY,LIST) Does the opposite of a shift. Or the opposite of a push, depending on how you look at it. Prepends list to the front of the array, and returns the number of elements in the new array. unshift(ARGV, '-e') unless $ARGV[0] =~ /^-/; values(ASSOC_ARRAY) values ASSOC_ARRAY Returns a normal array consisting of all the val- ues of the named associative array. The values are returned in an apparently random order, but it is the same order as either the keys() or each() function would produce on the same array. See also keys() and each(). vec(EXPR,OFFSET,BITS) Treats a string as a vector of unsigned integers, and returns the value of the bitfield specified. May also be assigned to. BITS must be a power of two from 1 to 32. Vectors created with vec() can also be manipulated with the logical operators |, & and ^, which will assume a bit vector operation is desired when both operands are strings. This interpretation is not enabled unless there is at least one vec() in your program, to protect older programs. To transform a bit vector into a string or array of 0's and 1's, use these: $bits = unpack("b*", $vector); @bits = split(//, unpack("b*", $vector)); If you know the exact length in bits, it can be used in place of the *. wantarray Returns true if the context of the currently exe- cuting subroutine is looking for an array value. Returns false if the context is looking for a scalar. return wantarray ? () : undef; REGULAR EXPRESSIONS: The patterns used in pattern matching are regular expres- sions such as those supplied in the Version 8 regexp rou- tines. (In fact, the routines are derived from Henry Spencer's freely redistributable reimplementation of the V8 routines.) In addition, \w matches an alphanumeric character (including "_") and \W a nonalphanumeric. Word boundaries may be matched by \b, and non-boundaries by \B. A whitespace character is matched by \s, non-whitespace by \S. A numeric character is matched by \d, non-numeric by \D. You may use \w, \s and \d within character classes. Also, \n, \r, \f, \t and \NNN have their normal interpre- tations. Within character classes \b represents backspace rather than a word boundary. Alternatives may be sepa- rated by |. The bracketing construct ( ... ) may also be used, in which case \<digit> matches the digit'th sub- string. (Outside of the pattern, always use $ instead of \ in front of the digit. The scope of $<digit> (and $`, $& and $') extends to the end of the enclosing BLOCK or eval string, or to the next pattern match with subexpres- sions. The \<digit> notation sometimes works outside the current pattern, but should not be relied upon.) You may have as many parentheses as you wish. If you have more than 9 substrings, the variables $10, $11, ... refer to the corresponding substring. Within the pattern, \10, \11, etc. refer back to substrings if there have been at least that many left parens before the backreference. Otherwise (for backward compatibilty) \10 is the same as \010, a backspace, and \11 the same as \011, a tab. And so on. (\1 through \9 are always backreferences.) $+ returns whatever the last bracket match matched. $& returns the entire matched string. ($0 used to return the same thing, but not any more.) $` returns everything before the matched string. $' returns everything after the matched string. Examples: s/^([^ ]*) *([^ ]*)/$2 $1/; # swap first two words if (/Time: (..):(..):(..)/) { $hours = $1; $minutes = $2; $seconds = $3; } By default, the ^ character is only guaranteed to match at the beginning of the string, the $ character only at the end (or before the newline at the end) and perl does cer- tain optimizations with the assumption that the string contains only one line. The behavior of ^ and $ on embed- ded newlines will be inconsistent. You may, however, wish to treat a string as a multi-line buffer, such that the ^ will match after any newline within the string, and $ will match before any newline. At the cost of a little more overhead, you can do this by setting the variable $* to 1. Setting it back to 0 makes perl revert to its old behav- ior. To facilitate multi-line substitutions, the . character never matches a newline (even when $* is 0). In particu- lar, the following leaves a newline on the $_ string: $_ = <STDIN>; s/.*(some_string).*/$1/; If the newline is unwanted, try one of s/.*(some_string).*\n/$1/; s/.*(some_string)[^\000]*/$1/; s/.*(some_string)(.|\n)*/$1/; chop; s/.*(some_string).*/$1/; /(some_string)/ && ($_ = $1); Any item of a regular expression may be followed with dig- its in curly brackets of the form {n,m}, where n gives the minimum number of times to match the item and m gives the maximum. The form {n} is equivalent to {n,n} and matches exactly n times. The form {n,} matches n or more times. (If a curly bracket occurs in any other context, it is treated as a regular character.) The * modifier is equiv- alent to {0,}, the + modifier to {1,} and the ? modifier to {0,1}. There is no limit to the size of n or m, but large numbers will chew up more memory. You will note that all backslashed metacharacters in perl are alphanumeric, such as \b, \w, \n. Unlike some other regular expression languages, there are no backslashed symbols that aren't alphanumeric. So anything that looks like \\, \(, \), \<, \>, \{, or \} is always interpreted as a literal character, not a metacharacter. This makes it simple to quote a string that you want to use for a pattern but that you are afraid might contain metacharac- ters. Simply quote all the non-alphanumeric characters: $pattern =~ s/(\W)/\\$1/g; ----------------- END of Perl Expression Documentation ----------------