Perl String Functions And Operators
By Justin PoirierIn the following, any reference to a string, value, or array means either a literal value (with necessary delimiters at either end if a string) or the name of a variable referring to the intended value. Any regular expression referred to is assumed to include delimiters on either side of the pattern. Functions are shown with parentheses around the parametres, but as with all Perl functions, can also be called with a space before the first parametre and no parentheses.
-
pack(data type specifier string,comma-separated list of scalar values or arrays)Returns a string containing the scalar values listed and/or the individual values comprising any arrays listed. The values will first be converted to the data types specified. The data type specifier string will contain a character for each value in the list, in the same order, specifying what data type it should be converted to out of those listed at the perl.org pack help documentation. This function is useful when data will be viewed by another computer system or another process on the same system, and must be put in a readable format first.
-
unpack(data type specifier string,string of data)Returns a list of values in native Perl formats parsed from the data string, which usually comes from another process on the same or a different computer and is in the formats specified by the data type specifier string as with pack. By list we mean a composite expression, which is a special feature of the Perl language. In Perl, any code which produces the operand of some operator will be expected to return either a scalar value or a list, ie the code will exist in scalar context or list context. An example of a list context in which unpack() could be used is the assignment of the values it returns to several scalar variables. An assignment of multiple values at once like this is performed using the assignment operator
=, with the scalars receiving values collected into a list on the left and a list of the values being assigned on the right. When a list is entered literally in code, as the variables on the left will be, they are separated by commas and enclosed in parentheses. An example of using unpack() in an assignment is shown below.
($variable1, $variable2, $variable3) = unpack("aaa", "ABC")unpack() can also be used in scalar context, in which case it returns only the first value parsed from the string.
-
join(separator string,list of strings);Returns a string containing the listed strings combined, with the separator string appearing between each.
-
split(regular expression,string)Returns a list of strings that appear as substrings of the string passed, separated by other substrings that match the regular expression.
-
string1
.string2Returns a string equal to string1 with string2 concatenated onto it.
-
string
xnumberReturns a string equal to the string passed concatenated onto itself repeatedly, so that it appears the indicated number of times.
-
qw(literal strings, without enclosing delimeters, separated by spaces)Returns a list of the strings passed. Executes at compile-time.
-
chomp(string)Returns the string with trailing newline characters removed.
-
length(string)Returns the length of the string.
-
lc(string)Returns a string equal to the string passed, but with all characters in lowercase.
-
uc(string)Returns a string equal to the string passed, but with all characters in uppercase.
-
hex(string representing an integer in hexadecimal format)Returns the integer in the string, in decimal format.
-
string1
eqstring2Returns whether the two strings are equal.
-
string1
nestring2Returns whether the two strings are not equal.
-
string1
gtstring2Returns whether, at the leftmost position at which the corresponding characters in the two strings are unequal, the character has a higher-numbered ASCII code in string1.
-
string1
ltstring2Returns whether, at the leftmost position at which the corresponding characters in the two strings are unequal, the character has a lower-numbered ASCII code in string1.
-
string1
gestring2Returns whether the two strings are equal or, at the leftmost position at which the corresponding characters in the two strings are unequal, the character has a higher-numbered ASCII code in string1.
-
string1
lestring2Returns whether the two strings are equal or, at the leftmost position at which the corresponding characters in the two strings are unequal, the character has a lower-numbered ASCII code in string1.
-
string
=~regular expressionReturns whether the string is matched by the regular expression.
-
string
!~regular expressionReturns whether the string is not matched by the regular expression.