First Steps / More about strings
Subtitles of the Movie
One important difference between strings and numbers is that unlike strings, numbers don't need quotations marks when we refer to them. So if we set the value of a variable to a number we shouldn't use quotations marks around it. Otherwise Perl is going to interpret the value that we've given it as an arbitrary string value. So it's going to be the number 3 followed by the number 5, followed by the number 4, rather than the number 354. Similarly we don't want to get, with performing some kind of numeric operations such as adding 10 to 10, we don't want to use any quotes anywhere in there at all. Because Perl needs to be able to say ok, this is the number and this is a number, and we are going to treat them both like numbers when we come add them together. So when we go to setup our variable age, we don't have to use any quote marks. We can just put the number straight in. One final thing on the issue of the quotation marks, is that when we come to use quotation marks within a string we then have a bit of dilemma; it's not terribly inspired line of prose, but I think it shows what I am trying to say here. The Perl executable is able to tell that this whole string is a string, because of the fact that we put the quotation marks around either side of the string. However we need to put more quotation marks within the string as well, to show that the word in this case is a piece of dialog. However because the way Perl executable works, like every other programming language, is a very explicit and literal minded way. It's not able to reasonably say, oh, of course these single quotes here within the string, are not supposed to be anything to do with marking whether a string starts or stops. Instead Perl is going to say, ok, this section of code is a string, and then this section of code is not a string, and then this section of code is a string again. And that all make an absolute nonsense of this line of code, because then the area in here has to be interpreted as Perl code. Let's see what happens when we try to run this program. We get a rather nasty and ugly error. Bare word found with operator expected, What it's trying to tell us is that it's not liking 'hello'. Not liking that 'hello' that it doesn't make any sense to it. And it's not able to treat it as a string, because we've used a quote mark there. Now to get round it there's two ways we can do it. One way is to mix the kind of quotes we are using. We can use double quotes here, then Perl would be completely happy. Alternatively we can use double quotes on the outside, and then any single quotes that we use within the string, will be treated as simply part of the string and not anything to do whether the string itself is starting or stopping. Once again we are able to achieve the same result. However, it may be that for the reasons that we of efficiency that I mentioned earlier, we want to use single quotes; and it may be that for the reasons of prose style, we want to be using single quotes within our text as well. Help is at hand. We can use the back slash and this is called escaping a character. It's just like we did with the lower case 'n' end earlier, when we wanted to tell Perl to treat the 'n' as a new line character. In this case we are telling Perl to treat the quotation marks as simple quotation marks, rather than treating them as marking to beginning or ending of the string. We save that, return to our command prompt. As you can see, Perl is able to correctly interpret what we want those quote marks in that context to mean.
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 