Gowri Chandra Sekhar Barla, TLS, Chennai wrote:
From: John W. Krahn [mailto:[EMAIL PROTECTED]
$ echo "
/* requesting for the script
* more
* and more
* for printing line number */
Fun_script()
{
Int a; /* local parameter */
Int b; /* local parameter */
/* adding the
Two numbers */
Int c = a+b;
}/* function end */
" | perl -e'
my ( $count, $in_comment );
while ( <> ) {
my $temp = $_;
s!/\*.*?\*/!!g;
s!/\*.*?$!! and $in_comment++;
$in_comment and s!^.*?\*/!! and $in_comment--;
$in_comment and s/.*//;
s/^\s+//;
s/\s+$//;
print length() ? ++$count . " $temp" : $temp;
}
'
Final line of the script I am unable to understand
This my final question please..
print length() ? ++$count. " $temp" : $temp;
why length is required
while ( <> ) {
Reads the current line of the file and assigns it to the $_ variable.
my $temp = $_;
We store a copy of the current line into the $temp variable.
s!/\*.*?\*/!!g;
s!/\*.*?$!! and $in_comment++;
$in_comment and s!^.*?\*/!! and $in_comment--;
$in_comment and s/.*//;
s/^\s+//;
s/\s+$//;
Then we modify the contents of $_ by removing any comments and any
leading and trailing whitespace. If there is anything left then it is
probably a valid code line.
print length() ? ++$count . " $temp" : $temp;
Finally we test the length of $_ and if there is anything left print the
$count plus the actual unmodified line in $temp or if the modified line
is empty then just print the unmodified line itself.
John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/