Check POP3 E-mail

T

Tihana

How to check E-mail in POP3 mailbox,
if there have world "Congratulation" in subject.

TNX
 
M

Matt Garrish

wana said:
This might work. I modified the example from Mail::pOP3Client docs. I
did
not test so it may be wrong.

#! /usr/bin/perl

use strict;
use warnings;
my @message = ();
my $flag = 0;
use Mail::pOP3Client;
my $pop = new Mail::pOP3Client( USER => "username",
PASSWORD => "password",
HOST => "pop3.yourserver.net" );
for(my $i = 1; $i <= $pop->Count(); $i++ )
{
$flag = 0;
@message = ();
foreach( $pop->Head( $i ) )
{
/^(From|Subject|Date):\s+/i
&& push @message, "$_\n";

Why did you change from "and" to the higher precedence "&&"?
if (/^Subject:.*Congratulation.*/i)
{
flag = 1;

Hmm.. $flag = 1?
}
}
foreach( $pop->Body( $i ) )
{
push @message, "$_\n";
}
if (flag)

Oops, you did it again!

The script could certainly be cleaned up, but I suppose it's otherwise
functional. From the OP's posting, however, I would tend to infer that he
wants to delete messages with that word (i.e., a spam filter), so he can
probably just check the subject line and determine what to do at that point
instead of collecting the entire message into an array.

Matt
 
A

A. Sinan Unur

This might work. I modified the example from Mail::pOP3Client docs.
I did not test so it may be wrong.

Why post it then?
#! /usr/bin/perl

use strict;
use warnings;
my @message = ();

There is no need to explictly initialize my variables.
my $flag = 0;
Ditto.

use Mail::pOP3Client;
my $pop = new Mail::pOP3Client( USER => "username",
PASSWORD => "password",
HOST => "pop3.yourserver.net" );
for(my $i = 1; $i <= $pop->Count(); $i++ )
{
$flag = 0;
@message = ();

The fact that you keep reinitializing this variable means you have declared
it in the wrong scope.

Rest of the script snipped.

Basically, you haven't thought hard enough about the task. What you want to
do is check if the subject line of the message contains the required
phrase, and if it does, print the whole message. There is no need for all
the flag setting etc.

However, your main sin is responding to a poster who is asking for a canned
script without showing that he has put any effort into this by trying to do
the work he should have done. Now that the cat is out of the bag, however:

#! perl

use strict;
use warnings;

use Mail::pOP3Client;

my $pop = Mail::pOP3Client->new(
USER => 'xxxxxxx',
PASSWORD => 'xxxxxxx',
HOST => 'xxxxxxx',
);

my $count = $pop->Count;
$count >= 0 or die "Cannot get messge count.\n";
$count > 0 or die "No messages in mailbox.\n";

for my $i (1 .. $count) {
my @headers = $pop->Head( $i );
for my $h ( @headers ) {
if( $h =~ /^Subject:\s+Congratulation/ ) {
print print "$h\n", join "\n", $pop->Body( $i );
last;
}
}
}

$pop->Close;
__END__

D:\Home> perl chk.pl
Subject: Congratulation

This is a test
 
W

wana

Tihana said:
How to check E-mail in POP3 mailbox,
if there have world "Congratulation" in subject.

TNX

This might work. I modified the example from Mail::pOP3Client docs. I did
not test so it may be wrong.

#! /usr/bin/perl

use strict;
use warnings;
my @message = ();
my $flag = 0;
use Mail::pOP3Client;
my $pop = new Mail::pOP3Client( USER => "username",
PASSWORD => "password",
HOST => "pop3.yourserver.net" );
for(my $i = 1; $i <= $pop->Count(); $i++ )
{
$flag = 0;
@message = ();
foreach( $pop->Head( $i ) )
{
/^(From|Subject|Date):\s+/i
&& push @message, "$_\n";
if (/^Subject:.*Congratulation.*/i)
{
flag = 1;
}
}
foreach( $pop->Body( $i ) )
{
push @message, "$_\n";
}
if (flag)
{
foreach(@message)
{
print;
}
}
#$pop->Delete($i);
}
$pop->Close();
 
A

A. Sinan Unur

(e-mail address removed) (krakle) wrote in
....


How about for resetting purposes in mod_perl?

Care to show what you mean by giving us some code?

Please note that I have a pretty good idea what you might be referring to,
but it is your job to actually _make_ an argument (using code, since this
is a programming newsgroup) for the point you are trying to make.

Sinan
 
A

A. Sinan Unur

if( $h =~ /^Subject:\s+Congratulation/ ) {
print print "$h\n", join "\n", $pop->Body( $i );

Hmmmm ... clearly, the second 'print' should not be there. Arrrgh!

Sinan,
 
D

David Gale

Quoth A. Sinan Unur said:
There is no need to explictly initialize my variables.

Nor does it hurt to do so, and is a good habit to get into, in case you're
ever in an environment when you can't choose which language to program in,
and have to use something (say, C) which does not implicitly initialize
variables.

Of course, I always initialize my variables (in any language) simply because
I'm a control freak, and want to be darn certain I know what's in there.
;-)

-D.
 
G

Gunnar Hjalmarsson

David said:
Quoth A. Sinan Unur:

Nor does it hurt to do so, and is a good habit to get into, in case
you're ever in an environment when you can't choose which language to
program in, and have to use something (say, C) which does not
implicitly initialize variables.

Of course, I always initialize my variables (in any language) simply
because I'm a control freak, and want to be darn certain I know
what's in there. ;-)

Not sure what you guys are talking about now. :)

Does anything really get *initialized* by saying

my @array = ();

or

my %hash = ();

? I thought that was only in order to ensure that the array or hash is
*empty* (which isn't necessary since my() takes care of it).

Do you actually initialize anything but scalars?
 
G

Gunnar Hjalmarsson

wana said:
I believe that declaring a scalar in Perl automatically sets it to a
false value.
Yes...

I don't know if it is '' or 0

Neither. It's undef.
but it will become the appropriate one when you use it.

Well, you will get an uninitialized warning if you for instance print
the undefined value or do

if ($num < 1) {
# do something;
}
Also, I did have the mod_perl thing in mind too. Apparently mod_perl
scripts will stay loaded in memory after running and variables may
retain their values, requiring the implicit initialization.

Global variables retain their values in mod_perl, but I don't think
that's true for lexically scoped variables.
 
W

wana

Gunnar said:
Not sure what you guys are talking about now. :)

Does anything really get *initialized* by saying

my @array = ();

or

my %hash = ();

? I thought that was only in order to ensure that the array or hash is
*empty* (which isn't necessary since my() takes care of it).

Do you actually initialize anything but scalars?

I have a lot of habits that carry over from C and C++ and also from being
unsure of things like precedence (extra parentheses) and variable
initialization, like in this case.

One thing I especially like about Perl, as I am learning, is that everything
has a definite defined action or meaning. For example, the list of things
that will resolve to false like '', 0, undef etc... I believe that
declaring a scalar in Perl automatically sets it to a false value. I don't
know if it is '' or 0 but it will become the appropriate one when you use
it. Anyway, the point is that there are no undefined things like in C where
you don't know what value you will find in an uninitialized variable and
the defined action may vary from system to system.

Also, I did have the mod_perl thing in mind too. Apparently mod_perl
scripts will stay loaded in memory after running and variables may retain
their values, requiring the implicit initialization.

I have found that doing most of my Perl programming on a shared system which
does not use mod_perl due to security risks, I have not really had to worry
about it. I think that mod_perl is only safely used on a dedicated server.
The book I have on Perl and MySQL for the Web goes to great lengths to get
readers to install and use mod_perl and then explains later that it can not
be used safely on shared systems. A shared web server is the only
practical and affordable way I can write Perl CGI programs and keep them
online 24/7.

By the way, the module support for e-mail in Perl is really cool. I have
some great automation things going on that I never thought were possible.
My family is getting sick of receiving an e-mail every 24 hours with a
randomly generated directory link to my family photos. They think my
security measures are a little excessive.

wana
 
J

John W. Krahn

David said:
Nor does it hurt to do so, and is a good habit to get into, in case you're
ever in an environment when you can't choose which language to program in,
and have to use something (say, C) which does not implicitly initialize
variables.

According to the C FAQ:

Variables with static duration (that is, those declared outside of functions,
and those declared with the storage class static), are guaranteed initialized
(just once, at program startup) to zero, as if the programmer had typed ``=
0''. Therefore, such variables are initialized to the null pointer (of the
correct type; see also section 5) if they are pointers, and to 0.0 if they are
floating-point.


John
 
T

Tassilo v. Parseval

Also sprach John W. Krahn:
According to the C FAQ:

Variables with static duration (that is, those declared outside of functions,
and those declared with the storage class static), are guaranteed initialized
(just once, at program startup) to zero, as if the programmer had typed ``=
0''. Therefore, such variables are initialized to the null pointer (of the
correct type; see also section 5) if they are pointers, and to 0.0 if they are
floating-point.

But then, static variables are roughly the equivalent to Perl's package
variables. They are used of course but not a fraction as frequently as
automatic variables that may be declared at the beginning of a block
(with new Ansi99 even anywhere, I think). And those are never
initialized to anything sensible.

Tassilo
 
D

David Gale

Quoth John W. Krahn said:
According to the C FAQ:

Variables with static duration (that is, those declared outside of
functions, and those declared with the storage class static), are
guaranteed initialized (just once, at program startup) to zero, as if
the programmer had typed ``= 0''. Therefore, such variables are
initialized to the null pointer (of the correct type; see also
section 5) if they are pointers, and to 0.0 if they are
floating-point.

Ok, there's one situation in which C pre-initializes variables. However,
that's a rather rare one. Most variables are non-static (declared inside
functions, such as main(), and without the static storage class), and thus
not pre-initialized. Pointers are an extreme example, but very, very
common.

Personally, I initialize my static variables explicitly, even though they
are implicitly initialized, because it makes it clearer and far less likely
to lead to hard-to-find bugs (example: in a code re-write, you realize that
a variable doesn't need to be static, so you remove the keyword, but forget
to add in an initialization...might work some of the time, but not all the
time.)
 
B

Ben Morrow

It is a trivial waste of runtime, and a non-trivial waste of a reader's
time when they try to figure out *why* you're explicitly initializing
something which doesn't need it.

Thus I would strongly disagree here. Write less code, unless that makes
things less clear.

Know The Language You're Writing In. It *never* pays to carry habits
across from one language to another.

You do. Perl guarantees variables to be correctly initialized to what
you expect. If you don't trust Perl to leave $x undef after 'my $x;',
why do you trust it after 'my $x = undef;'?
Not sure what you guys are talking about now. :)

