Solved

Generating Webpage dynamically with Perl

Posted on 2004-05-02
11
241 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
 
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
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.

 

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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Batch, VBS, and scripts in general are incredibly useful for repetitive tasks.  Some tasks can take a while to complete and it can be annoying to check back only to discover that your script finished 5 minutes ago.  Some scripts may complete nearly …
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
The viewer will learn how to dynamically set the form action using jQuery.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

914 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

16 Experts available now in Live!

Get 1:1 Help Now