file operations in perl
Moderator: General Moderators
file operations in perl
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
Thanks
You can do it painlessly without a function.
Example:
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
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);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
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.
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.
Alright, it looks like you can use a module called LWP::Simple to do this.
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.
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);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.
It seemed to compile ok without any errors but it does not seem to write to the file. Code go's like:
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!
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);*EDIT* Just to note, I am running this as an application, not a web form!
Thanks alot for your help but I figured it out:
See I had to use C:\
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);