First Steps / Strings and quotes
Subtitles of the Movie
In the last movie there, I very briefly introduced another new concept without going into any detail. I mentioned that a string was a set of characters. You can think of a string as a set of characters all strung together, that's the name. In this movie we are going to look at what a string is. And the way in which we have to treat a string, and the difference between a string and a number. Strings and numbers are two very basic data types, within Perl. Here's an example of a string, it's just a set of characters, one following another. And because we've enclosed between two quote marks like so, Perl is able to look at the string and not attempt to interpret it as a Perl code. So, for instance it doesn't look for a function called this, and it doesn't try to interpret any of the other words within the line as variables or functions or anything else. We can either use single quotes to delimit our string, or we can use double quotes. And there are some important differences between the two. If we include a variable name within a string in double quotes, we can actually allow Perl to interpret the name of that variable even though it's within a string. Let's take a look at an example of that actually working. First of all let's setup a variable. And we are going to call that variable 'name', and its value is going to be 'Kelvyn'. Then we are going to try and print, Hi! My name is - and the variable name. We are going to call this var.pl; you can call whatever you want with your system. Now this script, although it looks like it should printout - "Hi! My name is Kelvyn." - is not actually going to work. Let's take a look at the command prompt here, and as you can see the string is printed out including the dollar sign, and including the variable name. Now if we go back to our text editor here, and use double quotes instead of single quotes, and then we return to our command prompt, run the same script again, it's been able to correctly interpret the fact that there's a variable within our string. It's going to look for the dollar sign and it's going to interpret that variable, and find the value, in this case Kelvyn. Why would we ever use single quotes, if double quotes are so much more flexible? Single quotes are the preferred option, because they require less work on the part of the Perl executable. If Perl has to look through every string that we typed to see if there are any variables in it, then there is a very small performance loss. Whereas if it knows in the case of if we are using single quotes, that everything within it is going to be plain text, then it's able to execute it slightly faster. There's another difference between single quotes and double quotes, and that's the fact that we can include new line characters. And new line character here, although it may not be very clear what's going on, is this part here: there's a back slash followed by the 'n' character, lower case 'n'. And Perl, if we use the double quotes, is able to interpret that as a new line. Lets see how that looks back at the command prompt. As you can see, Perl is being able to split that string into two lines at the place where we've included the new line character. If we include more than one new line character, it would have left a space of two lines, and so on.
Tutorial Information
| Course: | Perl Fundamentals |
| Author: | Joshua Mostafa |
| SKU: | 33403 |
| ISBN: | 1-9320-7215-2 |
| Release Date: | 2002-12-19 |
| Duration: | 7.5 hrs / 113 lessons |
| Work Files: |
Yes |
| Captions: | For Online University members only |
| Compatibility: |
Vista/XP/2000, OS X, Linux QuickTime 7, Flash 8 |
VTC Sign up & Benefits
- Unlimited Access
- 98,729 Video Tutorials (23,265 free)
- Video Available as Flash or QuickTime
- Over 1026 Courses
- $30 for One Month Access
- Multi-User Discounts Available
United States 