Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: avoiding overwriting variables in an inherited class

by JavaFan (Canon)
on Apr 21, 2012 at 10:34 UTC ( [id://966338]=note: print w/replies, xml ) Need Help??


in reply to avoiding overwriting variables in an inherited class

That's a problem with Perl's absolute minimal OO. People have recognized this, and several solutions are available. For instance, using inside-out objects, you can do this with minimal code changes:
package FunnyEmail; use base Email::Simple; use Hash::Util::FieldHash 'fieldhash'; ### Add this fieldhash my %JOKE; ### Add this sub new{ my $class = shift; my $text = shift; # assume this gets passed my $self=Email::Simple->new($text); $JOKE{$self} = undef; ### Change this bless $self,$class; return $self; }
Now your 'JOKE' attribute is in a lexical variable, and not accessable outside of the lexical scope. So, it would not clash with whatever Email::Simple is using, nor will it clash with anything a subclass of FunnyEmail would be doing.

Replies are listed 'Best First'.
Re^2: avoiding overwriting variables in an inherited class
by whatnext156 (Initiate) on May 03, 2012 at 18:06 UTC

    Thank you for your replies .... sorry for taking a while to get back - I had some login problems as I have never posted to this site before! Hopefully now resolved.

    The fact this is an issue is a message that I do in fact have some kind of comprehension about the whole thing. Fantastic!!!

    After some initial difficulties (duh??) I have been reading more about inside-out objects and I have to agree with the poster that these must be the way forward.. alternatively using Moose? (your comments welcome) which negates the worry.

    in general i am surprised by Perl's OO as I am used to Perl being well thought out. I guess OO is always a subjective thing (higher level than the sequential stuff?). ie gives more flexibility but also an enormous number of ways of doing things?

      You could also use Moose, but (as someone who only does Moose when it's on my plate), Moose has always struck me more as a lifestyle than a solution to a problem; its community is not as cultish as some of the webframeworks, but they're on the edge.

      But hey, if it works for you, more power to you.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (3)
As of 2024-04-24 19:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found