CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Perl print header, then print plain text.

 



klowery
Novice

Jun 3, 2014, 7:00 AM

Post #1 of 18 (6607 views)
Perl print header, then print plain text. Can't Post

I am trying to write a program using a form and it's printing the HTML just fine, but then I want to print plain text "code style" for the output but I can not figure it out, instead it prints in html also.


Code
use strict; 
use warnings;

use CGI qw( :standard );
use YAPE::Regex::Explain;

print header,
start_html('Display Regex'),
start_form(-method => 'GET'),
textfield(-name => 'REGEX'),
submit(-value => 'Explain!'),
end_form,
end_html;

if (param) {

print header( -type => 'text/plain' );

my $re = param('REGEX');
print YAPE::Regex::Explain->new($re)->explain;

}



(This post was edited by klowery on Jun 3, 2014, 7:01 AM)


FishMonger
Veteran / Moderator

Jun 3, 2014, 7:30 AM

Post #2 of 18 (6590 views)
Re: [klowery] Perl print header, then print plain text. [In reply to] Can't Post

You already sent the html header, so you can't send another one and expect it to override the first.

Try adding an else clause.


Code
use strict;  
use warnings;

use CGI qw( :standard );
use YAPE::Regex::Explain;

if (param) {

print header( -type => 'text/plain' );

my $re = param('REGEX');
print YAPE::Regex::Explain->new($re)->explain;

}
else {
print header,
start_html('Display Regex'),
start_form(-method => 'GET'),
textfield(-name => 'REGEX'),
submit(-value => 'Explain!'),
end_form,
end_html;
}



klowery
Novice

Jun 3, 2014, 7:46 AM

Post #3 of 18 (6579 views)
Re: [FishMonger] Perl print header, then print plain text. [In reply to] Can't Post

That worked thanks alot! Is there any way that can print on the same page instead of outputting it to a new page?


FishMonger
Veteran / Moderator

Jun 3, 2014, 7:54 AM

Post #4 of 18 (6571 views)
Re: [klowery] Perl print header, then print plain text. [In reply to] Can't Post

That could be done with AJAX calls.

https://developer.mozilla.org/en-US/docs/AJAX/Getting_Started


klowery
Novice

Jun 3, 2014, 8:22 AM

Post #5 of 18 (6555 views)
Post deleted by klowery [In reply to]

 


FishMonger
Veteran / Moderator

Jun 3, 2014, 9:22 AM

Post #6 of 18 (6524 views)
Re: [klowery] Perl print header, then print plain text. [In reply to] Can't Post

The module doesn't provide a method to do that, but you could parse the returned data to remove that portion. Or, you could extend the module by adding your own method to achieve that goal.


klowery
Novice

Jun 3, 2014, 11:57 AM

Post #7 of 18 (6459 views)
Re: [FishMonger] Perl print header, then print plain text. [In reply to] Can't Post

