Page 1 of 1
file operations in perl
Posted: Sun Jul 11, 2004 5:53 pm
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
Posted: Sun Jul 11, 2004 6:09 pm
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?
Posted: Sun Jul 11, 2004 6:12 pm
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

Posted: Sun Jul 11, 2004 6:25 pm
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>) {
print TEXT_FILE $html_line;
}
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

Posted: Sun Jul 11, 2004 6:27 pm
by Joe
Excellent nigma. Thanks alot. If I have any more questions I will be sure to ask
Joe

Posted: Sun Jul 11, 2004 6:31 pm
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
Posted: Sun Jul 11, 2004 6:40 pm
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.
Posted: Sun Jul 11, 2004 6:42 pm
by Joe
Thanks alot nigma, Much appreciated

Posted: Sun Jul 11, 2004 6:48 pm
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.
Posted: Sun Jul 11, 2004 6:59 pm
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!
Posted: Sun Jul 11, 2004 7:32 pm
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:\