Does anything really get *initialized* by saying

No, well, not in any way that does not occur with 'my @array;'. Perl
does not have the concept of an explicit initializer, in the way that
(say) C does. Scalars are implicitly initialized to undef, arrays and
hashes to the 'empty foo'.
my @array = ();

This statement creates a variable, with an implicit initialization (my
has both compile-time and run-time parts), and then assigns the empty
list to it (which is pointless, as it was just initialized to that). In
C, for instance,

char a = 'a';

is qualititatively different from

char a; a = 'a';

as the first will succeed if 'char' is changed to 'const char' but the
second will fail.

Ben
 
K

krakle

A. Sinan Unur said:
(e-mail address removed) (krakle) wrote in


Care to show what you mean by giving us some code?

Please note that I have a pretty good idea what you might be referring to,
but it is your job to actually _make_ an argument (using code, since this
is a programming newsgroup) for the point you are trying to make.

It was a question I made, not a point. I'm not trying to _make_ an
arguement. mod_perl scripts stay in memory. Unless you reset the
values of a variable at the beginning of the script or before that
variable is used it may contain the previous users data...
 
K

krakle

A. Sinan Unur said:
(e-mail address removed) (krakle) wrote in


Care to show what you mean by giving us some code?

Please note that I have a pretty good idea what you might be referring to,
but it is your job to actually _make_ an argument (using code, since this
is a programming newsgroup) for the point you are trying to make.

