Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

Re: problematic OO

by castaway (Parson)
on Aug 03, 2005 at 09:05 UTC ( #480405=note: print w/replies, xml ) Need Help??

in reply to problematic OO

Your problems are manyfold:
  • You call the new() sub without passing it any parameters, and inside the sub you attempt to read from $self, which is an empty hashref.
  • You pass the subject and message params to send as a hashref, yet they end up in the $user variable, which itself is never used
You seem to be missing the fact that OO is not quite magic, methods are just ordinary subs, so you still need to read their parameters from what gets passed in. Is the subject supposed to be set on new() (ie object creation), or when you send the email?

Also, you have a global %mail hash, which will stay the same for every PL::Mail object you create, which probably isnt what you want.. To make it unique per object, you need to store that data in $self.

Also you need to be very careful about case sensitivity, $self->{SUBJECT} and $self->{subject} are not the same thing.

Here is an attempt at repair:

package PL::Mail; use strict; use Mail::Sendmail; ## Only global is your server since it doesnt change (?) my $server = ''; ## The new constructor just sets up the server and the From address si +nce these are static (could put the server name here directly and not + have a global variable) sub new { my $class = shift; my $self = {}; $self->{mail}{Smtp} = $server; $self->{mail}{From} = 'Postmaster <>'; bless $self, $class; return $self; } ## The send method is passed a hashref, containing subject, message an +d optionally a to key. ## eg $obj->send({subject => 'testing', message => 'just trying to sen +d mail', to => 'me'}); ## The $self hashref, which represents the object, is used to store al +l the parts of the %mail hash, which is then passed to sendmail sub send { my ($self, $data) = @_; $self->{mail}{To} = $data->{to} ? $self->{to} : 'Devin Austi +n <>'; $self->{mail}{Subject} = $data->{subject}; $self->{mail}{Message} = $data->{message}; my $date = Mail::Sendmail::time_to_date(); $self->{mail}{Message} .= <<"" Message sent on $date From $ENV{REMOTE_ADDR} All information contained in this message do not reflect the ideas of Devin Austin or MorningStarWeb and it's affiliates. ; if (sendmail %{$self->{mail}) { print STDERR $Mail::Sendmail::error; } else { print STDERR "Error sending mail ($date): $!"; } }


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://480405]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (6)
As of 2018-06-24 19:51 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (126 votes). Check out past polls.