Solved

Generating Webpage dynamically with Perl

Posted on 2004-05-02
11
246 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
[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
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
Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

 
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

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

Objective of This Article In 1990’s, when I was a budding software professional, I had a lot of confusion about which stream or technology, I had to choose to build my career. In those days, I had lot of confusion like whether to choose System so…
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 purpose of this video is to demonstrate how to Import and export files in WordPress. This will be demonstrated using a Windows 8 PC. Go to your WordPress login page. This will look like the following: mywebsite.com/wp-login.php : Click on Too…
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…

732 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