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

Re: Private method variations

by flyingmoose (Priest)
on Mar 01, 2004 at 01:36 UTC ( #332749=note: print w/ replies, xml ) Need Help??


in reply to Private method variations

I'm rather new to Perl OO-ness (but I have significant experience in other languages), but I must say, I like this idea very very much. It fixes one of my biggest issues with Perl OO -- enforcing encapsulation requires some huge incantations that make the code rather hard to read.

Just thinking aloud, but could something similar be done to enforce private variables? This would be a cool feature to have, even if strangely implemented. Hopefully in the same module?

Even if some extra private-variable functionality was not added, I can see how this could trivially be used to restrict an accessor/mutator function. Generally I try to write my OO such that trivial accessor/mutators aren't needed (too many accessors/mutators tends to indicate design flaws), but in Perl, the accessor/mutators are somewhat useful since it's way too easy to create a typo in a hash lookup.

Also, source filters, from what I know of them, are supposed to be evil...but as long as this one doesn't bork with line numbers, it should be ok! Seems like goodness to me.

Cool. I'd say definitely put something like this on CPAN. I'd use it.


Comment on Re: Private method variations
Re: Re: Private method variations
by esskar (Deacon) on Mar 01, 2004 at 02:09 UTC
    Well, normally "private" variables are named with an "_" at the beginning as a key in the blessed hashref...
    since the blessed object is "just" a normal data structure in perl, it is always possible to access the data within that structure... but why does the user who is using your module want to kill himself by not using your methods but accessing the datastructure itself?!?
    On your hand, Perl OO is not as other OO programming like in C++ or java. The blessed "thingy" is not your class object but just an object within your class. so it can not know anything about public or private.

    Hope, i got it right!

    Update:Daim... i missed the original post...sorry...stupid mistake...
      On your hand, Perl OO is not as other OO programming like in C++ or java

      It's a good thing that it's not like C++ (restrictive) or Java (broken), but I still say there is room for improvement. Using underscores is cheesy and was one of the reasons I scoff at Python when people claim it has a good OO model.

Re^2: Private method variations
by adrianh (Chancellor) on Mar 01, 2004 at 08:39 UTC
    Just thinking aloud, but could something similar be done to enforce private variables? This would be a cool feature to have, even if strangely implemented. Hopefully in the same module?

    Have a look at previous nodes on inside out objects. They're a nice technique Abigail-II came up with for doing private variables. As a bonus they prevent typos too.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (12)
As of 2014-09-02 12:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite cookbook is:










    Results (22 votes), past polls