[comp.lang.perl] Help with perl program on PS/2

dittrich@milton.u.washington.edu (Dave Dittrich) (08/01/90)

First, let me admit that I am a new perl user (with a backgound in awk, c,
sed, and sh).  I am trying to learn from and enhance the code documenting
example provided by Larry Wall in response to a previous question (Reference
<DGA#'-*@rpi.edu> in comp.unix.questions).

I have included the perl program I am using (on an IBM PS/2, DOS 3.3, using
Ian Stewart's ms_sh 1.6.2).  A sample of the C program source I am running
the script on, and the results I am getting, are also included.

The problem appears as the trailing comment delimiter ("*/") being appended
to the final line of text on the output.

I am using ms_sh because of another problem I was encountering, that of
STDOUT not being re-directable to a file.  It appears that STDIN is not
handled properly during the DOS batch file kludge suggested to invoke perl
ala the "#!/usr/bin/perl" method.

Thanks for the help.

-------------------------Example perl script follows:------------------------

#!c:\usr\bin\perl

if ($ARGV[0] eq "") {
	print "usage: $0 file";
	exit 1;
}

if (open(IN, $ARGV[0])!=1) {
	die "can't open file $ARGV[0]";
}

$fnumber = 0;
while (<IN>) {
    if (m!^/\*! .. m!^\*/!) {			# for the lines of
						#   the comment
	s/^\*\*\s*//;				# discard leading cruft
	$heading = $1 if s/^\.(\w+)\s*//;	# possibly switch headings
	eval "\$X$heading .= \$_" if $heading;	# append to current heading
    } # if
    elsif ($XFUNCTION ne '') {			# found one of our comments
	$- = 0 if (++$fnumber > 1);		# page feed for all but first
	$XFUNCTION =~ s/\(\)//;
	write top;
	$XPURPOSE =~ s/\n/ /g;			# so filled lines work right
	@XFORTRAN = split(/\n/,$XFORTRAN);
	@XPARAMETERS = split(/\n/,$XPARAMETERS);
	write;
	reset 'X';				# null out all headings
	$heading = '';
    } # elsif
} # while

close(IN);
exit;

format top =
Function:

        @<<<<<<<<<<<<<<<<<<<<<<<<<<
       	$XFUNCTION

.

format STDOUT =
Purpose:

~~      ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
        $XPURPOSE

FORTRAN Usage:

~~      @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
	shift(@XFORTRAN)

Parameters:

~~      @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
	shift(@XPARAMETERS)

Returns:

~~      ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
	$XRETURNS

.

-------------------------Example source file follows:------------------------

/*
** .FUNCTION	initmcs()
** .PURPOSE	Initialize the global MCS II board base address, and
**		reset the MCS II board.  This function MUST be called
**		before any other MCS functions.
** .FORTRAN	INTERFACE TO SUBROUTINE INITMCS [C] (IBASE)
**		INTEGER*2 IBASE [VALUE]
**		END
**		...
**		EXTERNAL INITMCS
** .PARAMETERS
**		address, (int) value of base address of MCS II board.
**
*/
void initmcs(int address)
{
	mcs_set_base((uint_t) address);
	mcs_reset_board();
	mcs_counter_setup();

} /* initmcs() */

-------------------------Example output follows:-----------------------------

Function:

        initmcs

Purpose:

        Initialize the global MCS II board base address, and reset the MCS
        II board.  This function MUST be called before any other MCS
        functions.

FORTRAN Usage:

        INTERFACE TO SUBROUTINE INITMCS [C] (IBASE)
        INTEGER*2 IBASE [VALUE]
        END
        ...
        EXTERNAL INITMCS

Parameters:

        address, (int) value of base address of MCS II board.
        */

Returns:
-- 
Dave Dittrich		INTERNET: dittrich@u.washington.edu
(206) 685-2438		UUCP:	  ...uw-beaver!u.washington.edu!dittrich
Dept. of Chemistry, University of Washington

lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall) (08/03/90)

In article <5776@milton.u.washington.edu> dittrich@milton.u.washington.edu (Dave Dittrich) writes:
: The problem appears as the trailing comment delimiter ("*/") being appended
: to the final line of text on the output.

:     if (m!^/\*! .. m!^\*/!) {			# for the lines of
: 						#   the comment
: 	s/^\*\*\s*//;				# discard leading cruft
: 	$heading = $1 if s/^\.(\w+)\s*//;	# possibly switch headings
: 	eval "\$X$heading .= \$_" if $heading;	# append to current heading
:     } # if

Your problem is right in there.  The final */ line is processed as part
of the range, and because $heading is still set to PARAMETER from before,
the "*/" gets appended to $XPARAMETER, and the format merrily prints it out.

Throw in a

	s!^\*/!!;

and it'll get better.

Larry