My web page uses Charset UTF-8 to allow Chinese character input in a textarea form field. I want to test if the input contains a certain character. I've writtena test script to see how Perl is going to handle the Chinese input. It's not finding the match when there is a known match.
Here is my test form:
<!DOCTYPE html>
<head>
<meta charset="utf-8">
</head>
<body>
<form method="post" action="http://www.my_domain.com/cgi-bin/my_test_script.pl">
<textarea name="user_input" rows="" cols=""></textarea>
<input type="submit" name="submit" value="submit">
</form>
</body>
</html>
Here is my code:
#!/usr/bin/perl -T
use strict;
use warnings;
use CGI;
use CGI::Carp qw(warningsToBrowser fatalsToBrowser);
use utf8;
print "Content-type: text/html; charset=UTF-8\n\n";
print "<meta http-equiv='content-type' content='text/html;charset=UTF-8'>";
my $query = new CGI;
my $msg = $query->param('user_input');
chomp $msg;
my $msg_code = ord($msg);
print "<p> Message was: ".$msg."\n";
print "<p> Message Code is: ".$msg_code."\n";
my $char_from_code_point = "\N{U+89C6}";
my $char_from_code_point_reverse_code = ord($char_from_code_point);
print "<p> char_from_code_point= ".$char_from_code_point."\n";
print "<p> char_from_code_point_reverse_code = ".$char_from_code_point_reverse_code."\n";
if ($msg =~ m/$char_from_code_point/) {
print "<p>Matched!\n";
}
else {
print "<p> NOT matched\n";
}
And here is the output from submitting the correct character:
Message was: 视
Message Code is: 232
char_from_code_point= 视
char_from_code_point_reverse_code = 35270
NOT matched
Could someone please point out what I'm doing wrong?
Thank you.
Related
this is my problem I'm trying to read an HTML file(index.html) then search all links an put it on a second file named salida.html, I read this answer, I read this answer and I tried to do it, but it didn't work for me.
This is my perl code:
use strict;
use warnings;
use 5.010;
use Tie::File;
my $entrada='index.html';
my $salida='salida.html';
open(A,"<$entrada");
my #links;
foreach my $linea (<A>){
print "Renglon => $linea\n" if $linea =~ m/a href/;
#print $B $linea if $linea =~ m/a href/;
push #links, $linea if $linea =~ m/a href/;
}
tie my #resultado, 'Tie::File', 'salida.html' or die "Nelson";
for (#resultado) {
if ($_ =~ m/<main class="contenido">/){
foreach my $found (#links){
$_ .= '<br/>'.$found;
}
last;
}
}
close(A);
My Perl code runs without problems but in the for of my code I'm trying to write the links that I have in my variable $links in a specific part of my salida.html file:
<!DOCTYPE html>
<html lang="es-mx">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Resultados de la busqueda</title>
<link rel="stylesheet" href="style-salida.css">
</head>
<body>
<div class="contenedor">
<header class="header">
<h2>Resultados de la busqueda</h2>
</header>
*<main class="contenido">
</main>*
<footer class="footer">
<h4>
Gerardo Saucedo Arevalo - 15092087 - Topicos selectos de tecnologias web - Búsqueda de enlaces dentro de
una página web
</h4>
</footer>
</div>
</body>
</html>
But my code always add the lines at the end of the file, I ran this code once and it worked perfectly, but then I add some lines and when I tried to run one more time didn't work.
I restored my file at the moment when it worked but it does not work anymore.
What I'm doing wrong?
Always process HTML or XML with an appropriate parser and then implement your processing on the DOM. My solution uses HTML::TreeBuilder. As your question doesn't include the contents of index.html I have appended my own to the solution:
#!/usr/bin/perl
use warnings;
use strict;
use HTML::TreeBuilder;
# Extract links from <DATA>
my $root1 = HTML::TreeBuilder->new->parse_file(\*DATA)
or die "HTML: $!\n";
my #links = $root1->look_down(_tag => 'a');
# Process salida.html from STDIN
my $root2 = HTML::TreeBuilder->new;
$root2->ignore_unknown(0);
$root2->parse_file(\*STDIN)
or die "HTML: $!\n";
# insert links in correct section
if (my #nodes = $root2->look_down(class => 'contenido')) {
$nodes[0]->push_content(#links);
}
print $root2->as_HTML(undef, ' '), "\n";
# IMPORTANT: must delete manually
$root2->delete;
$root1->delete;
exit 0;
__DATA__
<!DOCTYPE html>
<html>
<head>
<title>test</title>
</head>
<body>
<div>
Link 1
Link 2
</div>
</body>
</html>
Test run:
$ perl dummy.pl <dummy.html
<!DOCTYPE html>
<html lang="es-mx">
...
<main class="contenido"> Link 1Link 2</main>
...
</html>
here is my html code
<html>
<title>Results</title>
<body><h1> Here are your results</h1>
<p>Please click the Button to see your result run by Ravi's team.</p>
<form action='index.pl' method='post'>
<input type='submit' value='submit'>
</form>
</body>
</html>
and index.pl is my perl and my subroutine is as follows.
sub my_result{
my $run;
my $dir="/kbio/sraja/BenzoExposedDataSet/database/Output";
my $parsebphtml = "/parse_bphtml.pl";
my $olgacsvfile = "/database/Output/sample.csv";
my #bp=<$dir/*.bp>;
$run ="perl $parsebphtml > $olgacsvfile";
# print "$com\n";
system($run)==0 or my_err("Could not run $run\n");
#printing the table
open(F,"$olgacsvfile") or my_err("Could not open the csv ($olgacsvfile) file");
print "<h2> Average Results </h2>";
print "<table border=1>";
while(my $line=<F>){
print "<tr>";
my #cells= split ',',$line;
foreach my $cell (#cells)
{
print "<td colspan=1>$cell</td>";
}
print "</tr>";
}
print "</table>";
}
So as you see, table is what i need to return to results.html
Any help would be really appreciable.
thanks .
Geet
I don't know how much work you want to do but, if you want to keep it simple give a try at the HTML::Template module. Here is a simple usage example.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset=utf-8>
<title>A random page</title>
</head>
<body>
<TMPL_VAR NAME=page_content>
</body>
</html>
My perl code contained something like this. Better yet, check the documentation at http://metacpan.org/pod/HTML::Template .
use HTML::Template;
sub my_result {
return $html_string;
}
my $master_template = HTML::Template->new(filename => "Path to html template file");
$master_template->param('page_content' => my_result());
Depending on how far you plan on going with this, I would recommend that you a more advanced templating system such as the one used by the mojolicious framework (http://mojolicio.us/perldoc/Mojo/Template).
Cheers,
MrMcKizzle
I want to do a textarea/textbox in perl. I have this tag but it is coming up as syntax error.
<textarea name="answer" rows="20" cols="70"></textarea>
I have no idea why it is coming up, my code is:
#!/usr/bin/perl
use strict;
use warnings;
use CGI;
use CGI::Carp qw(fatalsToBrowser);
use constant debug=>0;
print "Content-type: text/html\n\n";
# Program: assignment 3
# Author: Jay
# Date: 02/2014
my $cgi= CGI->new();
my $a=$cgi->param('action');
my $num1=$cgi->param('num1');
my $num2=$cgi->param('num2');
my $num3=$cgi->param('num3');
my $num4=$cgi->param('num4');
my $hard=$cgi->param('hardware');
my $soft=$cgi->param('software');
print "$a, $num1, $num2, $num3, $num4" if debug;
if ($a eq undef) {
print "
<!DOCTYPE html>
<html>
<head>
<title>Error Logging</title>
</head>
<body>
<h1>Error Log - IT Support</h1>
<form method=\"post\" action=/~it.jasonc/cgi-bin/assignment3.pl>
<input type=\"hidden\" name=\"action\" value=\"error\">
<table>
<tr><td>Site:</td><td><input type=\"text\" name=\"site\"></td></tr>
<tr><td>Type of Error:</td>
<td><select name=\"error\">
<option value=\"1\">Hardware</option>
<option value=\"2\">Software</option></select></td></tr>
<textarea name="answer" rows="20" cols="70"></textarea>
<tr><td colspan=\"2\"><input type=\"submit\" value=\"Submit Error\"></td></tr>
</table>
</form>
</body>
</html> "
}
if ($hard) {
print "Go to room 1";
}
if ($soft) {
print "Go to room 2";
}
I had another type as an example
<textarea type=\"text\" name=\"details\" value\rows="4" cols="50">
Please explain the error here!
</textarea>
Please help!!
Jay
If you quote a string using the double quote " character, you must escape all double quotes inside it. You have escaped some, but not all:
...
<option value=\"2\">Software</option></select></td></tr>
<textarea name="answer" rows="20" cols="70"></textarea>
# ^ ^ ^ ^ ^ ^
As you can see, \"2\" for example is escaped, but "answer" is not.
A better way to handle this string is to use a different quoting, such as using qq, which can take many different delimiters, according to your needs, for example qq##:
print qq#
<!DOCTYPE html>
....
You can also use a heredoc:
print <<EOF;
<!DOCTYPE html>
....
EOF
The line <textarea name="answer" rows="20" cols="70"></textarea> is inside a large double quoted string. If you put a double quote inside the string (a large multiline string) you have to escape it by putting a backslash before it. It's done everywhere in your code, except where your code breaks, where there are 6 unescaped double quotes.
As the following text is HTML and not perl, a syntax error is to be expected.
If you don't want to care too much about quoting double quotes (it's always easy to forget some), there is of course another way to do it (actually more than one).
You could for instance use qq{} instead of double quotes for perl strings. Then your code would work if quotes are escaped or not. Nothing special, it's perfectly standard perl.
Below some use exemple where I unescaped some random quote inside the HTML code (in production code I would probably unescape them all to avoid syntaxic nois).
Beside that the mere difference is the use of qq{ after print and replacing the double quote closing string by }
#!/usr/bin/perl
use strict;
use warnings;
use CGI;
use CGI::Carp qw(fatalsToBrowser);
use constant debug=>0;
print "Content-type: text/html\n\n";
# Program: assignment 3
# Author: Jay
# Date: 02/2014
my $cgi= CGI->new();
my $a=$cgi->param('action');
my $num1=$cgi->param('num1');
my $num2=$cgi->param('num2');
my $num3=$cgi->param('num3');
my $num4=$cgi->param('num4');
my $hard=$cgi->param('hardware');
my $soft=$cgi->param('software');
print "$a, $num1, $num2, $num3, $num4" if debug;
if ($a eq undef) {
print qq{
<!DOCTYPE html>
<html>
<head>
<title>Error Logging</title>
</head>
<body>
<h1>Error Log - IT Support</h1>
<form method="post" action=/~it.jasonc/cgi-bin/assignment3.pl>
<input type=\"hidden\" name=\"action\" value=\"error\">
<table>
<tr><td>Site:</td><td><input type=\"text\" name=\"site\"></td></tr>
<tr><td>Type of Error:</td>
<td><select name=\"error\">
<option value=\"1\">Hardware</option>
<option value=\"2\">Software</option></select></td></tr>
<textarea name="answer" rows="20" cols="70"></textarea>
<tr><td colspan=\"2\"><input type=\"submit\" value=\"Submit Error\"></td></tr>
</table>
</form>
</body>
</html>
}
}
if ($hard) {
print "Go to room 1";
}
if ($soft) {
print "Go to room 2";
}
I am fairly new to Perl.
I have a form that reads into a script.pl and does the validation check and etc.
How can I make it so once its done showing the validation, loops back to the home page after a few seconds automatically?
I tried using the following and it didn't work:
use strict;
use warnings;
my $url = "http://google.com";
print "Location: $url\n\n";
An Example of HTML for this would be: <META HTTP-EQUIV="REFRESH" CONTENT="10;URL=index.htm">
here is what i have:
#!/usr/bin/perl
use strict;
use warnings;
my $url = "google.com";;
print "Location: $url\n\n";
print "Content-type: text/html\n\n";
%form=&parse_form();
etc....etc...
You could use the following alternative:
use strict;
use warnings;
my $url = "http://google.com";
print "Content-type: text/html\n\n";
print qq[
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>Redirecting...</title>
<meta HTTP-EQUIV="REFRESH" CONTENT="10;URL=$url">
</head>
<body>
</body>
</html>
];
The following is valid and should work fine:
use strict;
use warnings;
my $url = "http://google.com";
print "Location: $url\n\n";
I'm trying to create a simple program that will get the content of the webpage for the sake of my assignment.
Right now I create a very simple HTML page that will let the user enter a URL.
<html>
<head><title>URL page</title>
</head>
<body>
<form action="cgi-bin/b1.cgi" method="GET">
Enter the URL you want to see <input type="text" name="passing" size=40>
<input type="submit" value="submit">
</form>
</body>
</html>
so I just want to pass the url to my CGI program that I have so far
#!/usr/bin/perl
print "Content-type: text/html\n\n";
use LWP::Simple;
use CGI;
use HTML::HeadParser;
#my $pass = $cgi->param('passing');
$URL = get ("$passing");
$head = HTML::HeadParser->new;
$head->parse("$URL");
print "This is the Title of the page" . $head->header('Title') . "\n\n";
print $head->header('X-Meta-Description') . "\n\n";
print $head->header('X-Meta-Keywords') . "\n\n";
print $head->header('Content-Type') . "\n\n";
print $head->header('Content-Language') . "\n\n";
exit;
So from the above code as you can see if I can get the value that pass from the GET method to the line where it say URL = get(); then I can get the content.
I tried some approch like my $pass = $cgi->param('passing'); but it gives me an error about param
Any suggestion would be so much appreciated.