Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 119 lines (70 sloc) 2.709 kb
d5f3b1b @petdance Importing from Andy original repo
authored
1 =head1 Strings
2
3 =head2 Embed strings in other strings with interpolation
4
5 Double-quoted strings can interpolate other variables inside.
6
7 my $name = "Inigo Montoya";
8 my $relative = "father";
9
10 print "My name is $name, you killed my $relative";
11
12 =head2 Non-interpolating strings
13
14 If you don't want interpolation, use single-quotes:
15
16 print 'You may have won $1,000,000';
17
18 Or you can escape the special characters (sigils):
19
20 print "You may have won \$1,000,000";
21
22 =head2 Be careful with email addresses in strings
23
24 This email address won't be what you want it to be:
25
26 my $email = "andy@foo.com";
27 print $email;
28 # Prints "andy.com"
29
30 The problem is that C<@foo> is interpolated as an array. This
31 problem is obvious if you have C<use warnings> turned on:
32
33 $ perl foo.pl
34 Possible unintended interpolation of @foo in string at foo line 1.
35 andy.com
36
37 The solution is either to use non-interpolating quotes:
38
39 my $email = 'andy@foo.com';
40 my $email = q{andy@foo.com};
41
42 or escape the C<@>
43
44 my $email = "andy\@foo.com";
45
46 A good color-coding editor will help you prevent this problem in
47 the first place.
48
a5f3708 @petdance added length
authored
49 =head2 Use C<length()> to get the length of a string
50
51 my $str = "Chicago Perl Mongers";
52 print length( $str ); # 20
53
d5f3b1b @petdance Importing from Andy original repo
authored
54 =head2 Use C<substr()> to extract strings
55
56 C<substr()> does all kinds of cool string extraction.
57
58 my $x = "Chicago Perl Mongers";
59
60 print substr( $x, 0, 4 ); # Chic
61
62 print substr( $x, 13 ); # Mongers
63
64 print substr( $x, -4 ); # gers
65
66 =head2 Don't worry (too much) about strings vs. numbers
67
68 Unlike other languages, Perl doesn't know a "string" from a "number".
69 It will do its best to DTRT.
70
71 my $phone = "312-588-2300";
72
73 my $exchange = substr( $phone, 4, 3 ); # 588
74 print sqrt( $exchange ); # 24.2487113059643
75
76 =head2 Increment non-numeric strings with the C<++> operator
77
78 You can increment a string with C<++>. The string C<"abc"> incremented
79 becomes C<"abd">.
80
81 $ cat foo.pl
82 $a = 'abc'; $a = $a + 1;
83 $b = 'abc'; $b += 1;
84 $c = 'abc'; $c++;
85
86 print join ", ", ( $a, $b, $c );
87
88 $ perl -l foo.pl
89 1, 1, abd
90
91 Note that you must use the C<++> operator. In the other two cases
92 above, the string C<"abc"> is converted to C<0> and then incremented.
f765d0c @petdance moved heredocs
authored
93
94 =head2 Create long strings with the C<''> operators
95
96 You can create long strings with
97
98 =head2 Create long strings with heredocs
99
100 Heredocs are
101
102 =over
103
104 =item * Allows unbroken text until the next marker
105
106 =item * Interpolated unless marker is in single quotes
107
108 =back
109
110 my $page = <<HERE;
111 <html>
112 <head><title>$title</title></head>
113 <body>This is a page.</body>
114 </html>
115 HERE
116
117 XXX Discuss dangers of heredocs.
118
Something went wrong with that request. Please try again.