Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I'm working on a script to parse a Rational Rose design file (.cat/.mdl/.sub) and this pretty much does the job for me, However I had to resort to using a temporary file-handle for the "pre-processing", because when I read a line in from the original file, I may split it into three separate new lines. Here's an example of what the script does to the input (basically it pretty-prints the style so that the braces show up one per line, which makes other scripts reading this format easier to write)

BEFORE

(object (name1 val1 name2 val2))
AFTER
( object ( name1 val1 name2 val2 ) )
My question is: Is there a way to do this without resorting to a secondary file? I have another script that will use this code recursively against a whole baseline of design files, and using temporary files may make it too slow.

The Script:

$tempfile = "jojowashere.jbc"; if ($#ARGV < 0) { print "\nUSAGE: $0 filename.cat"; exit(0); } open (fp,$ARGV[0]) || die ("Can't open $ARGV[0]! Exiting."); open (tempfp,"> $tempfile") || die ("Can't create $tempfile! Exiting." +); while(<fp>) { if (m/(^.*)(".+")(.*)/) { $a = $1; $b = $2; $c = $3; $a =~ s/\(/\n\(\n/g; $a =~ s/\)/\n\)\n/g; $c =~ s/\(/\n\(\n/g; $c =~ s/\)/\n\)\n/g; $_ = $a . $b . $c; } else { s/\(/\n\(\n/g; s/\)/\n\)\n/g; } print tempfp "\n$_"; } close fp; close tempfp; $num_indents = 0; open (fp,$tempfile) || die ("Can't open $tempfile!"); while (<fp>) { s/^\s+//; s/\s+$//; chomp; if (length) { if (m/^\)$/) { --$num_indents; } $spacing = ""; for ($j=0;$j<$num_indents;$j++) { $spacing .= " " }; print "\n$spacing$_"; if (m/^\($/) { ++$num_indents; } } } close fp; unlink $tempfile;
Thanx, jojo

Replies are listed 'Best First'.
Re: Parsing a file, splitting current line into multiple ones, to be parsed separately
by John M. Dlugosz (Monsignor) on Jun 14, 2001 at 02:54 UTC
    You could try using a grammar parsing module instead of a line-oriented approach. Then you don't need to rearrange the parens.
      Hmm that definitely makes sense, I'll look into it. Are there any good grammar parser modules you recommend?

      thx, jojo

Re: Parsing a file, splitting current line into multiple ones, to be parsed separately
by JojoLinkyBob (Scribe) on Jun 14, 2001 at 02:37 UTC
    Doh! I wasn't logged in when I posted this, ergo anonymous

    :( .. Well if amounts to anything, it's from me (JojoLinkyBob)

    apologies, Desert coder