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

I'm processing a file by substituting a few words, and printing the text out as a file. Except for the 1st line, all the other lines in the processed file have a white space in front of them. I need to get rid of the spaces cause it's a perl file and recieve an internal server error. Any Idea's? Here's the code: -Lisa
#!/usr/local/bin/perl $InputFile = "input.txt"; $TextFile = "original_file.pl"; $Filetext= "modified_file.pl"; $CGIURL = "http://www.webiste/cgi-local/mainfile.pl"; print "Content-type: text/html\n\n"; read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'}); @pairs = split(/&/, $buffer); foreach $pair (@pairs){ ($name, $value) = split(/=/, $pair); $name =~ tr/+/ /; $name =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg; $value =~ tr/+/ /; $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg; if ($FORM{$name}) { $FORM{$name} = "$FORM{$name}, $value"; } else { $FORM{$name} = $value; push (@variables,$name); } } if ($FORM{'TextFile'}) { &SendFile; } if (-d $TextFile) { opendir(FILES,$TextFile); @tempfiles = readdir(FILES); closedir(FILES); foreach $file (@tempfiles) { if (-T "$TextFile/$file") { push (@files, $file); } } srand(); $files = @files; $file = @files[int(rand($files))]; $TextFile = $TextFile."/".$file; } open (FILE,"$TextFile"); @LINES = <FILE>; close (FILE); $text = join(' ',@LINES); $text =~ s/\n/ /g; $text =~ s/</\n</g; $text =~ s/>/>\n/g; @text = split('\n',$text); undef (@variables); foreach $line (@text) { if ($line =~ s/<!--(.*)-->/$1/) { push (@variables,$line); } } @sortedvariables = sort (@variables); open (FILE,"$InputFile"); @LINES = <FILE>; close (FILE); foreach $line (@LINES) { if ($line =~ /<!--InputWords-->/i) { print "<FORM METHOD=POST ACTION=\"$CGIURL\">\n"; print "<INPUT TYPE=HIDDEN NAME=TextFile "; print "VALUE=\"$TextFile\">\n"; print "<P><CENTER><TABLE>\n"; foreach $variable (@sortedvariables) { next if ($variable eq $lastvariable); print "<TR><TD ALIGN=RIGHT><P>$variable: </TD>"; print "<TD><INPUT TYPE=TEXT NAME=\"$variable\" SIZE=25>"; print "</TD></TR>\n"; $lastvariable = $variable; } print "</TABLE></P>\n"; print "<P><INPUT TYPE=SUBMIT VALUE=\"LOAD\">\n"; print "</CENTER></P></FORM>\n"; } else { print $line; } } exit; sub SendFile { open (FILE,"$FORM{'TextFile'}"); @LINES = <FILE>; close (FILE); $text = join(' ',@LINES); #$text =~ s/\n/ /g; $text =~ s/^\s+//; $text =~ s/\s+$//; foreach $variable (@variables) { if ($FORM{$variable} eq "") { print "incomplete!\n"; exit; } $text =~ s/<!--$variable-->/$FORM{$variable}/g; } #$text =~ s/A\/an <STRONG>(a|e|i|o|u)/An <STRONG>$1/g; #$text =~ s/a\/an <STRONG>(a|e|i|o|u)/an <STRONG>$1/g; #$text =~ s/A\/an/A/g; #$text =~ s/a\/an/a/g; open(OUT,">$Filetext") or die $!; print OUT "$text\n" or die $!; close(OUT) or die $!; exit; }

Replies are listed 'Best First'.
Re: space in front of lines
by grinder (Bishop) on Nov 05, 2002 at 08:55 UTC

    Change the lines like:

    @LINES = <FILE>;

    to:

    chomp( @LINES = <FILE> );

    The symptoms you describe is a classic case of not stripping out the newline from the lines you read in from the file. Other than that, you are making a lot of work for yourself in this code. By using the module CGI you could strip out a lot of code and make it more secure as well.


    print@_{sort keys %_},$/if%_=split//,'= & *a?b:e\f/h^h!j+n,o@o;r$s-t%t#u'
Re: space in front of lines
by davorg (Chancellor) on Nov 05, 2002 at 10:59 UTC
    $text = join(' ',@LINES);

    Maybe you shouldn't ask Perl to join the lines with spaces separating them then :)

    --
    <http://www.dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

      I tried all the suggestions.. including chomp does nothing, and removing the join line: $text = join(' ',@LINES); creates an empty file except for the number 70 which is strange. I think i may have to use a pattern matching operator to remove the spaces, but i'm not sure exactly where to insert it. -Lisa

        I think you misunderstood me. I'm not suggesting that you remove that line of code completely (obviously that's going to change the way your program works), just that if you ask Perl to join the lines separated by a space then you shouldn't be surprised if you get a space separating each lne :)

        If (as it seems) you want to join the lines, but not get the space separating them, then ask Perl to join them with an empty string as the separator.

        $text = join('', @LINES);
        --
        <http://www.dave.org.uk>

        "The first rule of Perl club is you do not talk about Perl club."
        -- Chip Salzenberg