Solved

perl/cgi problem: wrap does not recognize new line or carriage return

Posted on 2010-11-08
14
375 Views
Last Modified: 2013-12-25
Hello:

I previously asked for help for a text wrap code and received great help from you folks on getting the text to wrap. However now, the code is now not recognizing when you hit enter from the keyboard (carriage return). Here is the code:

<code>
$text = $message;
@lines = split(/[\r\n]/, $message);
my @lines2;
s/(.{30})\s+/$1\n/g, push(@lines2, split(/\n/)) foreach @lines;
$lCnt=$#lines2+1;



$container = "/opt/httpd/root-wwwin/htdocs".$cardToUse;
$font = "/opt/httpd/root-wwwin/htdocs/tech/performance-culture/rewards/ecards-master/templ/verdana.ttf";


$image = GD::Image->newFromJpeg($container);

$lineStart=80;
$lineHeight=24;
$txtColor = $image->colorResolve(0,0,0);

for $i (0..$lCnt){
      $xPos = 390;
      $yPos = $lineStart + ($lineHeight*$i);
      @bounds = $image->stringTTF($txtColor,$font,13,0,$xPos,$yPos,$lines2[$i]);
      }
</code>

Thanks in advance for your help.

Regards,
seeker7806
0
Comment
Question by:seeker7806
  • 8
  • 6
14 Comments
 
LVL 16

Expert Comment

by:jmatix
ID: 34093912
Do you mean $message is input by user by typing in on keyboard and the script is not wrapping at the carriange returns entered by the user?
0
 

Author Comment

by:seeker7806
ID: 34094412
yes. When the enter key is pressed in the text box the carriage return is not reflected on the image via the perl/cgi script although the perl/cgi script automatically wraps...

Thanks jmatix!
0
 
LVL 16

Expert Comment

by:jmatix
ID: 34094682
When I run the script standalone it is preserving the line breaks. It could be your server side script which processes the user input that strips all carriage returns.
0
 

Author Comment

by:seeker7806
ID: 34095256
Thanks jmatix.

Actually the code above is the server side perl/cgi script. I am thinking that maybe my front end javascript is the problem?

I have a question pending on EE for the font end javascript.

It is wierd, because before, the previous code recognized carriage returns but not the wrap, and now it recognizes the wrap (via line splitting) but not the carriage return.

Regards,
seeker7806
0
 

Author Comment

by:seeker7806
ID: 34103166
There is an update. The perl cgi script appears to recognize hitting the return/enter button as one line break, but will not recognize hitting the return/enter button a second time for making a paragraph.

This is for both IE and Firefox.

On the front end regarding the javascript, hitting the carriage return works, but not in IE. In IE if you hit the carriage return, there are no line breaks or paragraphs in the preview image, but there are both line breaks and paragraphs on the image in Firefox.

In both cases however, when you hit the submit button, the result in the card being sent is just a line break but no paragraphs (when hitting the enter/return/carriage return button).
0
 
LVL 16

Expert Comment

by:jmatix
ID: 34103538
OK. Now I get it. That is how split() function in Perl behaves. To change that behavior modify the split() as below:

@lines = split(/[\r\n]+?/, $message);
0
 

Author Comment

by:seeker7806
ID: 34104092
Thanks jmatix.

This is still producing a <br> but does not recognize two carriage returns:

<code>
$text = $message;
@lines = split(/[\r\n]+?/, $message);
my @lines2;
s/(.{30})\s+/$1\n/g, push(@lines2, split(/\n/)) foreach @lines;
$lCnt=$#lines2+1;

</code>
Regards,
seeker7806
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 16

Expert Comment

by:jmatix
ID: 34104193
Can you post the complete code as it is?
0
 

Author Comment

by:seeker7806
ID: 34104237
<code>
#!/usr/local/bin/perl5

use CGI':standard';
use CGI::Carp qw/fatalsToBrowser/;
use GD;
use DBI;
use CommonSubs;
use arrData;
use dbInfo;






$q = new CGI;



$recId = time();

$userId = $ENV{'REMOTE_USER'};
$userInfo = &CommonSubs::DirInfo($userId);


$from_sender = $q->param('from');
$to_recip = $q->param('to');
$cc_manager = $q->param('cc_manager');
$bcc = "xxxxxx\@xxxx.com";
$card = $q->param('card');
$message = $q->param('message');



my %availableCards = ("card1"            =>            "/tech/performance-culture/rewards/ecards-master/images/large_card_1.jpg",
                                                       "card2"            =>            "/tech/performance-culture/rewards/ecards-master/images/large_card_2.jpg",
                                                       "card3"            =>            "/tech/performance-culture/rewards/ecards-master/images/large_card_3.jpg",
                                                       "card4"            =>            "/tech/performance-culture/rewards/ecards-master/images/large_card_4.jpg",
                                                       "card5"            =>            "/tech/performance-culture/rewards/ecards-master/images/large_card_5.jpg",
                                                       "card6"            =>            "/tech/performance-culture/rewards/ecards-master/images/large_card_6.jpg",
                                                       "card7"            =>            "/tech/performance-culture/rewards/ecards-master/images/large_card_7.jpg",
                                                       "card8"            =>            "/tech/performance-culture/rewards/ecards-master/images/large_card_8.jpg",
                                                       "card9"            =>            "/tech/performance-culture/rewards/ecards-master/images/large_card_9.jpg");

my $cardToUse = $availableCards{$card};
my $emailContent = "";




