Perl Scalar and String Functions

There are a lot of string or scalar functions available in Perl which let you perform different operations on strings and scalars.

Some of these functions allow you to find the string’s length, get a substring or the position of a substring in a string, change the case of the characters in the string, to mention only some of them.

In calling these functions, if no argument variable is specified, the respective function will use by default the special variable $_.

We’ll shortly describe below some of the most frequently used Perl string functions:

chomp removes trailing newlines and returns the number of characters removed
chop removes and returns the last character from a string
chr converts ASCII or Unicode values into their corresponding characters
crypt allows you to store passwords or other sensitive data using ASCII characters as encrypted strings
hex converts a hex string into the numerical corresponding value
index returns the position of the first occurrence of a substring in a string
lc converts all the characters of a string in lowercase and returns the new string
lcfirst converts the first character of a string in lowercase and returns the new string
length returns the number of bytes/characters in a string
oct converts an octal string into the numerical corresponding value
ord converts a character into its ASCII/Unicode corresponding value
pack converts a list into a string, according to a user-defined template (ex. a binary representation of a list)
q/STRING/ singly quote a string
qq/STRING/ doubly quote a string
reverse in scalar context, concatenates the elements of a list and returns a string value with all characters in the opposite order
rindex is similar to index, but returns the position of the last occurrence of a substring in a string
sprintf emulates the C sprintf function
substr extracts a substring from a string and returns it
tr/// transliterate a string
uc converts all the characters of a string in uppercases and returns the new string
ucfirst converts the first character of a string in uppercase and returns the new string