Built-in Subroutines

Let's discuss the built-in subroutines used for performing various string operations in this lesson.

Extracting substrings

A string can possibly be divided into multiple pieces based on a separator. If the separator is “” (empty string), then each character will be extracted separately. The split subroutine is used for this purpose. If the separator is some other string, then it will become the cut point of the string. The cut point itself will not be stored as a piece of string. The split subroutine returns an array of substrings. This subroutine takes two parameters: the separator and the input string.

To extract a substring from the given string, substr is used. This subroutine takes three arguments parameters: input string, start index of the substring to be extracted, and length of the substring to be extracted. The following code snippet implements both of these subroutines.

Note: Strings, like everything in Perl, are 0-indexed, i.e. their first character is at index 0.

Create a free account to access the full course.

By signing up, you agree to Educative's Terms of Service and Privacy Policy