$text = $message;
@lines = split(/[\r\n]+?/, $message);
my @lines2;
s/(.{30})\s+/$1\n/g, push(@lines2, split(/\n/)) foreach @lines;
$lCnt=$#lines2+1;



$container = "/opt/httpd/root-wwwin/htdocs".$cardToUse;
$font = "/opt/httpd/root-wwwin/htdocs/tech/performance-culture/rewards/ecards-master/templ/verdana.ttf";


$image = GD::Image->newFromJpeg($container);

$lineStart=80;
$lineHeight=24;
$txtColor = $image->colorResolve(0,0,0);

for $i (0..$lCnt){
      $xPos = 390;
      $yPos = $lineStart + ($lineHeight*$i);
      @bounds = $image->stringTTF($txtColor,$font,13,0,$xPos,$yPos,$lines2[$i]);
      }

@months = qw(Jan Feb Mar Apr May Jun Jul Aug Sept Oct Nov Dec);
@weekDays = qw(Sun Mon Tue Wed Thur Fri Sat Sun);
($second, $minute, $hour, $dayOfMonth, $month, $yearOffset, $dayOfWeek, $dayOfYear, $daylightSavings) = localtime();
$year = 1900 + $yearOffset;
$dateandtime = "$hour:$minute:$second, $weekDays[$dayOfWeek] $months[$month] $dayOfMonth, $year";

my $user=$user;

$ts = time();
$imgFile = "ecard_".$user."_".$ts.".jpeg";
open(IMGFILE, ">/opt/httpd/root-wwwin/data-shared/tech/artg/cust-touch/videos/$imgFile");
binmode IMGFILE;
print IMGFILE $image->jpeg;
      
close (IMGFILE);

$img = "<img border=0 src=\"http:\/\/wwwin-dev.xxxxx.com/data-shared/tech/artg/cust-touch/videos/$imgFile\" />";

($host, $db, $dbUid, $dbPwd) = &dbInfo::setDbInfo();
$errStr = "";
$dbh = DBI->connect("DBI:Oracle:(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=$host)(PORT=1521))(CONNECT_DATA=(SID=$db)))","$dbUid","$dbPwd");
$sql = qq/insert into CDO_ECARD(REC_ID,USERID,TO_RECIP,FROM_SENDER,CC_MANAGER,CARD,MESSAGE,IMAGE,DATEANDTIME) values(?,?,?,?,?,?,?,?,?)/;
$sth      = $dbh->prepare($sql) or $errStr .="SQL Prepare Error: $DBI::errstr<br />";
$sth->execute($recId,$userId,$to_recip,$from_sender,$cc_manager,$card,$message,$imgFile,$dateandtime) or $errStr .= "Execute Error: $DBI::errstr<br />";
$sth->finish or $errStr .= "Finish Error: $DBI::errstr<br />";
$dbh->commit or $errStr .= "Commit Error: $DBI::errstr<br />";
$dbh->disconnect;

$emailContent = <<HTML;
<p>
$img
</p>
<p><div style=\"font-family:arial,sans-serif;font-size:16px;\"\>If you want to send an ecard click <a href=\"http:\/\/wwwin.xxxx.com\/tech\/performance-culture\/rewards\/ecards-master\/\"\>here\<\/a\><\/div\><\/p\>

HTML


&CommonSubs::sendEmail("$to_recip\@xxxx.com","$from_sender\@xxxx.com","You have received an e-card", "","$bcc",$emailContent);

print "Content-type: text/html\n\n";
print "<div style=\"font-weight:bold;font-size:18px;\"\>Your ecard has been sent<\/div\>";
print "<p>";
print "$img";
print "<p>";


</code>
0
 
LVL 16

Expert Comment

by:jmatix
ID: 34104730
Try changing the split as follows to inlcude the line breaks:

$text = $message;
@lines = split(/([\r\n])/, $message);
my @lines2;
s/(.{30})\s+/$1\n/g, push(@lines2, split(/(\n)/)) foreach @lines;
$lCnt=$#lines2+1;
0
 

Author Comment

by:seeker7806
ID: 34105026
Thanks jmatix.

I received the following error message:

Software error:

syntax error at /opt/httpd/root-wwwin/pcgi-bin/tech/performance-culture/ecards/addMsgToCard.cgi line 54, near ")) "
Execution of /opt/httpd/root-wwwin/pcgi-bin/tech/performance-culture/ecards/addMsgToCard.cgi aborted due to compilation errors.


line 54:

<code>
s/(.{30})\s+/$1\n/g, push@lines2, split(/(\n)/)) foreach @lines;
</code>
0
 
LVL 16

Accepted Solution

by:
jmatix earned 500 total points
ID: 34105210
Sorry, somehow I added an extra closing paren. Change that line to:

s/(.{30})\s+/$1\n/g, push@lines2, split(/(\n)/) foreach @lines;
0
 

Author Comment

by:seeker7806
ID: 34105404
Thanks jmatix:

Now the solution inputs too many carriage returns! But it seems we are making progress from before!

I am attaching a jpeg of the screen shot.

Regards,
seeker7806
ecard-test1.jpg
0
 

Author Comment

by:seeker7806
ID: 34114032
Hey jmatix:

I monkeyed around with the script and got it to approximate a proper carriage return. It's not perfect but I am hoping it will do for the stakeholder.

Thanks again
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this tutorial I will show you how to make a simple HTML bar chart with the usage of WhizBase, If you want more information about WhizBase please read my previous articles at http://www.experts-exchange.com/ARTH_5123186.html (http://www.experts-ex…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now