When is a value a string?
- [Voiceover] In Perl, a String value is one that can be represented as a string. So here's a working copy of hello.pl, from Chapter 4 of the Exercise Files. And if I Save and Build and Run, you see it says, "Hello, World!" I'm gonna come down here, and I'm gonna change this. Instead I'm going to say, my dollar S equals "Hello, World!" And say, S is, and I'm gonna put dollar S in square brackets here so that we can see the boundaries of the String when we display it.
And so I'm gonna Save and Run, and you can see it says, S is "Hello, World," and "Hello, World," with a comma and the exclamation point is inside those square brackets so we can see where the boundaries of the String are. This helps us in circumstances like if I were to put a space in here, Save and Run, see, that space shows up and we can see the space clearly because we have the brackets around the variable. So this is a String variable because it can be represented as a string.
And so I can treat it as a string, and I can do something like this. I can say, dollar S equals dollar S and the Concatenation operator, which is represented by a period. And I can say, "This is a string." And you notice I put a space in there, before the word, This. And now when I Save and Run, you see it says, "Hello, World!" Exclamation point, space, "This is a string" period. So this is the Concatenation operator, the period there is a Concatenation operator.
And so we've concatenated one string onto the end of another. Now, instead of Concatenation, we could do something like this. We could say, "This is a string" period. Dollar S, and then, "This is another string." And when I Build and Run, you see, we have, this is a string, and then we have, "Hello, World," and this is another string.
And that, "Hello, World," is where the dollar S is, because that was the value of dollar S before we reassigned it. And so this is called, String Interpolation. The other was called, Concatenation. So Interpolation is when you have a variable inside of a string, and that variable is simply expanded inside of the string. Now obviously, if I were to remove this space here, now the interpreter thinks that dollar S vis is the name of the variable.
And you see it's already giving us an error here. If I Build and Run, you see we get Global symbol, this, requires explicit packet name. That means that there is no variable named, dollar S this, like that. And so in a circumstance like that, I'm gonna go ahead and move this space too, just for consistency. In a circumstance like this, if you want to expand this variable, this dollar S, you can put it in curly braces, like that, and that'll tell the interpreter that this Interpolation is for that variable by itself.
And now when I Save, and Build and Run, you see it works exactly as expected. The "Hello, World," there is jammed in without any spaces around it. And so I can put the spaces back. I can leave the brackets in, and sometimes I'll do that anyway, just as a matter of good practice. And I'll Build and Run, and it says, "This is a string, Hello World, this is another string." Now if I want to include quote marks in my string, let's say I want quote marks around the dollar S, if I just put the quote marks in here, that's a syntax error, because it terminates the string, and there's no operator between these two strings.
So if I Save and I try to Build, we get lots of cascading errors. It says, "Ambiguous use of dollar S," and because this string is already surrounded by double-quotes. So we need to do what's called Escape. So if I put in a backslash here, and I put in a backslash there, it tells the interpreter, these quote marks inside of this string are to be interpreted as, they're not to be interpreted as quote marks in the sense of the language, instead they're to be displayed inside the string as quote marks.
This is called Escaping them, with the backslash. And so if I Save and Run, this works exactly as we expect, and you see now, we have quotes around, "Hello, World," there. And of course, if I want to use backslashes, I simply use double-backslash, like that. I Save and Run, you see we have single backslashes in the string. Now there is another way to do this. We'll put these quote marks back, and I'm actually gonna take out the Escaping. And I'm gonna use the double-Q Interpolation quote operator, and it looks like this.
Now I'm gonna go ahead and Save and Run, and you can see that this works exactly as we expect. And now I just have single quotes in here, and they're not Escaped. And what I've done is, this is an alternate quote operator. This feature is unique to Perl. I've never seen it in another language, but it's widely used because it's so convenient. So this double-Q is the interpolating quote operator, just like a double-quote mark. And it's followed by a symbol, and so in this case I'm using a parentheses, which tells it the opening parentheses tells us that the close of the string will be a closing parentheses.
So I can use, curly braces'll work just fine. And if I Save and Run, we see that works. I can use square brackets and that works. Or I can use vertical bars like this, you'll see that's actually a pretty common one, because we don't often use those inside of a string. And so that makes this very convenient. Or you can even use slash marks like this. And this is a little less common, because slash marks are used for another purpose in Perl. But it does work in this context.
So in Perl, a Scaler is a string, as long as it makes sense to use it in a string context. And we'll get into a lot of detail about the various operations you can perform on strings later in this course.
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