10.07.2015 Views

Beginning Web Development With Perl : From Novice to ... - Nabo

Beginning Web Development With Perl : From Novice to ... - Nabo

Beginning Web Development With Perl : From Novice to ... - Nabo

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

APPENDIX ■ PERL BASICS 289print '\ tThis is a single-quoted string.\ n';print "\ tThis is a double-quoted string.\ n";The double-quoted string will have its escape sequences processed, and the singlequotedstring will not. The output is$ perl quotes.pl\ tThis is a single quoted string.\ n This is a double-quoted string.$What do we do if we want <strong>to</strong> have a backslash in a string? This is a common concern forWindows users, as a Windows path looks something like this: C:\ WINNT\ Profiles\. . .. Ina double-quoted string, a backslash will start an escape sequence, which is not what we wantit <strong>to</strong> do.There is, of course, more than one way <strong>to</strong> do it. We can either use a single-quoted string,as shown previously, or we can escape the backslash. One principle that you’ll see often in<strong>Perl</strong>, and especially when you get <strong>to</strong> regular expressions, is that you can use a backslash <strong>to</strong>turn off any special effect a character may have. This operation is called escaping or, morecommonly, backwhacking.In this case, we want <strong>to</strong> turn off the special effect a backslash has, and so we escape it:#!/usr/bin/perl -w# quotes2.plprint "C:\ \ WINNT\ \ Profiles\ \ \ n";print 'C:\ WINNT\ Profiles\ ', "\ n";This prints the following:$ perl quotes2.plC:\ WINNT\ Profiles\C:\ WINNT\ Profiles\$Aha! Some of you may have gotten this message instead:Can't find string termina<strong>to</strong>r " ' " anywhere before EOF at quotes2.pl line 5.The reason for this is that you probably left out the space character in line 5 before thesecond single quote. Remember that \ ' tells <strong>Perl</strong> <strong>to</strong> escape the single quote, and so it merrilyheads off <strong>to</strong> look for the next quote, which of course is not there. Try this program <strong>to</strong> see how<strong>Perl</strong> treats these special cases:#!/usr/bin/perl -w# aside1.plprint 'ex\ \ er\ \ ' , ' ci\ ' se\ '' , "\ n";The output you get this time is$ perl aside1.plex\ er\ ci' se'$

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!