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

Hi. I'm trying to make a class that overrides Mail::Audit's accept() method. I want to do a few simple things then call SUPER::accept(). It all seems fairly straightforward, but my overridden accept() doesn't seem to be running at all. I'm relatively inexperienced with Perl's OO inheritance, so maybe there's something I'm missing?

Here's my new class:

package Mail::Audit::Summarize; use strict; use warnings; use Mail::Audit; our @ISA = qw(Mail::Audit); 1; sub accept { # I put this in to see if my accept() is even being # called. It appears not (since this die() call is # apparently not running). die "MY ACCEPT IS RUNNING.\n"; my $self = shift; # nothing here yet. $self->SUPER::accept(@_); }

And here's how I call it:

#!/usr/bin/perl use strict; use warnings; use lib '/home/diablo/modules'; use Mail::Audit::Summarize; my $item = Mail::Audit::Summarize->new(); $item->accept();

When I run email through this filter, the mail gets properly deposited into my mail spool, but doesn't seem to use my accept() at all. Any pointers or problems found would be much appreciated.

Replies are listed 'Best First'.
Re: Overriding Mail::Audit's accept()
by chromatic (Archbishop) on Jun 30, 2003 at 02:02 UTC

    It's not you, it's the Mail::Audit constructor. It doesn't do a simple bless $self, $class as you'd expect. I don't have a better solution at the moment, unfortunately, unless you'd like to say:

    { my $accept = \&Mail::Audit::accept; local *Mail::Audit::accept; *Mail::Audit::accept = sub { die "My accept is running\n"; my $self = shift; $self->$accept( @_ ); }; $item->accept(); }

    I wouldn't keep that around for long though. :)

      Yikes, I didn't notice that. I was looking around in Mail::Audit's code, but didn't notice the weird constructor. Thanks for pointing this out. Incidentally, has anyone got any idea why Mr. Cozens would choose to make his constructor like this?

        Incidentally, has anyone got any idea why Mr. Cozens would choose to make his constructor like this?

        I only skimmed it, but it looks Mail::Audit::new works more as a factory (for getting either a Mail::Audit::MailInternet or Mail::Audit::MimeEntity object) then as a constructor. It seems like this would have been something that should be mentioned in the perldocs.

        Speaking of the documentation, if your goal is to overide the accept method, then this section of Mail::Audit seems applicable...

        new(%options) ... Other options include the accept, reject or pipe keys, which specify subroutine references to override the methods with those names.
Re: Overriding Mail::Audit's accept()
by bobn (Chaplain) on Jun 30, 2003 at 01:58 UTC
    Is your module in /home/diablo/modules/Mail/Audit/Summarize.pm, or in $INC[something]/Mail/Audit/Summarize.pm? The OO model can get very confused when the package declaration doesn't match the directory structure.

    --Bob Niederman, http://bob-n.com