1

I dont really understand how to assign the output of this code to either a variable for each line of output or just one big block variable. Im using Mail::POP3Client if that helps. The output i am looking to assign a variable to is " print "$_\n" if /^(From|Subject):/i;"

my $count = $pop->Count();
if ($count < 0) {
    print $pop->Message();
} elsif ($count == 0) {
    print "no messages\n";
} else {
    print "$count messsages\n\n";

    for my $i (1 .. $count) {
        foreach ($pop->Head($i)) {
            print "$_\n" if /^(From|Subject):/i;
        }
        print "\n";
    }
}

2 Answers 2

3

You have two options, depending on how you want to process the data

Define a variable and append successively to it

my $from = '';
...
foreach ($pop->Head($i)) {
    $from .= "$_\n" if (/^(From|Subject):/i);
}

this will give you a large string with all Froms and Subjects together. Or you define an array and append to the end of this array

my @from;
...
foreach ($pop->Head($i)) {
    push @from, $_ if (/^(From|Subject):/i);
}

this will result in an array, where each element contains one From or Subject line.

According to Mail::POP3Client, if you want to delete all messages on the POP server

for my $i (1 .. $count) {
    $pop->Delete($i);
}

before close should mark all messages for deletion. When you finally close the connection,

$pop->Close();

all pending deletes will be processed.

Sign up to request clarification or add additional context in comments.

1 Comment

the single variable seems to work better for my case. Do you have any idea how i would delete all messages after this script is ran in CPAN the syntax is Delete( MESSAGE_NUMBER ) but not sure how to apply this.
2

Make an array beforehand, called, say, @mystuff. Instead of print "$_\n" if /^(From|Subject):/i;, make it push(@mystuff, $_) if /^(From|Subject):/i;. At the end you have an array of the things currently being printed in the foreach.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.