Program in perl to compute the number of lines in a file?

1 answer

Answer

1077932

2026-04-15 01:00

+ Follow

You can compute the number of lines in a file using Perl with the following code snippet:

<code class="language-perl">my $filename = 'your_file.txt';  # Replace with your file name

open(my $fh, '<', $filename) or die "Could not open file '$filename' $!"; my $line_count = 0; $line_count++ while <$fh>; close($fh); print "Number of lines: $line_count\n";

</code>

This script opens the specified file, reads through it line by line, increments the line count, and prints the total number of lines.

ReportLike(0ShareFavorite

Copyright © 2026 eLLeNow.com All Rights Reserved.