Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to create a CGI script for perl code?

Posted on 2000-02-16
11
Medium Priority
?
272 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 4
11 Comments
 
LVL 3

Accepted Solution

by:
guadalupe earned 20 total points
ID: 2528255
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
ID: 2528350
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
ID: 2528426
Try this:

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

This forces a word boundery around the word...
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:sdesar
ID: 2528498
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
ID: 2528568
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
 

Author Comment

by:sdesar
ID: 2528627
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
ID: 2528662
I placed the comment in
the foll. question

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

Author Comment

by:sdesar
ID: 2529908
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
ID: 2532195
guadalupe -checking to see if U did an an email notification..
0
 

Author Comment

by:sdesar
ID: 2532204
Thanks for your assistance.
0
 
LVL 3

Expert Comment

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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
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…
Six Sigma Control Plans

722 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