Hello Monks,
I'll be honest, this is my first post here, and I'm fairly new to perl (I come from a PHP background, I'm very excited to start perl!). I've finally found a suitable project to try my (perl) hand at. I'm teaching a lab section of Engineering Computing this semester, and as part of the class, the students are to email me their work (which, so far, consists of excel workbooks). After manually saving 30-some-odd of these today, I said, "There's gotta be a better way". Enter perl. I've been doing some reading on WWW::Mechanize and have successfully implemented it... so far. I'm trying to get it to find all the submitted excel files in my inbox (done), and download them (having trouble with this). Here's what I have so far:
#!C:\perl\bin\perl.exe
use warnings;
use WWW::Mechanize;
my $mech = WWW::Mechanize->new();
my $url = 'https://webmail.my_university.edu';
$mech->get($url);
# login
$mech->submit_form(
form_number => 0,
fields => {
imapuser => 'c4onastick',
pass => 'my_password_here',
}
);
# Navigate back to the first page that contains the most recent messag
+es
$mech->follow_link(url_regex => qr/page=1/ );
# Find all links (messages) with Lab 2 in the subject
my @links = $mech->find_all_links( text_regex => qr/lab\s*2/i );
my $content;
foreach my $link (@links) {
print $link->url."\n";
print $link->text."\n";
print "Following...\n";
$mech->follow_link( url => $link->url);
$mech->follow_link( url_regex => qr/\.xls/i ); #link to the attach
+ed excel file
$content .= $mech->content; # this might be where it is breaking d
+own
last; # Jump out after one for testing
$mech->back;
}
mkdir "Eng_files", 0755 or die "Couldn't create directory: $!";
chdir "Eng_files" or die "Can't change directory: $!";
open(FH, ">", "test.xls");
print "Writing out...\n";
print FH $content;
print "Done.\n\n";
So far, it works, it writes out and everyone is happy, except excel. It wont let me open them (I get a funky ready-only error, I don't think its excel's fault, I think I'm doing something wrong in the perl above). I'll eventually save these with the student's name in the filename (which I can also get from the message), but for now, I need to be able to open them to grade them! Thanks in advance for your help. I really look forward to becoming part of this community!
(Next project is a perl script to grade them for me!)