From the course: Perl 5 Essential Training

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Counting lines in a file

Counting lines in a file - Perl Tutorial

From the course: Perl 5 Essential Training

Start my 1-month free trial

Counting lines in a file

- [Voiceover] Continuing with our quick start, we're going to examine a simple script that counts the lines in a file. Here, I have opened a working copy of countlines.pl from Chapter 2 of the Exercise Files. This also uses the file linesfile.txt, so if I just double click on this here in Komodo Edit, you'll notice that that's what the file looks like. It's got 50 lines of text, and they're numbered, and they just say, "This is a line of text." So I'll go ahead and close that, and what this script does, is it opens that file, and it counts the lines of text, and it tells you how many there are. So this is a rudimentary script, counts the lines in a small file. It demonstrates opening, reading, and closing a file. It reads the entire file into memory, so it'll only work on reasonably small files, and let's just take a look at how it works. So on line 8, we declare a variable, and that's right here. The my keyword declares the variable to be lexically scoped in the enclosing block or…

Contents