Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Email::Find question

by cajun (Chaplain)
on Apr 05, 2002 at 05:23 UTC ( [id://156824]=perlquestion: print w/replies, xml ) Need Help??

cajun has asked for the wisdom of the Perl Monks concerning the following question:

I have a local file (a mailbox file actually) that I wish to parse and extract all of the various email addresses from. At this point, I do not care about their validity.

I've located the module Email::Find and installed it. In the POD there is an example given that appears to do what I'm looking for. Problem is the example is object oriented and I've not had any experience with OO thus far. I'm sure this is incredibly simple to those who studied and know OO, but I have not. I've spent a good amount of time searching for code using this module, but haven't found any.

The following is the basic example from the POD (I added my mailbox file):

#!/usr/bin/perl -w use Email::Find; $mailbox='./mail/addresses'; my $finder = Email::Find->new(sub { my($email, $orig_email) = @_; print "Found "..$email->format."\n"; return $orig_email; }); $finder->find(\$text);

I need a little bit of a kickstart to get to understanding how to use the module.

Replies are listed 'Best First'.
Re: Email::Find question
by tachyon (Chancellor) on Apr 05, 2002 at 06:16 UTC

    OO is easy to understand once you get your head around the basic concepts. First you create an object using the constructor (almost always called 'new') and store it in a scalar like $obj. Then you call methods on that object using the -> notation. A 'callback' is a reference to a sub that you want to run every time an event happens (like your method finds an email address).

    Try these tutorials perlman:perlobj and perlman:perltoot

    # make a new object of type 'Object' and pass it a callback sub my $obj = new Object( sub{print "Hello World"} ); # call a method on that object $obj->method; # when we 'use' an OO module we effectively just add its packages to o +ur code # so lets just write a little package here that has a callback sub package Object; use Data::Dumper; sub new { my $class = shift; my $callback = shift; my $anon_ref = { 'callback' => $callback }; # make object by blessing our anon hash into $class my $object = bless $anon_ref, $class; return $object; } sub method { my $self = shift; print "This is what \$self looks like:\n\n", Dumper($self), "\n\n"; print "And here is our callback sub in operation\n\n"; &{$self->{'callback'}}; }

    cheers

    tachyon

    s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

Re: Email::Find question
by mla (Beadle) on Apr 05, 2002 at 05:43 UTC
    Try this:
    #!/usr/bin/perl -w our $MAILBOX = './mail/addresses'; use strict; use Email::Find; use IO::File; # Read the mailbox my $fh = IO::File->new($MAILBOX) or die "unable to read mailbox '$MAILBOX': $!"; my $mail; { local $/; $mail = <$fh>; } # Find addresses my %addy; my $finder = Email::Find->new( sub { my($email, $orig_email) = @_; $addy{ $email->address }++; return $orig_email; } ); $finder->find(\$mail); foreach my $address (keys %addy) { print "$address $addy{ $address }\n"; }

    You just needed to read the mailbox file so you could pass it to the find() method.

    You should also become familiar with the Mail::Address module, since that's what is passed to your callback routine.

    You didn't indicate what you actually wanted to do with the addresses, so in this example I'm just storing them in a hash.

Fun with OO -- Re: Email::Find question
by joealba (Hermit) on Apr 05, 2002 at 15:24 UTC
    tachyon's post above covers the bases. Here are a few other tidbits about Object Oriented Perl as explained by TheDamian.

    A class is a package. You must have seen packages in Perl before, and you probably understand that they give you a nice, fresh, separate name space for some group of data and functionality. Well, that's fundamentally all an OO class is. The main difference is that classes do not / should not Export any variables into the namespace from which it is called. It gives you the methods to do what you need, without inviting you into it's house for tea.

    A method is a subroutine. Make a sub in that package/class file, and voila! You have a method that operates on the data in your class! These methods are often the only things that are allowed to play with the data in your object.

    An object is a blessed referent. Here's the magic. The Perl function bless() simply associates your class' data structure with that specific class. If you dump your class object with Data::Dumper, you'll see the class name tacked on the end. This is how Perl knows that your data belongs to that class, and it bumps up your data structure's reference count so that it sticks around.

    I'm a HUGE fan of OO programming. If you have the time and the motivation, I highly recommend that you grab this book: Object Oriented Perl. It's made my job so much easier and more fun! Seriously!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://156824]
Approved by PotPieMan
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (4)
As of 2024-04-26 06:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found