[Perl Series II-practice] 1. Number of lines for Perl to read code

Source: Internet
Author: User
Preface

Sometimes there are these requirements:

1. count the number of rows of a file

2. Calculate the number of valid lines of a source code. Exclude empty and comment rows

3. Unify the quantity and number of files in a directory

Many languages can achieve these needs, but using Perl should be a good choice.


Number of lines reading files

It's easy to use Perl to read the total number of lines in a file (both empty lines and comments)

open(FILE ,<>); my $lines_counter = 0;  while(<>){$lines_counter += 1; }print "lines:  $lines_counter\n";  

Of course, if it is simpler in Linux, you just need to input the following command:

wc -l filename



Number of lines of source code (number of spaces)

Count the number of lines of code in a source code file: input parameter: file path return: arrays of three elements; Total lines, empty lines, and comment lines respectively. Note: currently, this code is basically used in Java, C, and C ++, because the comments here process the following three types:

1. // line comment

2. Block comment

/*

*

*/

3. File comment

/**

*

*/

Complete code:



Source code line statistics Tool (Perl Version)

1.Code line counter

This is a paid software,
There are also free versions, but there are limits that only 10 files can be counted at a time.

:

Http://codelinecounter.bistonesoft.com/clcperl.htm

Download and install

The running interface is as follows:


2.Cloc

This should be a lot of tools.

It can count the number of various types of files in a path, the total number of spaces, code, and comments. It can also collect statistics on the status of files in a compressed package.

Project Introduction path:

Http://cloc.sourceforge.net/index.html#Basic_Use


Download path:

Http://sourceforge.net/projects/cloc/files/cloc/v1.60/

The Perl source code and the packaged EXE file are provided here.

Download the. exe file as a tool. The latest file name is cloc-1.60.exe

In command line mode, enter:

Cloc-1.60.exe file path or package name

The output result is similar:


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.