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:
Write text Between Two Tags

 



Goldc0der
Novice

Nov 21, 2005, 8:25 AM

Post #1 of 21 (3179 views)
Write text Between Two Tags Can't Post

Hi:

I┤m new on Perl, and i┤m looking for a little help in my job. Hope you could help me.

I have 3 files.

novidades.htm - (a normal webpage with to hidden tags: <a name=[START]></a> and <a name=[END]></a>)

captura.shtml - (calls an include .pl <!--#include virtual="cgi-bin/novidades.pl" -->)

novidades.pl - (this file should read "novidades.htm" file and write all content between [START] and [END])


Theres the code of "novidades.pl" file:
___________________________________________________________________

#!/usr/bin/perl

$ficheiro="../novidades.htm";

open(LISTA,"$ficheiro");
@linhas=<LISTA>;
$retorno="";

print "Content-type: text/html\n\n";

for ($i=0;$i<scalar(@linhas);$i++) {

$retorno .= @linhas[$i] =~ /[START](.*)[END]/egi;
}

print "$retorno";
___________________________________________________________________

Unfortenelly, it gives an error:
"[an error occurred while processing this directive] "

If i delete "e" letter on the end of...
$retorno .= @linhas[$i] =~ /[START](.*)[END]/egi; <--- /egi <--- this e

... the result is:
111111111111111111111111


What can i do to write everything between [START] and [END] tags on my page?


Thanks a lot
SÚrgio


rork
User

Nov 21, 2005, 11:29 AM

Post #2 of 21 (3174 views)
Re: [Goldc0der] Write text Between Two Tags [In reply to] Can't Post

You could load the whole file at once and strip everything up until [start] and everything from [end]

Code
{ 
undef $/;
open(LISTA, '<', $fichero) or die "Can't open $fichero: $!";
my $retorno = <LISTA>;
close(LISTA);
}

$retorno =~ s/^.*\[start\]//i;
$retorno =~ s/\[end\].$//i;

print $retorno;


Some notes:
the open is between { } so the undef of $/ won't affect the rest of your script.
The first regexp will remove everything untill and including the last [start]
The second regexp will remove everything from and including the first [end] after the last [start]

edit: fixed the code
--
Don't reinvent the wheel, use it, abuse it or hack it.

(This post was edited by rork on Nov 23, 2005, 7:42 AM)


KevinR
Veteran


Nov 21, 2005, 12:44 PM

Post #3 of 21 (3172 views)
Re: [Goldc0der] Write text Between Two Tags [In reply to] Can't Post

it's worth mentioning that you have a big problem in your regexp with the use of [START] and [END]. Using square brackets in a regexp defines a character class. So perl is looking for any of the characters between the squarte brackets, it's not looking for the patterns [START] and [END]. Note rork has the square brackets escaped with a back-slash in his code so perl will treat them as regular text instead of character class delimiters. Using something more like your methods you could maybe** do something like this:


Code
open(LISTA,"$ficheiro"); 
my @linhas = <DATA>;
$retorno="";

print "Content-type: text/html\n\n";

my $reterno = '';
for (@linhas) {
$reterno .= $1 while (/\[START\](.*?)\[END\]/gi);
}

print $reterno;


** I said maybe because I don't know how the html file you are parsing is actually written.
-------------------------------------------------


Goldc0der
Novice

Nov 21, 2005, 1:53 PM

Post #4 of 21 (3170 views)
Re: [rork] Write text Between Two Tags [In reply to] Can't Post

Hi rork:

I tried your code but, it gives me an error:
[an error occurred while processing this directive]


What can i do more?

Thanks for your help!


Goldc0der
Novice

Nov 21, 2005, 1:58 PM

Post #5 of 21 (3169 views)
Re: [KevinR] Write text Between Two Tags [In reply to] Can't Post

Hi KevinR

I tried your code.
No errors, very nice, but it seems that could not find any text between [START] and [END] words. (No results!)

Any other suggestions?

Thanks for your help!


KevinR
Veteran


Nov 21, 2005, 2:44 PM

Post #6 of 21 (3167 views)
Re: [Goldc0der] Write text Between Two Tags [In reply to] Can't Post

post the html file that has these [START] and [END] tags in it that you are parsing.
-------------------------------------------------


KevinR
Veteran


Nov 21, 2005, 2:47 PM

Post #7 of 21 (3166 views)
Re: [Goldc0der] Write text Between Two Tags [In reply to] Can't Post

rorks code has a syntax error in this line:

open(LISTA, '<', $fichero) or die "Can't open $fichero: $!");

