Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

use fields, underscores and subclasses

by MattLG (Sexton)
on Nov 23, 2008 at 00:06 UTC ( #725362=perlquestion: print w/ replies, xml ) Need Help??
MattLG has asked for the wisdom of the Perl Monks concerning the following question:

I'm trying to use the fields module (on 5.8) in my OO code but it seems that fields wants to make all my "protected" fields private because they start with underscores.

This feature seems to be well documented, but not the reasoning or the "correct" way to do protected variables.

If I just remove the underscores, that now tells the users that these variables are free to use as you please, whereas they should really understand that if they choose to use them, they take responsibility for any unexpected behaviour.

If perl doesn't have a concept of protected variables, then class inheritance becomes pointless, surely.

So, how do I create variables that are visible to subclasses but tell the user not to use them if they know what's good for them?

It seems to me that making everything public and using underscores makes a hell of a lot more sense than providing private functionality but not protected.

Comment on use fields, underscores and subclasses
Re: use fields, underscores and subclasses
by dragonchild (Archbishop) on Nov 23, 2008 at 00:12 UTC
    Perl doesn't do OO. Perl has features that allow people to build OO systems on top of Perl.

    Don't use fields. Use Moose. It's a much saner way to do everything OO in Perl and gives you a gazillion other features as well.


    My criteria for good software:
    1. Does it work?
    2. Can someone else come in, make a change, and be reasonably certain no bugs were introduced?
      Does Moose still impose that nasty compile-time penalty? It's the main reason I've been avoiding it on small projects.
        naturally

        It has gotten a lot better recently, thanks to Devel::NYTProf and several dedicated testers/benchmarks/developers.

        And if Moose is still too slow, you might want to give Mouse a look.

        -stvn
        You have small projects that need to be so fast that a 1-2s compiletime penalty is the dealbreaker?

        My criteria for good software:
        1. Does it work?
        2. Can someone else come in, make a change, and be reasonably certain no bugs were introduced?
Re: use fields, underscores and subclasses
by JavaFan (Canon) on Nov 23, 2008 at 01:02 UTC
    So, how do I create variables that are visible to subclasses but tell the user not to use them if they know what's good for them?
    Well, first of all, (IMO), it's never a good idea to access attributes from other classes (or objects) directly. So the user doesn't need to be told that it's a bad idea for a specific class - it's always a bad idea.

    Second, if you want to tell the user something, use POD.

    If perl doesn't have a concept of protected variables, then class inheritance becomes pointless, surely.
    Well, Perl doesn't even have the concept of object instance variables. Can't really blame Perl not to have a specific flavour of a thing it doesn't have to begin with. Perl's OO is the opposite of the rest of Perl. Most of Perl is about programmer convenience, letting the programmer focus on the important things, and having the language take care of the details. But when it comes to OO, Perl gives the programmer only two things: @ISA and bless. And that's it. Everything else you have to do yourself.
      > Perl gives the programmer only two things: @ISA and bless.

      you forgot -> for blessed refs! 8 )

      Cheers Rolf

        no he didn't, -> is for REFs, and @ISA/bless is for OO

      > it's never a good idea to access attributes from other classes (or objects) directly. So the user doesn't need to be told that it's a bad idea for a specific class - it's always a bad idea.

      So, are you saying the whole convention of underscoring "private" variables is pointless?

        So, are you saying the whole convention of underscoring "private" variables is pointless?
        IMO, yes. I've never written code where setting an attribute in another object or class was part of the API. I'd prefer if the language didn't even allow access to attributes that aren't both of the same class and the same object.
Re: use fields, underscores and subclasses
by shmem (Canon) on Nov 23, 2008 at 02:47 UTC
    See Alter for protected variables and black box inheritance.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (20)
As of 2014-07-22 14:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (116 votes), past polls