Learn how whitespace and comments are handled in Perl.
- [Voiceover] Comments and whitespace are an important part of any source code file. This is even more important in a scripting language where the code tends to be more fluid. Here's a working copy of countlines.pl from Chapter 3 of the exercise files. And in this file, you'll notice that there is whitespace, there's a blank line here, there's a blank line there, there's a space here, but there's also all these new lines. All the new lines also count as whitespace. In fact, in Perl, whitespace can be any of space characters, tab characters, new lines, carriage returns, and a few other less common characters.
Whitespace is mostly ignored in Perl. For example, I'll go ahead an run this, you'll see the result, it's counting the lines in this file, we've seen this before. I can go ahead and I can take out all kinds of whitespace here, like that, and I could do this all along here. I'm gonna go ahead and just finish taking out all the whitespace in this main function, of course not within the string, and there that bracket is now at the end of that line, of course we're scrolling horizontally here, but you can see that that entire function is now all on one line, and if I go ahead and save this and run it, you see it still has exactly the same result.
Now you'll notice that there's a couple of places where I've left the whitespace. I left the whitespace here between the word "sub" and "main" because otherwise the interpreter wouldn't be able to distinguish between the keyword and the name of the function. If I take that out and we try to run that, now we have syntax errors all over the place because Perl no longer understands where the keyword ends and the token begins. So if I put that back in and save and run, now it's working again.
So in most cases, the whitespace is not necessary, but there's a few places where it is, and of course inside the string here, obviously if I were to remove the whitespace and save this, you see those spaces now are gone from the string, and likewise if I remove the whitespace at the end of that, now that variable is no longer distinct, and so the interpreter doesn't know how to interpolate it. Of course if I take those spaces out and I put in curly braces around the name of the variable, and now it knows that that is a variable, and so that's a technique for distinguishing a variable inside of an interpolated string where you don't have spaces around the variable name.
So in practice, I'm gonna go ahead and put this back, and go ahead and run, make sure that we're back here, in practice, whitespace is good, it makes the source code readable. That's the purpose of source code, is so that it's readable to a human, otherwise we would all just write machine code. So we want this to be readable by a human. Likewise, comments are useful, they help this to be readable by humans. I have a comment right here that says "entry point", and that tells us that "main" is the entry point for the program.
I mean, you could deduce that, you could look a the source code, you could see that main is called here, but the comment is helpful, and it makes it a little bit more obvious, and that's the purpose of comments, again, to make the source code more readable for a human. In Perl, a comment is introduced by a hash mark or a pound sign or whatever you call this tic-tac-toe looking guy, and the comment continues until the end of the line. So I could introduce a comment here, and I could say "assign filename", and now everything up to that hash mark is part of the code, so the whitespace of course is ignored, and then starting at the hash mark and all the way to the end of the line is the comment, which is also ignored by the compiler/interpreter, and so it's treated like whitespace.
Now it's a matter of style what sort of comments you include. I find a comment like this to be superfluous because it's really already obvious that this is an assignment, and so I don't include comments like that, but many people choose to, and that's fine and good and it certainly doesn't hurt anything, and perhaps would help someone who's very new to the Perl language or new to programming. So comments and whitespace are important parts of making your code readable by humans.
We'll talk a little more about code readability in the best practices chapter later in this course.
Watch to learn the details of the Perl syntax, from variables, conditionals, loops, and data structures to regular expressions, functions, and references. A quick-start guide is included for experienced developers who want to get up and running with Perl 5 fast, and the entire course is recommended for both new and experienced programmers alike. Later chapters cover file handling and reusing code with Perl modules, plus Perl best coding practices.
- Understanding Perl's general syntax and the anatomy of a Perl script
- Writing statements and expressions
- Creating assignments
- Working with variables and strings
- Using data types effectively
- Defining logical flow with conditionals and loops
- Using special variables
- Using Perl operators
- Performing simple Perl programming tasks with expressions
- Matching data
- Defining and calling functions
- Using references
- Handling files in the file I/O
- Using built-in functions
- Reusing code with modules
- Coding with Perl best practices
Skill Level Intermediate
Programming Foundations: Refactoring Codewith Simon Allardice1h 44m Intermediate
1. Setting Up
About Perl3m 36s
2. Quick Start
3. Basic Syntax
4. Values and Variables
7. Special Variables
9. Regular Expressions
11. References and Structures
12. File I/O
13. Built-In Functions
15. Best Practices
- Mark as unwatched
- Mark all as unwatched
Are you sure you want to mark all the videos in this course as unwatched?
Take notes with your new membership!
Type in the entry box, then click Enter to save your note.
1:30Press on any video thumbnail to jump immediately to the timecode shown.