Beefy Boxes and Bandwidth Generously Provided by pair Networks httptech
Don't ask to ask, just ask
 
PerlMonks  

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 = 'pop.dnvr.qwest.net'; ## 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 <noreply@dhoss.cjb.net>'; 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 <devin.austin@gmail.com>'; $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): $!"; } }

C.


Comment on Re: problematic OO
Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://480405]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (7)
As of 2014-04-24 09:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (565 votes), past polls