Okay, so I'm doing a Perl tutorial now, as if we didn't have enough going on here already. Whatever. Let's go!
First some background information:You know what, I'll just show you how Perl works and you imagine what it can be used for. Only thing I'll say: Perl is used much more than you hear of.
How to get a Perl interpreter:If you're running Windows, go to
http://www.perl.org/get.html and download one of the provided Perl distributions for Windows (I recommend ActivePerl). Install.
If not, you already have Perl.
How to create a Perl program:Open your favourite plain text editor and write some Perl code. Save as
YOUR_PROGRAM_NAME_HERE.
How to run a Perl program:Open a shell, navigate to your program and enter "perl
YOUR_PROGRAM_NAME_HERE". Hit the large generally non-convex key normally to be found on the right of your keyboard. Lean back and have fun.
If you have Windows, you may run programs whose names end with .pl just by double-clicking them, but the window that appears will vanish as soon as Perl exits, either after successfully running or after finding syntax errors. In that case, you won't get to see the syntax error messages.
If you don't have Windows, you should be competent enough to find other ways of running your programs on your own.
Start your programs with "#!/usr/bin/perl", use the .pl filename extension and mark them as executable.
How to be able to write programs in Perl:Simple, just read on!
Now, most people would start a programming tutorial with a simple example program like a Hello World!.
We'll start even simpler, because I can.
# This is a comment.
This program, when executed, successfully does nothing at all. Comments start with a # and end at the end of the line.
Did you see that program? Did you remember what it looked like? Yes? Well, that's good, because you won't see another program example in a looong time.
The reason: Every time I learn a language using a quick start guide, I am able to use the language up to the points covered in the quick start guide, and then nothing. So we'll start very slowly. Before you can read and write, you need to learn the alphabet.
So we'll start by looking at some basic data values, called scalars.
First, numbers. I'll just pile all possible forms of numbers on you, and you'll understand.
3 # An integer, exactly what it looks like.
42 # A different integer.
-5 # A negative integer.
3.7 # A floating-point number.
-3.9 # A negative floating-point number.
0b10010 # 18 in binary (starts with 0b).
0100 # 64 in octal (starts with 0).
0x1f # 31 in hexadecimal (starts with 0x).
1234567890 # A large integer.
1_23_456_7890 # Same as above. You can put underscores anywhere you like.
0x49_96_02_D2 # Same as above. Non-decimal representations can also have underscores.
1.36e29 # A very large floating-point number.
13.6E28 # Same as above. The E can be uppercase.
-13.42e-34 # A very small negative number.
I won't tell you how Perl internally handles numeric values. But don't worry, they always do
exactly what you want them to do.
Second, strings. Yes, strings are a basic data value. Never underestimate Perl again.
There are insanely many different types of string literals. Remember: Perl was designed as a language to handle large amounts of data efficiently and easily. Oh, I didn't tell you that yet. But now you know.
I'll start with the most basic type: the single-quoted string literals. The contents of such a string are exactly what they look like, with two necessary exceptions:
'Urist' # A string consisting of five characters: U, r, i, s and t.
'MAGMA' # Those five characters.
'' # An empty string. Guess how many characters it has.
'Hello\n' # Hello followed by a backslash followed by a n.
'abc\'xyz' # If you want a single-quoted string literal to contain single quotes,
# just use a backslash to stop Perl from thinking that the string literal is already over.
'wtf\\' # If you want a single-quoted string literal to end with a backslash, then typing only one backslash
# isn't a good idea, as Perl will think you wanted it to stop thinking that the string literal is already over.
# So just backslash the backslash.
'\'\\' # Therefore, the contents of this string are a single quote followed by a backslash.
That's all there is to them.
Now we'll move on to double-quoted string literals. Watch:
"Urist" # A string consisting of five characters: U, r, i, s and t. Same as with single quotes.
"" # Also an empty string.
"Hello\n" # Hello followed by a newline. Backslash-character combinations have special meanings here.
"foo\tbar" # foo, a tab, and bar.
"\"\\" # A double quote followed by a backslash.
"\x41" # An ASCII value in hexadecimal (here: a capital A).
There is much more to these double quotes, which will all come later. And remember the insanely many different types of string literals? Those will probably also have to wait.
There are two more scalar value types: references and
undef. Have patience, they will be covered.
Now, let's talk operators! There are much more, here are the basic algebra and string operators.
Numeric operators:
3 + 4 # Addition: 7
4 - 1 # Subtraction: 3
3 * 6 # Multiplication: 18
6 / 2 # Division: 2
10 / 3 # 3.33333333..., there is no integer division.
3 ** 4 # Exponentiation: 81
26 % 7 # Modulus: 5
26.1 % 7.8 # Still 5. Modulus only supports integers. Non-integers are rounded down.
'3' * "4" # 12
"0.5" * " 6foo" # 3
String operators:
"abc" . "def" # Concatenation: "abcdef".
"abc" . 'def' # Same as above: "abcdef".
'Hello world' . "\n" # "Hello world\n".
"foo" x 4 # String repetition: "foofoofoofoo"
5 x 4 # 5555
4 x 5 # 44444
Did you notice the last two examples in each code block? Yes, that's correct: Perl will automatically convert numbers to strings and back, depending on how they need to be used. Yeah, that's awesome. Numbers are converted to strings canonically, and strings are converted to numbers by cutting off leading whitespace and trailing non-digits and interpreting what's left. Perl does a
really good job of reading your mind.
Next time: Scalar variables and example programs!