Net::POP3 Array Question 
Author Message
 Net::POP3 Array Question

Hello,  I'm trying to get a script to work that will POP a mail server
and grab the email from an account so that I can parse the email get the
information I need from it.

I am using the Net::POP3 module, and I am able to connect to the mail
server login and get the amount of messages that are available to
download but actually reading the message is a whole different story.

Here is a simple version of the script:

#!/usr/bin/perl

use Net::POP3;

$pop = Net::POP3->new('mail.my.domain');

$USER="username";
$PASS="password";

$result = $pop->login($USER,$PASS);

if ($result ne undef) {
  for ($num=0; $num<=$result; $num++) {


      print "$num: $line\n";
    }
  }

Quote:
}

$pop->quit();

As I understand it, this script should get the first 10 lines of each
message (including headers) and print them like:

1: line1
1: line2
1: line3
1: line4
etc.... for each message..

What I am getting when I run the script is:

1: ARRAY(0x20f0f8)
2: ARRAY(0x20f11c)
3: ARRAY(0x20f23c)
4: ARRAY(0x20f368)
etc...

It's probably just something stupid that I am missing, but does anyone
know why I am getting this error, and how to correct it? Any help would
be very much appreciated.

Thanks!

Darren Nay

Sent via Deja.com http://www.*-*-*.com/
Before you buy.



Wed, 18 Jun 1902 08:00:00 GMT  
 Net::POP3 Array Question