the last ')' should not be there:


open(LISTA, '<', $fichero) or die "Can't open $fichero: $!";
-------------------------------------------------


Goldc0der
Novice

Nov 21, 2005, 11:24 PM

Post #8 of 21 (3160 views)
Re: [KevinR] Write text Between Two Tags [In reply to] Can't Post

Hi KevinR:

Heres the content of novidades.htm file:

<html>
<head>
<meta http-equiv="Content-Language" content="pt">
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title></title>
</head>

<body>
<a name="[START]"></a>
<p><font face="Verdana" size="1"><b><font color="#800000">News 1</font><br>
</b>This is the news 1.</font></p>
<a name="[END]"></a>
<p><font face="Verdana" size="1"><b><font color="#800000">News 2</font><br>
</b>This is the news 2.</font></p>
<p><font face="Verdana" size="1"><b><font color="#800000">News 3</font><br>
</b>This is the news 3.</font></p>

</body>
</html>

The ideia is to catch always the first news and write it on an page (captura.shtml) that as the code:
<!--#include virtual="cgi-bin/novidades.pl" -->



Thanks again for your help KevinR


(This post was edited by Goldc0der on Nov 21, 2005, 11:39 PM)


KevinR
Veteran


Nov 22, 2005, 12:24 AM

Post #9 of 21 (3155 views)
Re: [Goldc0der] Write text Between Two Tags [In reply to] Can't Post

well, I would try something very similar to what rork posted then:


Code
open(LISTA, '<', $fichero) or die "Can't open $fichero: $!";  
my $retorno = do {undef $/; <LISTA>};
close(LISTA);

$retorno =~ s/^.*\[start\]//is;
$retorno =~ s/\[end\].*$//is;

print $retorno;

-------------------------------------------------


Goldc0der
Novice

Nov 22, 2005, 1:20 AM

Post #10 of 21 (3151 views)
Re: [KevinR] Write text Between Two Tags [In reply to] Can't Post

KevinR

I have no luck Frown

I tried that way to, but gave an error:
[an error occurred while processing this directive]


I tried to rename .pl file to .cgi, and i put the code:
<!--#exec cgi="cgi-bin/novidades.cgi"-->
... in captura.shtml file

