Learn functions for working with text and strings.
- [Voiceover] Perl provides a number of useful functions for working with strings. Here's a working copy of string.pl from chapter 13 of the exercise files and you'll notice that we have a string here and if I run this, it outputs that string to the screen using the say function. Now, if I want to, I can get the length of the string and I can say length of string and I can run that and it tells us the length of the string is 51, and again, because this is a built in function I don't actually need the parentheses and that works just exactly the same.
The chomp function will remove line endings, if any, and so in this case it returns the number of characters removed and so if I put in a new line at the end here, and I run this you see it's removed one character. And if I go ahead and simply say string, a couple of times you'll see that it's got extra line endings in it. If I come up here and I say chomp $string, then it takes those extra line endings out, it just has the one line ending for each line that say provides.
On the other hand, the chop function, it may look like it does the same thing, I'm going to run this here, but what's interesting is that chop doesn't actually check to see if it's a line ending character. So if I remove that and run chop, then you see that it now has taken out the period at the end so chop is an old function that was in earlier versions of Perl and starting in one of the early versions of Perl 5 chomp was added, and chomp is much more common, chop by itself is very rare actually at this point.
The substring function is used to get or alter a portion of a string, and so if I say substr here and the string, and we'll start at the fifth position in the string, and we'll go for 10 characters and so if I run this you see, starting at the fifth position which is the s in string, and then string is six, there's a space that's seven, and then three letters of has is 10 and so it's displaying just 10 characters of the string.
If I remove the second argument, it'll just start at that fifth character and return the rest of the string like that, or, if I want to, I can take all of this, and I need to put this in parentheses to do this, that returns the index into the string and so I can replace that with something else, and now when I say string, you notice that it's been replaced, well I need to put in a semicolon there, and you'll notice that string has has been replaced with the word foo, so this foo, a bunch of useful instead of this string has.
The index function is used for finding a substring within a string and it'll return the position of the string starting at zero and so if I say index, string and I'm looking for the word in, you'll notice that it'll return so this is four, five, six, seven, eight, cause it starts at zero, so it's eight. So it's giving us the index eight into the string. I can give it another argument to start later in the string so if I start at position 10, then it'll find the next occurrence of in, which is much later, it's towards the end of the string, so it's at position 45.
And if I search for something that is not found, run that, it gives us a minus one. So that's how we can tell if the string is not found. Likewise, there's an rindex, which starts at the right side, so if I run this starting at the right side, I'm going to get 45 cause it's going to find that first one first, and if I say start at 10, then it'll find the one at eight, because it's starting from the right and it's starting at position 10. There's a function called reverse, which in scalar context, so I'm going to say scalar here, because normally say is in list context.
In scalar context it'll actually take a string and reverse all the letters. So when I run this you'll notice that it's backwards. If it's not in scalar context, of course it reverses a list, which in this case will do nothing cause there's just one element in the list, but if I change this and say one, two, three, four, five, then it'll reverse that and give us five, four, three, two, one. There's a lower case function which will give us our entire string in lower case, and so when I run this you'll notice the first character's lower case, but if I take something else here and change that to all caps, then run it, you'll notice that it's all still in lower case, and likewise, there's a upper case function, Uc, which, lower case u, which will take the entire string and make it upper case and there's a uc first, which will only upper case the first character, which in this case is already upper case, but if I change this to a lower case t, you'll notice that the output is still upper case, so it just capitalizes the first letter.
Now, this is not all of the string functions. In the documentation with Perl, there's an index of Perl functions by category, and the very first category is for scalars and strings, and all of these functions are listed there and if you click on any of these, you get the complete documentation for that particular function. Perl has a very rich set of string functions. For a complete list and complete documentation, see this index functions by category page in the Perl documentation, or on the perldoc website.
Watch to learn the details of the Perl syntax, from variables, conditionals, loops, and data structures to regular expressions, functions, and references. A quick-start guide is included for experienced developers who want to get up and running with Perl 5 fast, and the entire course is recommended for both new and experienced programmers alike. Later chapters cover file handling and reusing code with Perl modules, plus Perl best coding practices.
- Understanding Perl's general syntax and the anatomy of a Perl script
- Writing statements and expressions
- Creating assignments
- Working with variables and strings
- Using data types effectively
- Defining logical flow with conditionals and loops
- Using special variables
- Using Perl operators
- Performing simple Perl programming tasks with expressions
- Matching data
- Defining and calling functions
- Using references
- Handling files in the file I/O
- Using built-in functions
- Reusing code with modules
- Coding with Perl best practices