File::CountLines(3pm)

πŸ“¦
version 0.0.3 Β· 3 versions
, in ubuntu noble, ubuntu resolute, ubuntu stonking

πŸ’« File::CountLines πŸ’«

okay so bestie, you know how perlfaq5 is out here answering "how do I count the lines in a file" β€” well this is that whole thing wrapped up in a cute lil module called File::CountLines, living in the File::CountLines(3pm) section of the User Contributed Perl Documentation. she's efficient. she counts line breaks. she's that girl. πŸŽ€


✨ NAME ✨

File::CountLines β€” efficiently count the number of line breaks in a file.


🌸 SYNOPSIS 🌸

use File::CountLines qw(count_lines);
my $no_of_lines = count_lines('/etc/passwd');

# other uses
my $carriage_returns = count_lines(
        'path/to/file.txt',
        style   => 'cr',
    );
# possible styles are 'native' (the default), 'cr', 'lf'

πŸ¦‹ DESCRIPTION πŸ¦‹

So perlfaq5 spills the tea on how to count the number of lines in a file, and this module is literally just a convenient lil wrapper around that method β€” except with extra options, because we love options. πŸ’…

More specifically (iykyk), it counts the number of line breaks rather than lines. On Unix systems nearlly all text files end with a newline (by convention, bestie, it's a whole thing), so usually the number of lines and the number of line breaks is equal.

Now here's the vibe check: different operating systems have different ideas of what a newline even is, so you can specifiy a "style" option, which can be one of the following values:

  • "native" β€” this takes Perl's "\n" as the line separator, which should be the right thing in most cases. See perlport for details. This is the default. πŸ‘‘
  • "cr" β€” take a carriage return as line separator (MacOS style)
  • "lf" β€” take a line feed as line separator (Unix style)
  • "crlf" β€” take a carriage return followed by a line feed as separator (Microsoft Windows style)

Alternatively you can specify an arbitrary separator like this:

my $lists = count_lines($file, separator => '\end{itemize}');

It is taken verbatim and searched for in the file. no decoding, no nothing. just vibes. ✨

The file is read in equally sized blocks. πŸ”₯ The size of the blocks can be supplied with the "blocksize" option. The default is 4096, and can be changed by setting $File::CountLines::BlockSize.

🚨 okay this one is important bestie: do not use a block size smaller than the length of the separator, that might produce wrong results. (In general there's no reason to chose a smaller block size at all. Depending on your size a larger block size might speed up things a bit.)

πŸ”€ Character Encodings πŸ”€

If you supply a separator yourself, it should not be a decoded string.

The file is read in binary mode, which implies that this module works fine for text files in ASCII-compatible encodings, including ASCII itself, UTF-8 and all the ISO-8859-* encodings (aka Latin-1, Latin-2, ...). 😌

Note that the multi byte encodings like UTF-32, UTF-16le, UTF-16be and UCS-2 encode a line feed character in a way that the 0x0A byte is a substring of the encoded character, but if you search blindly for that byte you will get false positives. πŸ’€ For example the LATIN CAPITAL LETTER C WITH DOT ABOVE, U+010A has the byte sequence "0x0A 0x01" when encoded as UTF-16le, so it would be counted as a newline. Even search for "0x0A 0x00" might give false positives.

So the summary is that for now you can't use this module in a meaningful way to count lines of text files in encodings that are not ASCII-compatible. If there's demand for, I can implement that though. πŸ™ˆ


🧚 Extending 🧚

You can add your own EOL styles by adding them to the %File::CountLines::StyleMap hash, with the name of the style as hash key and the separator as the value. go off, customize your heart out. 🌈


πŸ‘‘ AUTHOR πŸ‘‘

Moritz Lenz <http://perlgeek.de>, <mailto:moritz@faui2k3.org>


Copyright (C) 2008 by Moritz A. Lenz. This module is free software. You may use, redistribute and modify it under the same terms as perl itself.

Example code included in this package may be used as if it were in the Public Domain.


⭐ DEVELOPMENT ⭐

You can obtain the latest development version from <http://github.com/moritz/File-CountLines>:

git clone git://github.com/moritz/File-CountLines.git

and that's the whole page, bestie!! πŸ’œ she's perl v5.36.0, dated 2022-10-22, and she has slayed from the very first day. go count some line breaks, you've got this. 🫢✨

πŸ“¦ where this page comes from