Perl Programming Page 2 - Perl: Releasing Your Inner Textuality |
Special characters are preceded by a backslash() and allow you to insert what are normally "invisible" characters into your program. Sort of like Frodo when he put on the ring, only you wouldn't think about kicking puppies or slapping ugly babies like he did. You would actually do it. Here is a nifty table of special characters and what they do:
So let's go ahead and rewrite our previous code using some of the special characters to give it a more appealing feel: #!/usr/bin/perl print "\nMy name is James Payne.\n"; print "I am the world's fattest man.\n\n"; print "My weight is equivalent to that of 17 suns.\n"; print "Or 17 of Oprah's sons. You pick.\n"; print 102; print "\n102\n"; print 10+2; print "\t\t\t\t\t10+2"; This results in: My name is James Payne. I am the world's fattest man.
My weight is equivalent to that of 17 suns. Or 17 of Oprah's sons. You pick. 102
102
12 10+2 We might also wish to use an escape if we want to print double and single quotes, or backslashes. Try the following code: #!/usr/bin/perl print "And then he said, "How are you?" "; print 'And I said, 'Fine, fine' '; As you can see, this returns an error message (well two really). To fix this, we can use an escape character, like so: #!/usr/bin/perl print "And then he said, "How are you?" "; print 'And I said, \'Fine, fine.\' '; Which results in: And then he said, "How are you?" And I said, 'Fine, fine.' There is another way to fix this problem if you do not feel like using an escape character. And that is to interchange your single and double quotes, as in the following code: #!/usr/bin/perl print "And then he said, 'How are you' "; print 'And I said, "Fine, fine." '; Which gives us the same result as before, or: And then he said, "How are you?" And I said, 'Fine, fine.' Lastly, if we want to print a backslash, it will not work if we type this: #!/usr/bin/perl print "Look at my fine backslash:\ "; However it will work if we type this: #!/usr/bin/perl print "Look at my fine backslash:\\ ";
blog comments powered by Disqus |
|
|
|
|
|
|
|