Since you insist on seeing code for whatever unknown reason before you
answer my question (ignorance). I'll show you some...

_WITHOUT resetting variable_

use CGI;
my $cgi = new CGI;

my @message;

if (some_other_yada) {
push (@message, "This is message #1: $cgi->param('msg1')");
}

if (some_other_yada) {
push (@message, "Now this is message 2: $cgi->param('msg2')");
}

if (@message) {
foreach (@message) { print "$_\n" }
} else {
print "No messages";
}

If ran as a CGI everything works fine... HOWEVER, if ran as mod_perl
@message retains the previous users data then the new users data adds
to the array then the next users data will retain the last 2 users and
so on... So Even if the current users session never had any "messages"
to add @message will not be empty. BUT if you used

my @message = ();

just like the original poster used the array woudl be reset at the
start of the script emptying any data left over from the previous
session.
 
G

Gunnar Hjalmarsson

krakle said:
Since you insist on seeing code for whatever unknown reason before
you answer my question (ignorance). I'll show you some...

_WITHOUT resetting variable_

use CGI;
my $cgi = new CGI;

my @message;

if (some_other_yada) {
push (@message, "This is message #1: $cgi->param('msg1')");
}

if (some_other_yada) {
push (@message, "Now this is message 2: $cgi->param('msg2')");
}