With this last code you gave me, give no errors, but gave no results too :(

Theres no other way to terminate this problem?

Thanks for or patience Kevin


MrPJ
User

Nov 22, 2005, 2:35 AM

Post #11 of 21 (3148 views)
Re: [Goldc0der] Write text Between Two Tags [In reply to] Can't Post

Perhaps either the SSI directive isn't pointing to the correct path, or the SSI script isn't managing to load the file it is expecting to parse.


(This post was edited by MrPJ on Nov 22, 2005, 2:36 AM)


Goldc0der
Novice

Nov 22, 2005, 4:24 AM

Post #12 of 21 (3144 views)
Re: [MrPJ] Write text Between Two Tags [In reply to] Can't Post

Hi and thanks for your help MrPJ

I checkout all paths, and all tests are ok.

Need help plz!

Thanks again MrPJ


davorg
Thaumaturge / Moderator

Nov 22, 2005, 5:12 AM

Post #13 of 21 (3142 views)
Re: [Goldc0der] Write text Between Two Tags [In reply to] Can't Post


In Reply To
I tried that way to, but gave an error:
[an error occurred while processing this directive]


Whenever you get that error, you will almost certainly get a full error message in the web server error log. Looking at that will give you a big clue as to what the real problem is. Without that you are just guessing.

--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


Goldc0der
Novice

Nov 22, 2005, 6:23 AM

Post #14 of 21 (3138 views)
Re: [davorg] Write text Between Two Tags [In reply to] Can't Post

Hi davorg

Thanks for your help

I wrote to webmaster to know where server keeps the logs error files.

In meantime, i┤ll ask you if i can do what i whant with other way.



Thanks one more time.


davorg
Thaumaturge / Moderator

Nov 22, 2005, 6:26 AM

Post #15 of 21 (3136 views)
Re: [Goldc0der] Write text Between Two Tags [In reply to] Can't Post

Another way to see what the errors are would be to run the program from the command line.

Or you could add


Code
use CGI::Carp 'fatalsToBrowser';


to your program (but remember to remove it once you have finished debugging).

--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


Goldc0der
Novice

Nov 22, 2005, 6:29 AM

Post #16 of 21 (3135 views)
Re: [davorg] Write text Between Two Tags [In reply to] Can't Post

davorg

I just have access by FTP. Can┤t execute directly on the command line. :(

I put in first line of the script:
#!/usr/bin/perl
use CGI::Carp 'fatalsToBrowser';
...


This create some kind of log? When i run in the webbrowser, doesnt show anything.

Thanks again


(This post was edited by Goldc0der on Nov 22, 2005, 6:35 AM)


davorg
Thaumaturge / Moderator

Nov 22, 2005, 6:31 AM

Post #17 of 21 (3133 views)
Re: [Goldc0der] Write text Between Two Tags [In reply to] Can't Post

Then you're crippling yourself in your attempts to write CGI programs. You should seriously consider changing your web hosts, your customers, your sysadmins or whoever is imposing these constraints on you :)

--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


Goldc0der
Novice

Nov 22, 2005, 6:48 AM

Post #18 of 21 (3131 views)
Re: [davorg] Write text Between Two Tags [In reply to] Can't Post

Ahh.. finally something...

Ok... i did one thing. I put the code:
<!--#exec cgi="cgi-bin/novidades.cgi"-->
... on "captura.shtml" file.

I trade
<!--#include virtual="cgi-bin/novidades.pl" -->
by
<!--#exec cgi="cgi-bin/novidades.cgi"-->

then, i rename "novidades.pl" to "novidades.cgi"

I also trade the marks of "novidades.htm" from:
[START] -> xiniciox (Begin mark)
[END] -> xfimx (End mark)

The file "novidades.cgi" as the code:

#!/usr/bin/perl

use CGI::Carp 'fatalsToBrowser';

$ficheiro="../novidades.htm";

open(LISTA, '<', $ficheiro) or die "Can't open $fichero: $!";
my $retorno = do {undef $/; <LISTA>};
close(LISTA);

$retorno =~ s/^.*xiniciox//is;
$retorno =~ s/xfimx.*$//is;

print $retorno;

______________________________________________________

Now, when i run the script in the web browser there┤s no erros, but dont give anu results either.

It┤s seems, that .cgi file dont find any text between xiniciox and xfimx word. DAMN Mad



Help plz!


Goldc0der
Novice

Nov 22, 2005, 7:05 AM

Post #19 of 21 (3128 views)
Re: [Goldc0der] Write text Between Two Tags [In reply to] Can't Post

YESSSSSSSS... FINALLY...

Evething is OK know...

The problem was on START and END words. They are not accepted on <a name="[START]"></a>.

I trade to <a name="teste"></a>[START]

Keywords have to be outside the tags.

THIS CONVERSATION IS TERMINATED Cool

Thanks to all for your help.
Big hug to all.


KevinR
Veteran


Nov 22, 2005, 11:09 AM

Post #20 of 21 (3120 views)
Re: [Goldc0der] Write text Between Two Tags [In reply to] Can't Post

besides needing the correct tags you probably could have just added the http header to the last code I posted and it would have worked:

print "Content-type: text/html\n\n";
-------------------------------------------------


Goldc0der
Novice

Nov 23, 2005, 4:27 AM

Post #21 of 21 (3113 views)
Re: [KevinR] Write text Between Two Tags [In reply to] Can't Post

Hi KevinR

I know... its working 100%

Thanks a lot for your help m8 Wink

 
 


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

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