file operations in perl

XML, Perl, Python, and other languages can be discussed here, even if it isn't PHP (We might forgive you).

Moderator: General Moderators

Post Reply
User avatar
Joe
Forum Regular
Posts: 939
Joined: Sun Feb 29, 2004 1:26 pm
Location: UK - Glasgow

file operations in perl

Post by Joe »

Is there any method that any knows of that reads files like the PHP file_get_contents() but instead in Perl. Just something im trying out!

Thanks
User avatar
nigma
DevNet Resident
Posts: 1094
Joined: Sat Jan 25, 2003 1:49 am

Post by nigma »

Could you elaborate a bit?

Are you just trying to figure out how to read a file in perl? or are you wondering if there is a function that will read the contents of a file for you?
User avatar
Joe
Forum Regular
Posts: 939
Joined: Sun Feb 29, 2004 1:26 pm
Location: UK - Glasgow

Post by Joe »

I am trying to take the contents from an .html file on my server and place them into a .txt file on my machine! Preferably a function but if theres another way then great :)
User avatar
nigma
DevNet Resident
Posts: 1094
Joined: Sat Jan 25, 2003 1:49 am

Post by nigma »

You can do it painlessly without a function.

Example:

Code: Select all

$html_filename = "";	# name of html file to read from would go here
$text_filename = "";	# name of text file to move html files data to would go here

# open $html_filename for reading
open HTML_DATA, $html_filename or die "Unable to create filehandle HTML_DATA";

#open $text_filename for writing
open TEXT_FILE, ">$text_filename" or die "Unable to create filehandle TEXT_FILE";

while ($html_line = <HTML_DATA>) &#123;
	print TEXT_FILE $html_line;
&#125;

close(HTML_DATA);
close(TEXT_FILE);
Now depending on how you intend to use this file you may or may not want to learn how to perform some file tests.

Let me know if you have any questions. It's kind of ironic that you posted this question because I was just brushing up on some perl ;)
User avatar
Joe
Forum Regular
Posts: 939
Joined: Sun Feb 29, 2004 1:26 pm
Location: UK - Glasgow

Post by Joe »

Excellent nigma. Thanks alot. If I have any more questions I will be sure to ask :)

Joe 8)
User avatar
Joe
Forum Regular
Posts: 939
Joined: Sun Feb 29, 2004 1:26 pm
Location: UK - Glasgow

Post by Joe »

Well one more question actually. Is there any way so that it reads the page directly from the website if I run it as an application :) This would work out alot better for me!

Thanks
User avatar
nigma
DevNet Resident
Posts: 1094
Joined: Sat Jan 25, 2003 1:49 am

Post by nigma »

Yea, I'm sure you can get the contents of a webpage, say google and then put whatever html is generated into a text file.

But, I may have to pull out another perl book and look things up before I can say with certainty whether or not it would be pretty simple.

Let me do that and i'll post a followup.
User avatar
Joe
Forum Regular
Posts: 939
Joined: Sun Feb 29, 2004 1:26 pm
Location: UK - Glasgow

Post by Joe »

Thanks alot nigma, Much appreciated :)
User avatar
nigma
DevNet Resident
Posts: 1094
Joined: Sat Jan 25, 2003 1:49 am

Post by nigma »

Alright, it looks like you can use a module called LWP::Simple to do this.

Code: Select all

$urlToGet = "url_here";
$text_file = "text_file_here";
open TEXT, ">$text_file" or die "Unable to open $text_file";

# grap contents of $urlToGet with function "get"
$url_contents = get $urlToGet;

print TEXT $url_contents;

close(TEXT);
Worked for me, let me know about you :)

BTW, if you plan on doing a lot of this kind of stuff you may want to invest in one of the oreilly perl books. I've got both Learning Perl and Perl Cookbook, both have been very helpful in the past.
User avatar
Joe
Forum Regular
Posts: 939
Joined: Sun Feb 29, 2004 1:26 pm
Location: UK - Glasgow

Post by Joe »

It seemed to compile ok without any errors but it does not seem to write to the file. Code go's like:

Code: Select all

use LWP::Simple;

$urlToGet = "http://www.google.com";
$text_file = "start.txt";
open TEXT, ">$text_file" or die "Unable to open $text_file";

# grap contents of $urlToGet with function "get"
$url_contents = get $urlToGet;

print TEXT $url_contents;

close(TEXT);
And thanks for those book suggestions. I will take you up on that.

*EDIT* Just to note, I am running this as an application, not a web form!
User avatar
Joe
Forum Regular
Posts: 939
Joined: Sun Feb 29, 2004 1:26 pm
Location: UK - Glasgow

Post by Joe »

Thanks alot for your help but I figured it out:

Code: Select all

use LWP::Simple;
$filename = "C:\start.txt";
$url_contents = get("http://www.google.com");

open(DATA, "> $filename") or die("File Error!");

print DATA $url_contents;
close(DATA);
See I had to use C:\
Post Reply