in reply to Keep file formatted

This is the problem: $text = join(' ',@LINES); That jams all your text together in one long line, so when it comes out it looks like a mess. How about this:
open (FILE,"$FORM{'TextFile'}"); @LINES = <FILE>; close (FILE); chomp(@LINES); foreach $line (@LINES) { foreach $variable (@variables) { if ($FORM{$variable} eq "") { print "incomplete!\n"; exit; } $line =~ s/<!--$variable-->/<STRONG>$FORM{$variable}<\/STRONG>/g; } $line =~ s/A\/an <STRONG>(a|e|i|o|u)/An <STRONG>$1/g; $line =~ s/a\/an <STRONG>(a|e|i|o|u)/an <STRONG>$1/g; $line =~ s/A\/an/A/g; $line =~ s/a\/an/a/g; print "$line\n"; }

Replies are listed 'Best First'.
Re: Re: Keep file formatted
by tommyw (Hermit) on Aug 08, 2002 at 11:11 UTC

    And, of course, you could move the check on the @variables outside the processing loop. Then you'll get an error, and no output, rather than half the output and an error. Also, the filename really, really ought to be validated in someway, in case somebody submits (for example) "> /etc/passwd", which would promptly attempt to erase your password file (I'm only going to force the file to be opened for reading, which still isn't adequate, but it's an improvement).

    foreach $variable (@variables) { die "Incomplete $variable\n" if $FORM{$variable} eq ""; } open FILE, "< $FORM{'TextFile'}" or die "Cannot open TextFile $FORM{'TextFile'}"; foreach $line (<FILE>) { foreach $variable (@variables) { $line =~ s/<!--$variable-->/<STRONG>$FORM{$variable}<\/STRONG>/g; } $line =~ s/A\/an <STRONG>(a|e|i|o|u)/An <STRONG>$1/g; $line =~ s/a\/an <STRONG>(a|e|i|o|u)/an <STRONG>$1/g; $line =~ s/A\/an/A/g; $line =~ s/a\/an/a/g; print "$line\n"; } close FILE;

    --
    Tommy
    Too stupid to live.
    Too stubborn to die.