What is Perl’s Definition?
Perl is a computer language. Some people say it stands for “Practical Extraction and Report Language”.
What is Perl used for?
It is used to write computer programs. For example: web sites, games, operating systems.
What is the “First Program”?
The First Program is a test of the language. This test makes the operating system display a message to you which says “Hello, World!”, hence this is known as the “Hello, World! Program” in computer science.
What does the First Program look like?
It looks like this:
#!usr/bin/perl print "Hello, World!\n";
That’s it! Just text.
What does the output look like?
When you run the “Hello, World! Program” on your computer, you see the following message:
This is the first thing you would do with Perl — get the computer to say hello to you using Perl.
What’s next (if I already know about all this stuff)?
These options assume intermediate or advanced computer skills or some experience with programming, even if it’s just designing a website with HTML. You should know what the pictures below are or what they represent: In order, they are 1) a picture of the Windows Command Prompt and 2) a picture of the Linux Terminal (also called the “Command Line”). Both are ways of accessing and running Perl programs. If you don’t know what these windows are, read on to learn more at a gradual pace. For now, let’s just call ‘em windows. (Not much of a view right now I know, but things should be getting steadily more transparent!)
How would learning to program benefit me?
Learning to program itself would be a mental challenge and an opportunity to think both big and small picture. It’s both a big-picture and a detail-oriented endeavor. Personally, I plan to (very slowly) create a simple website that can greet visitors with an appropriate greeting based on the time of day. This will be my test case to determine my own competence as a beginning programmer (I started Perl Programming in January of this year). In the future, I hope to build a fully functional blog using Perl and other web technologies. In the far future, I may be interested in working at the intersection of Architecture and Software, hacking into 3D Printers and building houses using cheap raw materials and Perl.
There are so so many things you can do, really — and the more you know, the more you can imagine:
You might want to combine newsfeeds from several of your favorite websites, or build a property perimeter security system, or an anti-squirrel water cannon defense mechanism, or a simple horoscope app for your phone, or a blog, and so on. Programming can empower you to be a Creator and a Producer of both products and meaning in your own life. That is how I see it.
So why Perl? I’ve heard there are many programming languages to choose from…
One of the best things about Perl is that it has a huge and friendly user community — people who both help teach you how to program (or point you in the right direction on something), and also those that leave behind repositories of tools in addition to providing help in using and improving upon specific tools.
Another great thing about Perl is its post-modern philosophy: There is more than one way to do it. Perl leaves the programming up to you. You may see this as a bad thing, but flexibility can be advantageous too. This flexibility is what has allowed Perl to remain a reliable choice for many, many projects and organizations worldwide for the last 25 years. It is easy to modify and build on previous code. This flexibility makes Perl fast to use (once you know how to use it). If you want to see the flexibility taken to its logical conclusion, see this funny contest that Advanced Perl Coders used to enter: The Obfuscated Perl Contest. I liken it to some beautiful-and-frenetic Jazz, one possible avenue of expression using a saxophone.
Can I see some simple examples of Perl?
Here are a few simple programs, what I’ll call the First, Second, and Third Program, all in visual (screenshot) format. (Note: I am using Linux Mint 14 to show you these. In Windows it would look a bit different, but the Perl code would look the same.)
First, the whole thing at once:
Notice that in my Text Editor (called Gedit in my case) I actually have three Tabs open (three different documents are loaded). You can’t see the others currently, but they contain the Perl code that is necessary for each of the First and Second Programs.
If you were feeling bold, you could copy the exact code that I have in the document below in a normal text document (notepad, for instance) and reproduce my results I have shown in the Terminal window, even if you are using Windows and the Windows Command Prompt would, in fact, be the program to display these results to you. Let’s take it slowly, though. I’m kinda new at this too..!
Allow me first to show you what’s under those other Tabs (in Gedit above), and then I can explain what is going on with the Terminal after that.
Here is the First Program:
And here is the Second Program. Notice it is just one line longer than the First Program:
Once you begin to notice small differences in the syntax or in example code that I or others give you, you can begin to decipher what does what. This skill of observation and inference is just as if not more important than memorizing or reproducing from memory some formula. Equally true is that programming is all about the disposition that you develop as you continue to ask questions, analyze, and find new solutions to problems that you have. One of the frustrations you are likely to run into quickest is not knowing what your problem is, thus not understanding how to begin to investigate it. The best thing to do in this case is to reach out to people and ask for help. I found this to be true too, that if I document what I am trying to do, what I expected to find, what I actually found, how I have tried to look for/find answers, and what my problem is, simply stated, other programmers will be fairly happy to help. Sometimes it comes down to some astonishingly easy piece of the puzzle that was just flipped the wrong side up.
If you find my guide or any steps within it to be confusing, reach out and ask me a question: 84adam [at] thrivenotes [dot] com. You are welcome to just say ‘hi’ too! I would be happy to hear that people are reading this!
If you care to ask the experts instead, try researching your question and then asking it (if it doesn’t yet exist in some form) on Stack Overflow. Other places to go for help or to find answers include: PerlMonks and Learn Perl.
Now let’s return to the Terminal:
What’s going on in this picture?
What I have done is 1) Write three separate Perl programs, and then 2) Run each one in the Terminal.
They are not very long programs. They are about as short as you can get in fact. Each one simply “prints” something back at me in the Terminal window. This, again, would look roughly the same in Windows, except that each prompt would probably start with something like C:\Users\YourName> or something to that effect.
Let me break it down further. Notice the first line. There is a Linux command prompt (the green text with the name of my computer and a tilda and dollar sign: ~ $), followed by a PATH. A path is the location of a program or file on your computer. My First, Second, and Third Programs all happen to live in the same place on my Linux Mint system, in the adamu directory, which is inside of the home directory. (I have done this intentionally to make creating this tutorial easier to manage.) Later in the tutorial, I will show you where you can save your Perl files and all your programs, and a shortcut to avoid typing out the whole PATH each time, called ‘cd’, or Change Directory.
In order to run each of the Perl programs, I have to type ‘perl’ and then the location of (the PATH of) the Perl program I am trying to run. Hence, I type:
Next I press ENTER and the program runs. It does this instantaneously because the program itself is so small and quick to execute. (Some very very large programs with 10′s of thousands of lines of code take significantly longer.)
So all of a sudden the program has run and finished. It has displayed the Hello message as we’ve described earlier. Easy, right?
Next I run the Second Program in a similar fashion. You can see the differences in the Terminal window image above. ENTER YOUR MESSAGE HERE is also displayed. That is all for the Second Program.
What’s up with the Third Program then?
The Third Program is a kind of overview and scaled-up example meant to show you some of the key components of Perl programs and also some organizational or logistical elements that you should be aware of: Commenting, Adding New Lines, Different Ways to Do the Same Thing, Turning on Warnings, Turning on Strict Rules, Using UTF-8, Using a Recent Version of Perl, etc. (More on these things later!)
Let’s take a look at some parts of my document that I have not yet shown you. BEWARE that the first line, which is absolutely required for you to get any of your Perl programs running, is missing. For reference, it is: #!usr/bin/perl
The last screenshot of the Third Program only showed you up to the last ‘print’ line, which makes the computer display the message “INSERT YOUR MESSAGE HERE”. Below that is another challenge to you, the Third Program challenge. All I am really asking you to do here is replace ‘print’ with ‘say’, which is another way to do it in Perl. Remember our motto?
Both ‘print’ and ‘say’ do the same thing, effectively (for our current purposes). One is more modern than the other (‘say’ is newer). Perl, like any other spoken or computer programming language, is evolving. This is one of the new branches or flowers of Perl of late. It just so happens that ‘say’ adds a new line for you, where ‘print’ doesn’t (hence ‘print’ needs that linebreak instruction ‘\n’ if you want a new line). You see why I didn’t reveal this 3rd Challenge from the get-go — we are descending deeper into the rabbit hole.
Before we continue, I would like to do a recap.
What have we seen so far?
Basically, I’ve tried to show you that it is easy to write short programs that ‘print’ things back to you either in the Terminal or in Windows Command Prompt. This is an important first step to make sure your programming language of choice (i.e. Perl) is functioning correctly and that it itself is installed in the correct place in your system.
To summarize, to make the computer print something (or ‘say’ something) back to you, you need to do the following:
- Write and save a program in an easy-to-remember place, such as on the desktop or in the user directory.
- Include the first line ( #!/usr/bin/perl ) called the “shebang line”, which tells the operating system where to look for Perl.
- Tell the computer to print or say your message when the program is run.
- Open Terminal or Command Prompt and enter the instruction to use Perl to run your First Program (you will need to know the program’s Path or how to Change Directories before doing this).
- Press ENTER.
And with that you should see the message you expected.
That is your first Mini-Course in Perl Programming.
But wait! How do I get all of that set up?
If you’ve never done any of this before, I might expect you to be a bit confused at this point. That’s okay. Be confused; take it slow.
For those with more understanding of what I’m doing, or those that have most definitely already chosen which OS they will program on, skip to the next most appropriate section.
A Quick Pre-test: What is this picture?
To answer correctly, you should have noted the use of Notepad++ (the text editor) and the Windows Command Prompt (being used to run the plusplushelloworld program on Windows (7). This “Hello, World!” program is very short too.
You may also have noticed that my Windows username is 84adam, and that I keep my Perl programs on the Desktop in a “PERL” folder. Observant and knowledgeable readers will also have seen that I used the cd (change directory) command first to direct Windows to the Perl program before running it (meaning I didn’t have to type in the full file path).
If you got this, skip ahead. If you didn’t, read on.
Let’s try it! (Step One — You are now ‘learning to walk’ in the wide world of programming.)
That’s the spirit. First, assuming you have Windows, press the Windows-Key & “R” at the same time. You should see something like this:
That’s the Run prompt. It asks you what you would like to run or what commands you would like to issue. This is very bare-bones, back-to-the-basics computing we are talking about here. If you type ‘firefox’ into it, it will run Firefox. If you type ‘chrome’, a similar type of event should occur. In this case we will type ‘cmd’ to open the Command Prompt for Windows where we can perform our second step:
Next I will type the command below into the command prompt. This will tell me if I have Perl installed on my computer:
Type that then press ENTER and it returns a detailed description of your lack or wealth of Perl on your PC. Just how Perly should it be, you ask? It should hopefully say something like Perl version 5.10 or 5.12.4 or perhaps some higher number if we are lucky. If it returns an error don’t worry, we’ll take steps immediately to resolve that issue. Here’s what mine says:
Note that I have ActiveState installed. This is a program that allows you to play around with Perl on your Windows PC. You should download it from here ( http://www.activestate.com/activeperl/downloads ) if you have not already done so. (I don’t expect all readers to know to do this at this point. If you do, good for you.) You will need ActiveState’s ActivePerl ASAP. (SPECIAL NOTE: Strawberry Perl is a good alternative.)
Note that my Perl version is 5.12.4 (although not for long!). … I learned this after typing ‘perl -v’ and pressing ENTER in the Command Prompt.
I will also ask you to download Notepad++ at this time in addition to getting ActiveState/ActivePerl. Notepad++ installation looks like just about anything else you have ever installed.
Step Two: Installing ActivePerl (strolling along at 2-3 mph)
- Download the program (called ActivePerl, version 5.16 or higher, in the form of an ‘MSI package’). Choose the 32 bit version if you are unsure of whether or not you have a 64-bit operating system.
- Navigate to it in Windows Explorer (it should be on the desktop or in the downloads folder).
- Double-click on the MSI file you downloaded for ActivePerl in Windows Explorer.
- Allow it to run and go through the steps to install ActivePerl. Agree to the terms, leave all the settings at default, etc. It may show you the release notes when it is done installing.
- Now open ‘Run’ (use Windows+R) > type ‘cmd’ > press ENTER > type ‘perl -v’ > press ENTER. Note whether you got an image somewhat like mine:
I now have Perl version 5.16, as I’ve updated my Perl and my ActiveState ActivePerl program to the latest release in order to complete this tutorial. If you got a screen like mine above, you are set up. Remember you will also want to use Notepad++ in the subsequent steps of this long set-up process.
Step Three: Get Notepad++
Step Four: Open Notepad++
Perhaps you have a shortcut on your desktop now. Otherwise, open Start > All Programs > Notepad++.
Step Five: Write Your First Program (you’re now walking at a brisker pace of 4-5 mph)
Type in the following into this new document:
#!usr/bin/perl print "Hello, World!\n";
Step Six: Save Your First Program
Go to File > Save As… and save the document as ‘first-program’. Select the Perl source file format from the drop down menu. There is a long list of programming language file formats. You want the one that says .pl/.pm/.plx. Make sure to select an easy location for the file so that you can find it again quickly. On the Desktop is a good option. Otherwise you may wish to create a new folder in which to store your program. Mine is called ‘PERL’ and is kept on my Desktop, as shown earlier.
When you first try to run your program you will need to know the Path information (location information) for your program, even if you don’t end up typing in that whole file path into the command prompt.
The actual Path should look something like this in its full form:
After you have successfully saved this first-program(.pl) somewhere, go ahead and try to run it:
Step Seven: Run Your first-program.pl in Windows Command Prompt (6 mph)
1. Windows-key + R
2. ‘cmd’ and ENTER
3. ‘cd Desktop’ or ‘cd Desktop\PERL’ or ‘cd Desktop\YourNiftyPerlFolder’ and ENTER
4. ‘perl first-program.pl’ and ENTER
5. Read the results which have been printed out for you: Hello, World!
Step Eight: Copy, Modify, and then Run a second and then a third program (8 mph)
Navigate in Windows Explorer to your PERL area (whether that’s on the desktop or in a Perl folder that you’ve made for yourself). Select your first-program.pl and right-click it. Select ‘Copy’ and then right-click again in that same space/location and select ‘Paste’. You now have a copy of your first-program. Rename it ‘second-program.pl’. Open it using Notepad++. You may have to right-click and select ‘Open With’ and then choose Notepad++ as the application to use if it doesn’t automatically open using Notepad++. You may now modify that program.
Please add the following line (which I’ve written in red text) into your program (add your own flair too if you wish):
#!usr/bin/perl print "Hello, World!\n"; print "INSERT ANY MESSAGE HERE\n";
Next, save that file and run it again using Run > cmd > ‘cd’ to proper directory > ‘perl second-program.pl’. See the results and notice the differences.
Next, make another copy of the second-program, paste it in the same location, and rename it ‘third-program.pl’. Carefully type the following text into your third-program — make sure not to repeat the first (“shebang”) line:
#!usr/bin/perl #never forget the first line above #this is a comment; it will not be 'printed' or seen by users who 'run' your program #it is good practice to note to yourself (and others who may see your code) what you are doing use strict; #this makes perl focus more on proper programming syntax use warnings; #this makes perl tell you when your code is problematic use 5.014; #this makes perl use the newer features of perl -- you could also type 'use 5.016' or higher if you have it use utf8; #this allows perl to display international/special characters properly print "More messages like these!\n"; print "It's good to practice what you've learned.\n"; say "Why not use some newer features of Perl, like 'say'?";
This is the third-program. Significantly longer, but also with lots of good information encoded into it using the “#comments”. You should employ #comments to help yourself learn too, by the way. The output for this third-program should look like this in the Command Prompt:
More messages like these! It's good to practice what you've learned. Why not use some newer features of Perl, like 'say'?
Step Nine: Write and Run a “Cleaned-up” Fourth Program with Spiffy New Features (9 mph)
Create a new Notepad++ document with the name fourth-program.pl. Save it in your (now) usual place. Include the following code in it:
#!usr/bin/perl use strict; use warnings; use 5.014; use utf8; print "\n" , "#" x 9 , "\n"; say "Hi there!"; say "%" x 9; say "\n"; print "What is 2 plus 2?\n"; say "Why it's" , ' ' , 2+2 , ' ' , "of course!"; print "OK! Cya!\n";
That program looks a bit spiffier now, doesn’t it?
Ok, so what’s going on here?
We’ve got the multiplication of a string, the ‘printing’ and ‘saying’ of many things, the insertion of some extra spaces with ‘ ‘, extra blank lines, and some math, of course. 2 + 2 = 4. Easy, right?
If it were easy, I wouldn’t feel it necessary to go to all this trouble to explain it…
Next I will talk about strings, integers, floating point numbers, math, spaces, modifying strings, etc…
Then I will get into the 5th Program arena of Scalars, although we may need to dip into the Linux pool a bit first — just for fun!
(Be warned, though: this may take a while… Last update: March 16, 2013.)
The following topics/questions will be clarified and expanded upon shortly…
- ActivePerl or Strawberry Perl
- Notepad / Padre IDE / Notepad++
- File Naming Conventions
- What If I Have Windows 8?
- Why Would I Want to Use Linux?
- Popular Linux Distributions: Ubuntu, Linux Mint, Debian, Fedora, Etc.
- How to Try Out a Distribution: VirtualBox
- Linux Academy (& other guides to Linux)
- Text Editors (Gedit, etc.) / Vim / Emacs
- PerlBrew (optional)
- Using the Terminal (cd)
- Checking Your Perl Version
- File Paths, Naming Conventions, Where to Save
What’s next for this tutorial?
- Explain why someone might want to use Linux instead of or in addition to Windows
- Explain and give details of what else can be done simply using Perl: Math, Scalar Values, Arrays, Hashes, etc.
- Explain how I am going about learning Perl using the book Learning Perl
- Create and explain how to create a time-of-day-based greetings program
- Upload this program to the web and modify it to run it using user IP-addresses and time zones