Solved

Generating Webpage dynamically with Perl

Posted on 2004-05-02
11
242 Views
Last Modified: 2013-12-25
I believe this question is really simple.  I am have a Perl CGI file that I read a page through and I need to be able to then print out the html webpage.  I am in essence creating an html frame without frames.  So when you click on a link it passes you through this page and adds some stuff at the top.  I am fine when the page is part of my website and everything is local because I can read the file into an array and then print it out.  The problem is that a couple of the links are on another site so they come in as the full path with http://etc.  
I need to have a way of knowing the url to just print out the html.  Is there a command or a simple way of doing this.
0
Comment
Question by:spoma
11 Comments
 

Author Comment

by:spoma
ID: 10973948
I found LWP::Simple and this seems to do what I needed.  
0
 
LVL 48

Expert Comment

by:Tintin
ID: 10973953
To output the contents of a URL, do:

use LWP::Simple;
getprint('http://www.example.com/page.html');
0
 

Author Comment

by:spoma
ID: 10991951
TinTin
That worked to a degree.  The problems I encountered was that it was including all of the html heading tags.  I am trying to use a component called HTML::Parser and I have it pretty much working but I am trying ot get it not only to print out text but also to print out the <tags>.  here is th sample code from the web i found.  anyone have any ideas how to make this print out tags also.


 use HTML::Parser ();

  sub start_handler
  {
    return if shift ne "title";
    my $self = shift;
    $self->handler(text => sub { print shift }, "dtext");
    $self->handler(end  => sub { shift->eof if shift eq "title"; },
                           "tagname,self");
  }

  my $p = HTML::Parser->new(api_version => 3);
  $p->handler( start => \&start_handler, "tagname,self");
  $p->parse_file(shift || die) || die $!;
  print "\n";
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 48

Expert Comment

by:Tintin
ID: 10992657
Try

use LWP::Simple;
use CGI ':standard';

$page = get("http://www.example.com/page.html");
print escapeHTML($page);
0
 

Author Comment

by:spoma
ID: 10995158
That does not seem to work either.  What I need is that I do a get like what you suggest:

$treeview = get(url);

this will then get the entire contents of this url.  i need to be able to then strip out certain tags such as <html>,<title>asdfsaDF</title>, any meta information, the <head> tags so that my html will be valid.  

I am in essence taking this html and putting it in a table in the center of my webpage.  so i must get rid of all tags that will make it invalid html.  Am I going about this the best way?  The example I give gives me the ability to pull out certain tags but I can not get it to print the tags I want it merely prints the text associated with the tags.
0
 
LVL 48

Expert Comment

by:Tintin
ID: 11000090
So you're asking for something different now.  Previously you said you wanted to print out the tags also.

What form is the data in the page you are retreiving?  Is it already in a table, or is is just straight paragraphs?

0
 

Author Comment

by:spoma
ID: 11000443
No.  I am asking for the same thing.  The current code sample I showed you prints out only the text between certain <TAGS>.  I need it to print out the <TAGS> along with it.  So if I have HTML like the following

<html>
<head>
<style>hello</style>
</head>
etc....

And I say to print out between the <head> tags i need it to give me
<style>hello</style>
currently it would give me
hello
0
 

Accepted Solution

by:
gorch earned 50 total points
ID: 11013773
Hi spoma,

as far as I understand your problem you will need something like:

  # Read in the url line-wise as you would read a file.
  foreach $line (<url>) {

    # Check whether you reached your pattern from which you want to start collecting content.
    if ($line =~ /<start-tag-or-pattern>/) {

        # From now on until the end-tag appears do what you want with the content.
        do {
            print"$line";
        } until ($line =~/<end-tag-or-pattern/);

    }

  }

Embedding this peace of code in a loop and making the patterns variable will enable you to fetch various content pieces from whithin the page.

Does this help?

gorch
0
 
LVL 6

Assisted Solution

by:nitinsawhney
nitinsawhney earned 50 total points
ID: 11144700
Hi Spoma,
One more if you want to print the html code rather then allowing browser to render it then try something like this.


do{
    $line =~ s/</&lt;/g;  
    $line =~ s/>/&gt;/g;
    print "$line";
   }until ($line =~/<end-tag-or-pattern/);


--
Nitin
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Turn A Profile Picture Into A Cartoon Using Photoshop And Illustrator This tutorial will teach you how to make a cartoon style image out of a regular picture. I have tried to keep the tutorial as simple as possible. I used Adobe CS4 for this tuto…
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
The purpose of this video is to demonstrate how to set up basic WordPress SEO. This will be demonstrated using a Windows 8 PC. The plugin used will be WordPress SEO by Yoast. Go to your WordPress login page. This will look like the following: myw…
The viewer will learn how to count occurrences of each item in an array.

776 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