Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Opening External Files With Perl 3

Status
Not open for further replies.

ETbyrne

Programmer
Dec 27, 2006
59
US
I have a script that takes a url that someone enters in it and then the script is then supposed to open the file that the url points to and read it. The problem is I am not sure how to do this because to use perls open command you have to enter the machine path to the file.

Is there a way to open any file off the internet and just read it?

Thanks.

_______________________________________

You don't know me.
 
Code:
use LWP::Simple;

my $data = get "[URL unfurl="true"]http://url/file.txt";[/URL]

-------------
Cuvou.com | The NEW Kirsle.net
 
I tried this but I am getting an error. Here is my script:

Code:
#!/usr/bin/perl -w
use strict;
use CGI qw(:standard);
use Fcntl qw(:flock :seek);
use LWP::Simple;
use lib qq*./tylermenezes/lib/*;

my $feedpath = param('feed') or die ('You have to select a feed!');
my $feed = get "$feedpath";
my $path = "./mywebs/ETbyrne/rerss";
my $title = "0";
my $link = "0";
my $url = "";
my $name = "";

open(FILE, "$feed") or die ("Can not open rss feed: $!");
seek(FILE,0,0);
my @file = <FILE>;
close FILE;

And here is the error I get.

Code:
The specified CGI application misbehaved by not returning a complete set of HTTP headers. The headers it did return are:

Prototype mismatch: sub main::head vs ($) at e:\0\74\38\237038\user\240783\htdocs\mywebs\ETbyrne\rerss\reader.pl line 5
Unsuccessful open on filename containing newline at e:\0\74\38\237038\user\240783\htdocs\mywebs\ETbyrne\rerss\reader.pl line 16.
Can not open rss feed: No such file or directory at e:\0\74\38\237038\user\240783\htdocs\mywebs\ETbyrne\rerss\reader.pl line 16.

Note: I contacted my host (I know him myself) and he said that LWP::Simple is installed and told me to add line 6 to my code but didn't say why.

_______________________________________

You don't know me.
 
LWP::Simple's get() method reads the data from the URL. You don't need to open any files. i.e.

Code:
my $html = get "[URL unfurl="true"]http://www.bloodgate.com/";[/URL]

# would produce the output...
$html = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<meta name="MSSmartTagsPreventParsing" content="TRUE">
<meta http-equiv="imagetoolbar" content="no">
<link rel="stylesheet" type="text/css" href="base.css">
<base href="[URL unfurl="true"]http://bloodgate.com/">[/URL]
<title>
Bloodgate
</title>
</head>
<body bgcolor="#e8e9ff">

<h1>Bloodgate.com</h1>

<p>

Welcome to my personal homepage. Here you will find some of my work,
usually a mixture of art and software:
</p>

<h2>Projects</h2>

<ul>
<li>My current <a href="perl/">Perl</a> projects
(<a href="perl/graph/">Graph::Easy</a> - <small>render graphs as HTML, SVG, ASCII or Unicode art</small>,
<a href="perl/bigint/">Math::BigInt</a>,

<a href="perl/art.html">ArtShow - a photo gallery</a>, ...)
<li>My <a href="photos">photo gallery</a>, photo related <a href="/photo/">articles</a>
</ul>

<h2>Older stuff</h2>

<ul>
  <li><a href="/hacking/index.html">Phototainer</a>
  <li><a href="asmedit/index.html">ASMEdit</a> - a very old project

<li>See also my <a href="spams/index.html">spam statistic page</a>
<li><a href="lego/index.html">Lego</a> - a very geeky <I>toy</i>
</ul>

<h2>Contact</h2>

<p>
If you want to <A HREF="mail.htm">email</A> me, please use

<A HREF="[URL unfurl="true"]http://www.gnupg.org">GNUPG</A>[/URL] with my <A HREF="tels.asc">public key</A>.
Please see also my <a href="wishlist.html">wishlist</a>.
</p>

<div class="address">
<table width="98%" border=0>
<tr><td>
  <small>Created by <a href="/mail.html">Tels</a>:
  a very long time ago... &nbsp; Last modified: 2006-03-15
  <a href="[URL unfurl="true"]http://www.netcraft.com/whats?host=www.bloodgate.com">[/URL]

  Powered by...</a>
  </small>
<td align=right>
<a href="[URL unfurl="true"]http://validator.w3.org/check/referer"><img[/URL]
     src="img/w3c.png" height="31" width="88"
     border="0" alt="Valid HTML 4.01!"></a>
<a href="[URL unfurl="true"]http://petition.eurolinux.org"><img[/URL] src="img/patent_button.png" border="0" ALT="No patents!"></a>
</td>
</tr></table>
</div>

<div class="bottom">
<p align="center">
<A HREF="/img/xenu.png"><IMG SRC="/img/s_xenu.gif" alt="google adword"></A>
<BR>
<A HREF="[URL unfurl="true"]http://slashdot.org/yro/02/03/22/0141250.shtml?tid=153">Censorship</A>[/URL]

is no solution, and never <A HREF="[URL unfurl="true"]http://www.xenu.net/">works</A>,[/URL]
anyway (image, click to enlarge, shows adword for xenu.net on 2002-03-23 10:36 GMT +200, which expired a couple hours later - probably due to the hight amount
of views). (Read the linked slashdot story for details!)
</p>
<p align="right">
<font size=-2>
Please don't click this <a href="/drowns/">link</a>, it's a spambot trap.
</font>
</p>
</div>

</body>
</html>
';

-------------
Cuvou.com | The NEW Kirsle.net
 
So how would I go threw the file and read it? The only way I know is looping, which requires the opening.

Code:
open(FILE, "$feed") or die ("Can not open rss feed: $!");
seek(FILE,0,0);
my @file = <FILE>;
close FILE;

foreach (@file)
{
  print "$_";
}

NOTE: I don't just want to print the file to screen, I want to read it and store certain information, like reading a rss feed.

_______________________________________

You don't know me.
 
....well, i'm not sure if you can loop through a scalar string as if it were an array so... why not use split w/ the '\n'
being the argument... now you have your array.. and can do
@my_new_array = split ($str, '\n');
foreach $_ (@my_new_array) { etc..


something like that should work?
 
or for perl:
Code:
@my_new_array = split(/\n/,$str);
foreach my $line (@my_new_array) {
   do something with $line
}



------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Thanks everyone, it works now. :)

_______________________________________

You don't know me.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top