PDA

View Full Version : perl guru


gse
10-21-2001, 11:39 AM
very simple question I suppose for someone who knows cgi.
Basic form to email cgi script
having trouble with <textarea> output.
the html form contains:
<textarea name="message" wrap="true">..
perl script gets value from this textbox and outputs to email. The problem is how the hell can I make the script wrap the "message" before it outputs "message" to email. I get one long line in this field when it emails back to me. Any code I need to add to the script to make it "wrap" this field before outputting to email?

thanx in advance

Groovy
10-21-2001, 11:54 AM
geezzz...
might be simple for a real guru but not for me!!! :(

run
10-21-2001, 12:07 PM
solution 1:
dont wrap, send in HTML.
html parser will do it.

solution 2:
wrap.
you need to know how long the line you want before wrapping it.

solution 3:
<textarea name="textfield" wrap="PHYSICAL"></textarea>

run
10-21-2001, 12:08 PM
not sure about third one but i think thats the easiest way

gse
10-21-2001, 12:13 PM
Originally posted by run:
<STRONG>solution 1:
dont wrap, send in HTML.
html parser will do it.

solution 2:
wrap.
you need to know how long the line you want before wrapping it.

solution 3:
&lt;textarea name="textfield" wrap="PHYSICAL"&gt;&lt;/textarea&gt;</STRONG>

solution 3 don't work - i tried 'physical" same thing.
the script processes it as text I suppose
I need solution 2, can you give more details?

run
10-21-2001, 12:20 PM
yeah i know one way just let me think if theres anything better than i know, cuz its kinda long.

Third Os
10-21-2001, 12:27 PM
I had the same problem while writing a message board. The text from the textarea box was just one big line.
After taking a closer look at how it was saved, I saw that you need to edit the linebreaks a bit:
Before you output it in your mail, replace all the chr(10) chars to &lt;br&gt; tags.
If you can translate it from coldfusion, use this:
"#Replace(mailoutput, chr(10), '&lt;br&gt;','ALL')#"
That did the trick for me....

run
10-21-2001, 12:31 PM
third os way might work only if you send in html.

i wrote a sub for you to convert the text. Im pretty positive theres an easier way to do it but at this moment i dont know.


sub wrap{
my ($txt, $len) = @_;
my ($w, $wrap, $thisLine);
foreach $w (split(/ /, $txt)){
if ($thisLine&gt;$len){ $wrap .= "\n$w"; $thisLine = 0; }
else { $wrap .= "$w "; $thisLine += length($w); }
}
return $wrap;
}

run
10-21-2001, 12:34 PM
oh by the way

$text = wrap($text, $preferred_length);

:)

gse
10-21-2001, 12:38 PM
brb, lol

Ok, by looking at the script, I think this subroutine splits up the lines:
sub decode_vars
{
$i=0;
read(STDIN,$temp,$ENV{'CONTENT_LENGTH'});
@pairs=split(/&/,$temp);
foreach $item(@pairs) {
($key,$content)=split(/=/,$item,2);
$content=~tr/+/ /;
$content=~s/%(..)/pack("c",hex($1))/ge;
$content=~s/\t/ /g;
$fields{$key}=$content;
if ($key eq "data_order") {
$content=~s/\012//g;
$content=~s/\015//g;
$content=~s/ //g;
$content=~s/ //g;
@sortlist=split(/,/,$content);
}

anything I need to add here?

run
10-21-2001, 12:45 PM
sub wrap{
my ($txt, $len) = @_;
my ($w, $wrap, $thisLine);
foreach $w (split(/ /, $txt)){
$thisLine += length($w);
if ($thisLine=&gt;$len){ $wrap .= "\n$w"; $thisLine = 0; }
else { $wrap .= "$w "; }
}
return $wrap;
}



this one is improved a bit.

for parisng input better use ReadParse() that comes with cgi-lib.pl.

gse
10-21-2001, 01:05 PM
thanks Run!

salsbury
10-22-2001, 11:20 AM
here's how it is recommended in the perl manpage:

format =
^&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt ;&lt; ~~
$_

.

$/ = '';
while (&lt;&gt; ) {
s/\s*\n\s*/ /g;
write FILEHANDLE;
}

(FILEHANDLE being the filehandle to your mail process) perl format stuff once you get used to it is pretty damn spiff.