Perl: Concatenating Text and More (Page 1 of 4 )
This marks the finale of our coverage of text in Perl (until we get into some more of the advanced Perl features at any rate). We left off with the here document and how to use it to display text exactly as we type it in, using a Mark Twain poem that he had written for his daughter's tombstone (which interestingly enough was a rewrite of another poet's poem). We also learned a little bit about ASCII and the values as they pertain to text, showcasing the 93 visible characters.
In this tutorial we are going to pick up on our discussion of ways to manipulate strings, starting with concatenation, or combining, and working our way through to converting strings. It's a lot to cover in a short time, so let's get to work.
Concatenation...The Function You Can Barely Spell
A lot of the string functions resemble mathematical equations. For instance, our first batter up is pretty similar to a simple addition. When you concatenate, all you are doing is adding two strings together. Here is an example:
#!/usr/bin/perl
print "My name is " . "whicka whicka " . "Mr. Sippi.";
This results in the following being displayed to your screen:
My name is whicka whicka Mr. Sippi.
You can also assign a variable to another variable through concatenation. In this next example, we are going to display the slightly repetitive lyrics of the Spaniels song, "Goodnight Sweetheart, Goodnight":
#!/usr/bin/perl
$a='Goodnight sweetheart';
$b="Well, it's time to go";
$c="\n";
$d='I hate to leave you, but I really must say';
$e= $a . ', goodnight';
print $a;
print $c . $b;
print $c . $a;
print $c . $b;
print $c . $d;
print $c . $e;
As you can see, the concatenation occurs with the variable $e, in which we add the string ', goodnight' to the variable $a. Afterward the variable $e holds the value: "Goodnight sweetheart, goodnight". This is the result of our program:
Goodnight sweetheart
Well, it's time to go
Goodnight sweetheart
Well, it's time to go
I hate to leave you, but I really must say
Goodnight sweetheart, goodnight
Note that when you have double quoted strings, you don't always need to concatenate. Observe this sample:
#!/usr/bin/perl
$a='Big ';
$b='Macs';
print 'I like to eat ' . $a . $b;
This prints out:
I like to eat Big Macs
If we had used double quotes, we could have accomplished the same thing like this:
#!/usr/bin/perl
$a='Big ';
$b='Macs';
print "I like to eat $a $b";
Printing this:
I like to eat Big Macs
without having to use the concatenating operator (.).
Remember that single quotes do not interpret, so had you tried that method with single quotes, like this:
#!/usr/bin/perl
$a='Big ';
$b='Macs';
print 'I like to eat $a $b';
Your result would have been:
I like to eat $a $b
Which don't taste anywhere near as good.
And lastly, if you want to concatenate a variable to another variable, you can use the concatenate assignment operator (.=), as in the following example:
#!/usr/bin/perl
$a='Big ';
$b='Macs';
$a .= $b;
print "I like to eat $a";
Which is really just shorthand for:
#!/usr/bin/perl
$a='Big ';
$b='Macs';
$a = $a . $b;
print "I like to eat $a";
It might not seem like a big difference, but when you begin adding a lot of variables to one another and get into more complicated coding, it really makes a difference.
Next: Making Copies >>
More Perl Articles
More By James Payne