10.07.2015 Views

Beginning Web Development With Perl : From Novice to ... - Nabo

Beginning Web Development With Perl : From Novice to ... - Nabo

Beginning Web Development With Perl : From Novice to ... - Nabo

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

112CHAPTER 6 ■ NET:: TOOLSmy $pop3conn = Net::POP3->new("mail.example.com", timeout => 30);if ($pop3conn->login($username,$password) > 0) {print "You've Got Mail!\n";my $messages = $pop3conn->list();foreach my $msg (keys %{$messages}) {print "Message $msg is $messages->{$msg} bytes\n";}}$pop3conn->quitThe quit() method closes the connection. I’ll explain it further in the “Deleting E-Mailand Quitting” section.The output from the program will be similar <strong>to</strong> the following, which shows that I happen<strong>to</strong> have 12 messages in this mailbox:You've Got Mail!Message 6 is 3353 bytesMessage 11 is 4234 bytesMessage 3 is 7721 bytesMessage 7 is 2385 bytesMessage 9 is 1578 bytesMessage 12 is 257788 bytesMessage 2 is 4700 bytesMessage 8 is 1659 bytesMessage 1 is 3723 bytesMessage 4 is 1312 bytesMessage 10 is 1832 bytesMessage 5 is 2145 bytesGetYou actually retrieve the message with the get() method, called with the message number asan argument. The get() method returns a reference <strong>to</strong> an array of the message itself. You cando as you please with this array reference, including simply printing the messages. Listing 6-2(Pop3example2.pl) shows an example that retrieves a message and prints it <strong>to</strong> STDOUT.Listing 6-2. Getting and Printing POP3 Messages#!/usr/bin/perl -wuse strict;use Net::POP3;my $username = "user\@example.com";my $password = "password";my $pop3conn = Net::POP3->new("mail.example.com", timeout => 30);my $nummsgs = $pop3conn->login($username,$password);

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!