Solved

How to create a CGI script for perl code?

Posted on 2000-02-16
11
235 Views
Last Modified: 2010-03-05
How do I create a CGI script for the following perl code?

The follw. script  dmag helped me with, I want to call it from a CGI.  
I tried it but there's no output in keywords.html
How can I place/call this script from a key.cgi file.

#!/usr/bin/perl
#File- anchors.pl
                # This uses 4 files:
                open(KI, "<keywords.txt")   or die; # simple keywords, one per line
                open(KO, ">keywords.html")  or die; # The htmlized kewords
                open(AI, "<ia_source.html") or die; # The original HTML document
                open(AO, ">ia.html")        or die; # The bold/tagged HTML document

                @keywords = <KI>; # grab all the keywords
                chomp @keywords; chomp @keywords;  # Remove linefeeds

                # Make sure keywords are unique. I assume only 1 kw per document is needed
            #    @keywords = grep { !$seen{$_}++ } @keywords;
@keywords = grep { s/ //g; $_ && !$seen{$_}++ } @keywords; ## handles spaces in input file
                print KO<<EOF; # This is the start of the keywords.html doc
                <HTML>
                  <title>This is the Keywords Document</title>
EOF
                undef $/; # turn of line-at-a-time processing, and suck up whole files
                # Assumption: You have enough RAM to load in ia.html into memory.

                ($head,$_) = split /<BODY/i, <AI>; # read in HTML.
                # Strip off everything before body tag, since we can't manipulate it

                foreach $k (sort @keywords)  ## added sort to alphabetize words
                {
                  $k =~ s/\s//g; # No whitespace allowed in keyword (otherwise, need to
                  # mess around with the link -- it can't have spaces.)
                  print KO "<A HREF='ia.html#$k'>$k</A><BR>\n"; # add outbound link
                  s!$k!<A NAME='$k'><B>$k</B></A>!; # Create inbound link
                # I assume that none of the keywords are subsets of the other keywords..
                }

                print AO "$head<BODY$_";
~
0
Comment
Question by:sdesar
  • 7
  • 4
11 Comments
 
LVL 3

Accepted Solution

by:
guadalupe earned 10 total points
Comment Utility
You can simply pace this code in a cgi which you call from say the action of a form.  Then just print the output you want to show as a result without specifying a finle handle.  Standard output of a perl script being run a cgi will go to the calling browser.  The only other thing is to print the MIME header first like this:


#!usr/bin/perl

#previous script stuff

print <<EOF;
Content-type: text/html\n\n
<HTML>
<HEAD>
<TITLE> New Document </TITLE>

</HEAD>

$head<BODY$_

EOF

##########################

Just remember to put a return and NO ";" after the final EOF...
 


0
 

Author Comment

by:sdesar
Comment Utility
I will try the above suggestion.

I need a suggestion-
The above script
creates an anchor for a keyword eg- national
then the .html file has creates a link
to international... since it has national in it.

How can I avoid this?
0
 
LVL 3

Expert Comment

by:guadalupe
Comment Utility
Try this:

s!\b$k\b!<A NAME='$k'><B>$k</B></A>!;

This forces a word boundery around the word...
0
 

Author Comment

by:sdesar
Comment Utility
Thanks the above suggestion it worked...

I have one more ques.  instead of pointing to that keyword how can I modify the script so it gets just that whole paragraph that contains the keyword that I click on... and the paragraph is defined bet. <p> </p> or <P> and </P>.n How can I get the whole paragraph?
~
0
 
LVL 3

Expert Comment

by:guadalupe
Comment Utility
Oh yeah I forgot about this question...

The problem is that you'll have to make a link to another perl script that will be passed the indicated ky words and then read out the indicated paragraps and then display them...  Place a comment in that old question so I get the emaiul and I'll go back and see what I can do...
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:sdesar
Comment Utility
I tried your suggestion on Placing the file in a CGI script.

It updates keywords.html --- BUT it updates the previous data and not the current data that's in keywords.out

Here's the script-

#######################CREATE ANCHORS to the keywords##################
# This uses 4 files:
                open(KI, "<./tmp/keywords.out")   or die; # simple keywords, one per line
                open(KO, ">./tmp/keywords.html")  or die; # The htmlized keywords
                open(AI, "<./tmp/file.html") or die; # The original HTML document
                open(AO, ">./tmp/fileKeywords.html")        or die; # The bold/tagged HTML document

                @keywords = <KI>; # grab all the keywords
                chomp @keywords; chomp @keywords;  # Remove linefeeds

                # Make sure keywords are unique. I assume only 1 kw per document is needed
          ##      @keywords = grep { !$seen{$_}++ } @keywords;
@keywords = grep { s/ //g; $_ && !$seen{$_}++ } @keywords; ## handles spaces in input file
                print KO<<EOF; # This is the start of the keywords.html doc
print <<EOF;
Content-type: text/html\n\n
                <HTML>
                <HEAD>
<style type="text/css">
A {text-decoration:none}
</style>
<head>
                  <title>This is the Keywords Document</title>
</head>
EOF
                undef $/; # turn of line-at-a-time processing, and suck up whole files
                # Assumption: You have enough RAM to load in fileKeywords.html into memory.

                ($head,$_) = split /<BODY/i, <AI>; # read in HTML.
                # Strip off everything before body tag, since we can't manipulate it
                foreach $k (sort @keywords)
                {
                  $k =~ s/\s//g; # No whitespace allowed in keyword (otherwise, need to
                  # mess around with the link -- it can't have spaces.)
                  print KO "<A HREF='http://jbh3-1.csci.csusb.edu/public/grad/sdesar/tmp/fileKeywords.html#$k' target=defsbox>$k</A><BR>\n"; # add outbound link#                  s!$k!<A NAME='$k'><B>$k</B></A>!; # Create inbound link

         s!\b$k\b!<A NAME='$k'><B>$k</B></A>!;
     # I assume that none of the keywords are subsets of the other keywords..
                }

                print AO "$head<BODY$_";


Also, here's the url to test

http://jbh3-1.csci.csusb.edu/public/grad/sdesar/url_bold.cgi

enter a URL
CHECK THE RESULTS in -

    Keywords in ASCII
    Keywords in HTML with anchors created Version
    Finds Keywords and creates the anchorsin BOLD HTML Version

enter another URL and check the results again
0
 

Author Comment

by:sdesar
Comment Utility
I placed the comment in
the foll. question

sdesar
                                         Title: "Need an Opinion - finding similarity"
0
 

Author Comment

by:sdesar
Comment Utility
I have placed a comment in the foll. ques. also..
I think this relates to the topic-

sdesar

How to FETCH PARAGRAPHS- which have Keywords?  
0
 

Author Comment

by:sdesar
Comment Utility
guadalupe -checking to see if U did an an email notification..
0
 

Author Comment

by:sdesar
Comment Utility
Thanks for your assistance.
0
 
LVL 3

Expert Comment

by:guadalupe
Comment Utility
Sorry its a busy day I'll try and get to it tomorrow... sorry.
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
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.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

771 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

9 Experts available now in Live!

Get 1:1 Help Now