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

Dear Monks, I'm making a module that will go through my database logs and search for errors. When one is found it should email me. However when the module is run I get: Not a CODE reference at log_check.pl line 93. The code I have is below:
if (@errors) { $mailer = new Mail::Mailer("sendmail"); $mailer->(To => $admin_email, Subject => 'Database Log Errors') or die "Can't open +: $!\n"; print $mailer "@errors\n"; $mailer->close(); }
Any idea what the error means and how to resolve it? Thanks - from Deaflugs

Replies are listed 'Best First'.
Re: using the Mailer / sendmail perl module...
by apl (Monsignor) on Oct 08, 2007 at 14:37 UTC
    If you don't want to use a module, the simplest way of e-mailing to user@host.com is by calling:
    sub Mail { my ( $Msg ) = @_; my $Hdr = "From: Speaker to Users\n". "To: <user\@host.com>\n". "Subject: End of Day problems\n\n"; open( SENDMAIL, "|/usr/lib/sendmail -oi -t -odq") or die( "Can't fork for sendmail: $! \n" ); print SENDMAIL $Hdr.$Msg; close(SENDMAIL) or warn "sendmail didn't close correctly!"; }
      That works tremendously. I was pulling faces 15mins ago when I tested it, and nothing happened... 15 mins after it 'should' have arrived the email lands. So that shall be fine, it's for a midnight cron job and I'll be dammed if I'm leaping out of bed to offer support :P Thanks for everyones help!
        Sorry, I should have mentioned the delay in delivery. Like you, I use this for late night reporting where delivery time is not as critical as simply reporting the problem.

        (I do jump out bed to do support. 8-) )
Re: using the Mailer / sendmail perl module...
by jettero (Monsignor) on Oct 08, 2007 at 14:24 UTC
    Maybe Mail::Mailer isn't the best choice. Popular choices include MIME::Lite, which can use either sendmail or Net::SMTP. If you don't want to do any work Net::SMTP::OneLiner might help (disclosure: I wrote that).

    Also, I don't see a line 93 in your sample code... Could you give me a hint on that? What if it doesn't even relate to Mail::Mailer?

    -Paul

      apologies I forgot to mark it out.... line 93 is
      $mailer->(To => $admin_email, Subject => 'Mascot db_update errors') or die "Can't +open: $!\n";
      I can't see any issues with that line however. I originally thought it required a  $mailer -> open ({args}) but according to the module docs apparently not when I fell down on open not being recognised. I'll take a look at the smtp oneliner though see if it will fit the bill, thanks Paul.

        I should have been able to see it anyway then.

        my $lambda = sub { print "lol!\n" }; $lambda->(); # will work my $mailer = Object::Name->new(); $mailer->(); # will not work

        I think you need to call a method name there ($mailer->send() for example), rather than invoking the blessed reference as a code reference -- which clearly doesn't work.

        -Paul