Quote:
> if ($result ne undef) {

Turn on warnings and 'use strict', and you'll catch this kind of mistake
right away. You want to be testing defined($result), I think. undef is
neither a string nor a number.

Quote:
>   for ($num=0; $num<=$result; $num++) {

Correct, but a more Perlish way is to use a foreach loop from 0..$result.

Quote:
> What I am getting when I run the script is:

> 1: ARRAY(0x20f0f8)

See perlref to learn about array references. Cheers!

--
Tom Phoenix       Perl Training and Hacking       Esperanto
Randal Schwartz Case:     http://www.rahul.net/jeffrey/ovs/



Wed, 18 Jun 1902 08:00:00 GMT  
 Net::POP3 Array Question

Quote:



>       print "$num: $line\n";
>     }

From the Net::POP3 documentation:

top ( MSGNUM [, NUMLINES ] )
    Get the header and the first NUMLINES of the body for the message
    MSGNUM. Returns a reference to an array which contains the lines of
    text read from the server.

Notice the word reference.

Quote:
> What I am getting when I run the script is:

> 1: ARRAY(0x20f0f8)
> 2: ARRAY(0x20f11c)
> 3: ARRAY(0x20f23c)
> 4: ARRAY(0x20f368)
> etc...

Read 'perldoc perlref' for more information.

Jon
--
Knowledge is that which remains when what is
learned is forgotten. - Mr. King



Wed, 18 Jun 1902 08:00:00 GMT  
 Net::POP3 Array Question

Quote:


> >   for ($num=0; $num<=$result; $num++) {

> Correct, but a more Perlish way is to use a foreach loop from 0..$result.

I disagree.  This is more ML-ish (or other lazy fuctional language).
In Perl this actually has to build a temporary list so this should
usually be avoided unless you are sure $result will be small.

--
     \\   ( )
  .  _\\__[oo

 .  l___\\
  # ll  l\\
 ###LL  LL\\



Wed, 18 Jun 1902 08:00:00 GMT  
 Net::POP3 Array Question


Quote:


> > >   for ($num=0; $num<=$result; $num++) {

> > Correct, but a more Perlish way is to use a foreach loop from 0..$result.

> I disagree.  This is more ML-ish (or other lazy fuctional language).
> In Perl this actually has to build a temporary list so this should
> usually be avoided unless you are sure $result will be small.

I'm sure this has been fixed (the sequential list has been optimized
away) in recent versions of perl.  But perlsyn says nothing about it
either way, and I can't find an explicit statement elsewhere.

--
(Just Another Larry) Rosler
Hewlett-Packard Laboratories
http://www.hpl.hp.com/personal/Larry_Rosler/



Wed, 18 Jun 1902 08:00:00 GMT  
 Net::POP3 Array Question

Quote:





> > > >   for ($num=0; $num<=$result; $num++) {

> > > Correct, but a more Perlish way is to use a foreach loop from 0..$result.

> > I disagree.  This is more ML-ish (or other lazy fuctional language).
> > In Perl this actually has to build a temporary list so this should
> > usually be avoided unless you are sure $result will be small.

> I'm sure this has been fixed (the sequential list has been optimized
> away) in recent versions of perl.  But perlsyn says nothing about it
> either way, and I can't find an explicit statement elsewhere.

From perldelta (5.005_03):

"foreach (1..1000000) optimized

"foreach (1..1000000) is now optimized into a counting loop.  It does
not try to allocate a 1000000-size list anymore."

- Tom



Wed, 18 Jun 1902 08:00:00 GMT  
 Net::POP3 Array Question

Quote:


>> >   for ($num=0; $num<=$result; $num++) {

>> Correct, but a more Perlish way is to use a foreach loop from 0..$result.

>I disagree.  This is more ML-ish (or other lazy fuctional language).
>In Perl this actually has to build a temporary list so this should
>usually be avoided unless you are sure $result will be small.

Not true anymore..  foreach my $i (1 .. 100) is optimized to a for
loop.  A temporary list is not built.  Although, I don't recall which
version this started in.
--



Wed, 18 Jun 1902 08:00:00 GMT  
 Net::POP3 Array Question

Quote:


>> >   for ($num=0; $num<=$result; $num++) {

>> Correct, but a more Perlish way is to use a foreach loop from 0..$result.

> I disagree.  This is more ML-ish (or other lazy fuctional language).
> In Perl this actually has to build a temporary list so this should
> usually be avoided unless you are sure $result will be small.

Er.  No longer - from perlop :

              In the current implementation, no temporary array
       is created when the range operator is used as the expres-
       sion in `foreach' loops, but older versions of Perl might
       burn a lot of memory when you write something like this:

           for (1 .. 1_000_000) {
               # code
           }

I think it has been like this from 5.005

/J\
--
Simpson-Homer Simpson, he's the greatest guy in his-tor-y. From the town
of Springfield, he's about to hit a chestnut tree....D'oh!
--
fortune oscar homer



Wed, 18 Jun 1902 08:00:00 GMT  
 Net::POP3 Array Question

Quote:


>> >   for ($num=0; $num<=$result; $num++) {
>> Correct, but a more Perlish way is to use a foreach loop from 0..$result.
>I disagree.  This is more ML-ish (or other lazy fuctional language).
>In Perl this actually has to build a temporary list so this should
>usually be avoided unless you are sure $result will be small.

from perldelta - what's new for perl5.005

     foreach (1..1000000) optimized

     foreach (1..1000000) is now optimized into a counting loop.
     It does not try to allocate a 1000000-size list anymore.

A simple speed test on a shared system:

#!/usr/local/bin/perl -w
use strict;
use Benchmark;

use vars qw($max);
$max=10000;

timethese(10000, {
        'Foreach' => sub {
                my $c;
                for(1..$max) {
                        $c+=$_;
                }
            },
        'For' => sub {
                my $c;
                for(my $i=0;$i<$max;$i++) {
                        $c+=$i;
                }
            },

Quote:
});

__END__
Benchmark: timing 10000 iterations of For, Foreach...
       For: 46 wallclock secs (44.09 usr +  0.00 sys = 44.09 CPU)
   Foreach: 40 wallclock secs (38.44 usr +  0.00 sys = 38.44 CPU)

--
John Borwick



Wed, 18 Jun 1902 08:00:00 GMT  
 
 [ 9 post ] 

 Relevant Pages 

1. NET::SSH, NET::FTP, NET::POP3 (how to detect)

2. configuring pop3 host for net::pop3

3. Net::POP3 question

4. Small '-w' Question with Net::POP3

5. Net::POP3 Modules question

6. Question on NET::POP3

7. Silly newbie question (Net::POP3)

8. Net::POP3

9. Net:POP3 From/To lines

10. Net::POP3 Module error under Perl5 for Win32

11. Net::POP3 fails to login to mailbox if its empty

12. Help with Net::Pop3

 

 
Powered by phpBB® Forum Software