Lesson 2 – I'm On My Way

I'm on my way
From misery to happiness today.

I'm On My way.mp3
I'm On My way.wav


Strings

Strings are a sequence of letters (or "characters" in computer-geek lingo). In Perl, strings can be enclosed in double quotes or single quotes. Double quotes allow "magic stuff" to go on in between the quotes, but single quotes leave everything just as you typed it. To see what I mean, run the following text through Perl to see what happens (copy and paste it into a file, and run Perl on the file):
print 'Hello World\n';
print "Hello World\n";

print 'Hello World\n';
print "Hello World\n";
As you can see, printing "\n" causes Perl to print a new line.

OK, now let's take a closer look at this "\n" thing.  What is it?  Well, it's an "escape sequence."  The n is escaped by \, which is the "escape character".  An escape sequence is a multi-character sequence that stands for another character.  So, why do we need escape sequences?  Because some characters are invisible, or otherwise troublesome, and so we need some way to represent them in a concise and visible manner.  For example, we could have inserted an actual newline character into the double quotes, but it would look ugly, like this:

print 'Hello World\n';
print "Hello World
";

print 'Hello World\n';
print "Hello World
";

See?  It means the same thing as before, but it just doesn't look cool, and Perl is all about being cool.

Now let's meet another interesting escape sequence: \b.  It's the backspace.  Try the following Perl code:
print "Fred is way cool";
print "\b\b\b\b\b\b\b\b\bYou have been assimilated by the Borg!  Resistance is futile!\n";
As you can see, escape sequences like \b and \n represent control characters that do useful things to the output.  If we didn't have control characters, life would be boring.

Now let's try adding some variables to the mix:

$first = "Hello World\n";
print $first;
Easy huh? OK, let's try putting a string within a string:
$first = "Inner String";
$second = "Outer $first String";
print "$second\n";
Notice that with double quotes we can easily put strings within other strings. Now try it with single quotes, just to make sure we really understand the difference:
$first = 'Inner Child';
$second = 'Outer $first String';
print '$second\n';
Not quite what we wanted, was it?

Now, let's look at string concatenation:

$a = '9';
$b = '7';
$result = "$a times $b = " . ($a * $b) . "\n";
print $result;
The string concatenation operator in Perl is the dot (period). Notice that Perl is smart, and automatically converted $a and $b to integers in order do the multiplication, and then converted the result back to a string in order to do the concatenation. Since Perl is so smart, we don't even have to use quotes when dealing with numbers. Thus, most Perl programmers would have written it this way:
$a = 9;
$b = 7;
$result = "$a times $b = " . ($a * $b) . "\n";
print $result;
Let's generalize this a bit, and get $a and $b from the keyboard:
print 'Enter value for $a: ';
$a = <>;
print 'Enter value for $b: ';
$b = <>;
$result = "$a times $b = " . ($a * $b) . "\n";
print $result;
Try it. Notice something strange? It seems like there are some extra "newlines" in the output. That's because the <> operator reads a line of text from the keyboard, terminated by a newline. It's easy to get rid of the newline though. Let's try it again:
print 'Enter value for $a: ';
$a = <>;
chomp($a);
print 'Enter value for $b: ';
$b = <>;
chomp($b);
$result = "$a times $b = " . ($a * $b) . "\n";
print $result;
Ah, now that's more like it! The chomp() function throws away the trailing \n, which is something you'll want to do quite often.

OK, just for fun, let's try to write a simple "Pig Latin" translator, using the substr() function:

print "Enter your word: ";
$word = <>;
chomp($word);
print "You're new word is: " . substr($word, 1) . substr($word, 0, 1) . "ay\n";
Your final task in this lesson is to look up the function substr() in the Perl 4 documentation. Just click here . The reason that we're using the Perl 4 documentation is that it's much shorter than the Perl 5 documentation. And the reason that I'm making you look it up is that it's good for you.

OK, you've looked up the substr() function and you understand it now, right? Cool, then you are done with this lesson. The rest of you will have to stay after class until your homework is done.