implementing "protected" in OO perl

Discussion in 'Perl Misc' started by SNeelakantan_C@zaplet.com, Jan 16, 2005.

  1. Guest

    Is there a way of making a certain variable 'protected' so that only
    methods in derived classes may access it? I would prefer a mechanism
    that doesn't wrap the variable in a method call since performance would
    slow down by an order of magnitude.

    -Shanker
    , Jan 16, 2005
    #1
    1. Advertising

  2. Anno Siegel Guest

    <> wrote in comp.lang.perl.misc:
    >
    > Is there a way of making a certain variable 'protected' so that only
    > methods in derived classes may access it?


    As opposed to what?

    > I would prefer a mechanism
    > that doesn't wrap the variable in a method call since performance would
    > slow down by an order of magnitude.


    How do you know that? Access to this variable must dominate your program
    in the strangest way for this to be true.

    If you don't define an accessor (that is, a method) for the variable, it
    doesn't play along with OO at all; there will be no difference between
    derived classes and other classes with respect to that variable. Class
    variables are not inherited in Perl OO.

    Anno
    Anno Siegel, Jan 16, 2005
    #2
    1. Advertising

  3. Am 15 Jan 2005 16:44:57 -0800 schrieb <>:

    > Is there a way of making a certain variable 'protected' so that only
    > methods in derived classes may access it? I would prefer a mechanism
    > that doesn't wrap the variable in a method call since performance would
    > slow down by an order of magnitude.


    I'm thinking of handling it this way:

    package Example;
    use warnings;
    use strict;
    use Package::with::brings::used::functions;

    sub protected_method {
    ensure private caller;
    # ..
    }
    __END__

    private gets caller as parameter and has it's own caller. By comparing it
    is able to find out if both are equal. If they aren't, it drops an error
    object, which is used by ensure to drop an error message (and die). Well,
    the names are just quick thoughts. As well as a (maybe) useful kind of use
    like:

    if( has private caller ) {
    # ...
    }

    hth,
    p

    --
    http://www.dunkelheit.at/
    Robert Sedlacek, Jan 16, 2005
    #3
  4. Am Sun, 16 Jan 2005 03:18:26 +0100 schrieb Robert Sedlacek
    <>:

    > I'm thinking of handling it this way:


    Whoops. I'm sorry, I misread your post in first way. Have you thought about
    using tie()?

    sry again,
    p

    --
    http://www.dunkelheit.at/
    Robert Sedlacek, Jan 16, 2005
    #4
  5. Shawn Corey Guest

    wrote:
    > Is there a way of making a certain variable 'protected' so that only
    > methods in derived classes may access it? I would prefer a mechanism
    > that doesn't wrap the variable in a method call since performance would
    > slow down by an order of magnitude.
    >
    > -Shanker
    >

    Short answer no and yes. See perldoc -f my

    'my' limits the variable's scope to the file (among other things) so if
    your object is in a single file, this will work.

    --- Shawn
    Shawn Corey, Jan 16, 2005
    #5
  6. Guest

    wrote:
    > Is there a way of making a certain variable 'protected' so that only
    > methods in derived classes may access it? I would prefer a mechanism
    > that doesn't wrap the variable in a method call since performance would
    > slow down by an order of magnitude.


    My prefered method would be to state my preference in the specs/docs that
    coders should not diddle with my privates. Then do a code review, and fire
    any coders who diddled with my privates. If someone will only follow good
    practise when the language itself forces them to do so, I have to consider
    that programmer to be armed, hostile, and dangerous.

    Xho

    --
    -------------------- http://NewsReader.Com/ --------------------
    Usenet Newsgroup Service $9.95/Month 30GB
    , Jan 17, 2005
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andreas Klemt
    Replies:
    2
    Views:
    557
    Andreas Klemt
    Jul 5, 2003
  2. Paul Gillingwater
    Replies:
    0
    Views:
    114
    Paul Gillingwater
    Apr 6, 2004
  3. Replies:
    3
    Views:
    142
    Ted Zlatanov
    Jan 2, 2007
  4. PerlFAQ Server
    Replies:
    0
    Views:
    587
    PerlFAQ Server
    Feb 11, 2011
  5. PerlFAQ Server
    Replies:
    0
    Views:
    583
    PerlFAQ Server
    Mar 9, 2011
Loading...

Share This Page