Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

Re: Line-counts of perl programs/modules

by larsen (Parson)
on Apr 27, 2001 at 12:09 UTC ( #76015=note: print w/replies, xml ) Need Help??

in reply to Line-counts of perl programs/modules

Perhaps I've totally missed the point, but if you want to add line numbers to your program, you could include this snippet in every program you write.
sub lines_count { open IN, "< $0" || die "Can't open file (black magic?)\n"; while( <IN> ) { chomp; printf "%3d: %s\n", $., $_; } close IN; }
So, you could type:

perl -e lines_count > something_that_let_you_print. (Maybe) Useful on system where wc does not exist.

Update Sorry, I've said something wrong. From perlrun...

-e commandline
may be used to enter one line of script. If -e is given, Perl will not look for a script filename in the argument list. Multiple -e commands may be given to build up a multi-line script. Make sure to use semicolons where you would in a normal program.
So what you could write is:

perl -e 'while (<>) {chomp; printf "%3d: %s\n", $., $_; }' < some_script > something_that_let_you_print

Sorry :)

Replies are listed 'Best First'.
Re: Re: Line-counts of perl programs/modules
by snafu (Chaplain) on Apr 27, 2001 at 17:31 UTC
    What about the all too (perhaps not completely understood by me) easy

    print "Total lines in $0: ". __LINE__ ."\n";

    Could you just put that at the end of your script? That would give you your line count wouldn't it?

    $ perl My output from the script Total lines in 37
    Although I admit that the original question is a very good one and one that I am quite interested to see a more "official" answer to. I am still quite new to Perl and I do the wc route to getting my line counts for my scripts. Actually, I do something closer to:

    $ egrep -v "^#" | wc -l

    or something more informative...

    echo "$(egrep -v "^#" - $(egrep "#"" | bc

    Which would at least give me my actual lines of code minus my comments. I just finished a script that was 1/3 comments.

    - Jim

Re: Re: Line-counts of perl programs/modules
by knobunc (Pilgrim) on Apr 27, 2001 at 18:33 UTC

    I don't think he wants line numbers on the code, but rather a line count for the code. But... the following will put line numbers in front of each line:

    cat -n filename


Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://76015]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (3)
As of 2022-08-08 07:31 GMT
Find Nodes?
    Voting Booth?

    No recent polls found