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

OK, here's the situation:

I created a perl script to automatically create some files on my web site. Like the good and meticulous webmaster that I am, I thought that I had better preview my files before they are added to my website (it's very much like the node-preview blessing we've got here in the monastery). So, I made a script (try.pl) that calls itself to do the preview or another (check.pl) to actually add the new file to my web site.

It works fine apart from an annoying glitch. Extra blank lines appear in the text area, where the html code is listed for further editing. Actually, all my carriage returns are duplicated. If I try to preview the document again, I get more blank lines and you can imaging what kind of an html-code I get if I try to preview the document a few more times.

I've been wondering whether this is due to a problem in my perl script or something else. Maybe you, brothers, could help. Here's the code:

#!/usr/bin/perl -- print "Content-Type: text/html\n\n"; $center = "center.htm"; # html template # taken from formmail %Config = ('dir','','fil','','ttl','','alx',''); read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'}); @pairs = split(/&/, $buffer); foreach $pair (@pairs) { local($name, $value) = split(/=/, $pair); $name =~ tr/+/ /; $name =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg; $name =~ tr/\0//d; $value =~ tr/+/ /; $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg; $value =~ tr/\0//d; $value =~ s/\n//eg; if (defined($Config{$name})) { $Config{$name} = $value; } } $dir = $Config{dir}; $fil = $Config{fil}; $ttl = $Config{ttl}; $alx = $Config{alx}; $fl = join '', "../ujo/", $dir, "/", $fil, ".txt"; open CENTER, $center or die "Cannot open $center for read :$!"; while (<CENTER>) { if (/text begin/) {$ate = 1;} if (/text end/) {$ate = 0;} if (/\<\/body\>/) {&printform;} if ($ate) {&printate;} else {print;} } sub printate{ print "<!--text begin-->\n"; print $alx; } sub printform{ print "<p>&nbsp;</p><center>\n"; print <<POINT; <table border="0" cellpadding="0" cellspacing="0" styl +e="border-collapse: collapse" bordercolor="#111111" id="AutoNumber1"> <FORM ACTION="../cgi-bin/try.pl" METHOD=POST> <tr> <td valign="top">Directory:</td> <td>&nbsp;</td> POINT print " <td valign='top'><INPUT NAME='dir' SIZE=50 +value='$dir'></td>\n"; print <<POINT; </tr> <tr> <td valign="top">File:</td> <td>&nbsp;</td> POINT print " <td valign='top'><INPUT NAME='fil' SIZE=50 +value='$fil'><p></td>\n"; print <<POINT; </tr> <tr> <td valign="top">Title:</td> <td>&nbsp;</td> POINT print " <td valign='top'><INPUT NAME='ttl' SIZE=50 +value='$ttl'><p></td>\n"; print <<POINT; </tr> <tr> <td valign="top">Text:</td> <td>&nbsp;</td> POINT print " <td valign='top'><TEXTAREA NAME='alx' ROWS= +15 COLS=50>$alx</TEXTAREA><br>\n"; print " <INPUT TYPE=SUBMIT VALUE='Preview'>&nbsp;&n +bsp;&nbsp;<INPUT TYPE=RESET VALUE='Reset'\n"; print " <input type=hidden name='sort' value='order +:=env_dir,fil,ttl,alx'></form>\n"; print " <FORM ACTION='../cgi-bin/check.pl' METHOD=POS +T>\n"; print " <INPUT TYPE=HIDDEN NAME='dir' VALUE='$dir'>\n +"; print " <INPUT TYPE=HIDDEN NAME='fil' VALUE='$fil'>\n +"; print " <INPUT TYPE=HIDDEN NAME='ttl' VALUE='$ttl'>\n +"; print " <INPUT TYPE=HIDDEN NAME='alx' VALUE='$alx'>\n +"; print " &nbsp;&nbsp;&nbsp;<INPUT TYPE=SUBMIT VALUE= +'Submit'>"; print " <input type=hidden name='sort' value='order +:=env_dir,fil,ttl,alx'>"; print "</td></tr></table></center>\n" }

Janitored by Arunbear - added readmore tags, as per Monastery guidelines

Replies are listed 'Best First'.
Re: Extra blank lines in script output
by JediWizard (Deacon) on Nov 08, 2004 at 15:35 UTC

    Blessed art thou that use CGI;.

    May the Force be with you
Re: Extra blank lines in script output
by Mutant (Priest) on Nov 08, 2004 at 15:42 UTC
    One possible solution is to remove newlines when you're reading in the config:
    if (defined($Config{$name})) { $Config{$name} = chomp $value; }
    But that's really only a band-aid fix. As someone's already said, you'll have a lot less problems if you use CGI. Once you've got the hang of that, you might also want to look at HTML::Template, or one of the many other templating modules.
Re: Extra blank lines in script output
by talexb (Chancellor) on Nov 08, 2004 at 15:36 UTC

    Is there a chance that it's a CR/LF (carriage return/line feed) issue between platforms? Are you working on a Windows box but running on a Unix/Linux server?

    Alex / talexb / Toronto

    "Groklaw is the open-source mentality applied to legal research" ~ Linus Torvalds

      This is true but I do not upload a file onto the server. I simply cut and paste the html-code into the text area which is then processed by the perl script.
Re: Extra blank lines in script output
by steves (Curate) on Nov 08, 2004 at 17:56 UTC

    I think this could have something to do with the way you're filtering the POSTed data. The TEXTAREA tag will transmit lines with line breaks intact as CR/LF pairs. I see you stripping \n. If the server is UNIX, that would remove the LF characters but leave the CR characters. You might try also stripping \r.

Re: Extra blank lines in script output
by TedPride (Priest) on Nov 09, 2004 at 09:21 UTC
    It's line feeds for sure, I've run into this myself.