if (@message) {
foreach (@message) { print "$_\n" }
} else {
print "No messages";
}

Did you run that code? Apparently not, since it doesn't even compile
(under strict). Consequently, it does not serve the purpose of
illustrating anything.
If ran as a CGI everything works fine... HOWEVER, if ran as mod_perl
@message retains the previous users data then the new users data adds
to the array then the next users data will retain the last 2 users
and so on... So Even if the current users session never had any
"messages" to add @message will not be empty. BUT if you used

my @message = ();

just like the original poster used the array woudl be reset at the
start of the script emptying any data left over from the previous
session.

As I explained in another message in this thread, your theory is
incorrect, and if you had written real code and run it, you'd have found
out for yourself.
 
A

A. Sinan Unur

(e-mail address removed) (krakle) wrote in
arguement. mod_perl scripts stay in memory. Unless you reset the
values of a variable at the beginning of the script or before that
variable is used it may contain the previous users data...

Your statement is full of half truths. If you don't write 'good' programs,
then bad things surely mya happen. However, I have quite a few CGI scripts
that are being run under mod_perl, and I have yet to run into such an
issue.

Sinan.
 
B

Brian McCauley

Gunnar said:
Global variables retain their values in mod_perl, but I don't think
that's true for lexically scoped variables.

Top level lexically scoped variables in modules do retain their values
under mod_perl as one would expect.

Personally I dislike the use of the term "global" to refer to package
variables since for some purposes top-level lexicals should also be
considered global.

CGI scripts running under registry will be wrapped in a subroutine
wrapper so once this has happened they don't have any top level
lexically scoped variables. If you did have any and were relying on
them behaving as global variables (i.e. being shared by all subroutines)
then you should replace them with package variables. Having done this
you will need to explicitly undefine them if you want to avoid them
retaining their values. It is best to do this using local()[1].

You must of course remember that the retained value is not session aware.

[1] There are people who disagree with me about this. This is becasue
they've heard that using local() is bad. This in turn is because many
people have in the past used local() where they really would be better
off with the semantics of my(). This message has then taken on
religious proportions such that people believe local() is bad even when
they actually want the semantics of local() and not those of my().
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,781
Messages
2,569,615
Members
45,294
Latest member
LandonPigo

Latest Threads

Top