Here is what I found and tried, but it prints nothing here, but if i change the onclick function to checkre(['re__re'], it prints the wrong output.


Code
use strict; 
use warnings;

use CGI;
use CGI::Ajax;
use YAPE::Regex::Explain;

my $cgi = new CGI;

my $pjx = new CGI::Ajax('checkre' => \&checkre);
print $pjx->build_html( $cgi, \&ajax_html);


sub checkre{
my $input = shift;
my $output = $cgi->param('re');
return YAPE::Regex::Explain->new($output)->explain;
}

sub header {

my $header = qq~<HTML>
<HEAD>
</HEAD>
<BODY>~;
return $header;
}

sub ajax_html{

my $html_header = header();
my $html_footer = footer();

my $html_body = qq~<input type="text" name="re" size=55><input type=button onclick="checkre(['name__re'], ['rgx']);">
<div id="rgx"></div>~;

my $full_html = $html_header.$html_body.$html_footer;

return $full_html;
}

sub footer{

my $footer = qq~</BODY>
</HTML>~;
return $footer;

}



Zhris
Enthusiast

Jun 3, 2014, 12:12 PM

Post #8 of 18 (6449 views)
Re: [klowery] Perl print header, then print plain text. [In reply to] Can't Post

Hi,

The simplest thing to do would be to put your code in pre / code blocks, which will honour plain text styling i.e.:


Code
my $regex_explain = YAPE::Regex::Explain->new($re)->explain; 

print "<pre><code>$regex_explain</code></pre>\n";


Chris


(This post was edited by Zhris on Jun 3, 2014, 12:18 PM)


klowery
Novice

Jun 3, 2014, 12:15 PM

Post #9 of 18 (6445 views)
Re: [Zhris] Perl print header, then print plain text. [In reply to] Can't Post

Thanks alot, but my issue is with the AJAX call.


Zhris
Enthusiast

Jun 3, 2014, 12:18 PM

Post #10 of 18 (6440 views)
Re: [klowery] Perl print header, then print plain text. [In reply to] Can't Post

Understood, but the ajax call is unnecessary, my suggestion is an alternative.


(This post was edited by Zhris on Jun 3, 2014, 12:19 PM)


klowery
Novice

Jun 3, 2014, 12:25 PM

Post #11 of 18 (6429 views)
Re: [Zhris] Perl print header, then print plain text. [In reply to] Can't Post

Ok I understand, but I was trying to display the result on the same page as the input form.


Zhris
Enthusiast

Jun 3, 2014, 12:29 PM

Post #12 of 18 (6425 views)
Re: [klowery] Perl print header, then print plain text. [In reply to] Can't Post

Ahh apologies, I missed that. But still, you could use your original code, with the pre/code tags, which should display the form, then on submit the form and output. Untested:


Code
use strict;  
use warnings;
use CGI qw( :standard );
use YAPE::Regex::Explain;

print header,
start_html('Display Regex'),
start_form(-method => 'GET'),
textfield(-name => 'REGEX'),
submit(-value => 'Explain!'),
end_form;

if (param)
{
my $re = param('REGEX');
my $regex_explain = YAPE::Regex::Explain->new($re)->explain;
print "<pre><code>$regex_explain</code></pre>\n";
}

print end_html;


With regards to your ajax approach, i'm unfamiliar with using the module, but ['name__re'] should be ['re'].

Chris


(This post was edited by Zhris on Jun 3, 2014, 12:37 PM)


klowery
Novice

Jun 3, 2014, 12:40 PM

Post #13 of 18 (6409 views)
Re: [Zhris] Perl print header, then print plain text. [In reply to] Can't Post

Thanks Chris!


Zhris
Enthusiast

Jun 3, 2014, 12:43 PM

Post #14 of 18 (6406 views)
Re: [klowery] Perl print header, then print plain text. [In reply to] Can't Post

No problem, apologies once again for the confusion.


klowery
Novice

Jun 3, 2014, 12:48 PM

Post #15 of 18 (6400 views)
Re: [Zhris] Perl print header, then print plain text. [In reply to] Can't Post

Hmm, it seems to cut off part of the explanation part.

See

http://liveforfaith.com/news/re_explain.pl?REGEX=%28%3F%3C!\s%29-%28%3F!\s%29&.submit=Explain!


Zhris
Enthusiast

Jun 3, 2014, 12:54 PM

Post #16 of 18 (6395 views)
Re: [klowery] Perl print header, then print plain text. [In reply to] Can't Post

Ah yes, I can see where it cut off from viewing the source. Try encoding the HTML entities and see if that helps:


Code
use HTML::Entities; 

my $regex_explain = YAPE::Regex::Explain->new($re)->explain;
my $regex_explain_encoded = encode_entities($regex_explain);
print "<pre><code>$regex_explain_encoded</code></pre>\n";



klowery
Novice

Jun 3, 2014, 12:56 PM

Post #17 of 18 (6392 views)
Re: [Zhris] Perl print header, then print plain text. [In reply to] Can't Post

You have been a great help! Much appreciated!


Zhris
Enthusiast

Jun 3, 2014, 12:59 PM

Post #18 of 18 (6387 views)
Re: [klowery] Perl print header, then print plain text. [In reply to] Can't Post

No problem, I wrote something similar today. In case you find it useful, I used the following opening tags / styles to give it a border, a nicer font and properly handle word wrapping across all browsers.


Code
<pre style="border:1px solid black;white-space:pre-wrap;white-space:-moz-pre-wrap;white-space:-pre-wrap;white-space:-o-pre-wrap;word-wrap:break-word;"> 
<code style="font-family:DotumChe,'Lucida Console',Monaco,monospace;font-size:8pt;font-weight:bold;">

</code>
</pre>


Chris

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives