Public/Private Data/Funcion in Perl OO?

Discussion in 'Perl Misc' started by Davy, Aug 7, 2006.

  1. Davy

    Davy Guest

    Hi all,

    I am a C++ programmer and new to Perl.
    Is there some equal idea like Public/Private Data/Funcion in Perl OO?
    I found the tutorial provided by Perldoc show all the Data is public??

    Thanks!
    Davy
    Davy, Aug 7, 2006
    #1
    1. Advertising

  2. Davy

    -berlin.de Guest

    Davy <> wrote in comp.lang.perl.misc:
    > Hi all,
    >
    > I am a C++ programmer and new to Perl.
    > Is there some equal idea like Public/Private Data/Funcion in Perl OO?
    > I found the tutorial provided by Perldoc show all the Data is public??


    Right about methods (and subs in general). There is only the convention
    to let the names of private methods begin with an underscore (_private).
    That doesn't stop anyone from using them, but at least they'll know they
    are doing something wrong.

    Data stored in lexical variables are inaccessible from outside their scope
    so they are truly private. This is not true for package variables.

    Anno
    -berlin.de, Aug 7, 2006
    #2
    1. Advertising

  3. Davy

    AC Guest

    <-berlin.de> wrote in message
    news:...
    > Davy <> wrote in comp.lang.perl.misc:
    >> Hi all,
    >>
    >> I am a C++ programmer and new to Perl.
    >> Is there some equal idea like Public/Private Data/Funcion in Perl OO?
    >> I found the tutorial provided by Perldoc show all the Data is public??

    >
    > Right about methods (and subs in general). There is only the convention
    > to let the names of private methods begin with an underscore (_private).
    > That doesn't stop anyone from using them, but at least they'll know they
    > are doing something wrong.
    >
    > Data stored in lexical variables are inaccessible from outside their scope
    > so they are truly private. This is not true for package variables.
    >
    > Anno


    You should check out Damian Conway'a "Perl Best Practices". It covers these
    issues exactly. You can use "inside out" objects to hide data and you can
    use anonymous code refs to achieve private methods. Its been a while since I
    read the book, so I'm not sure about how to get protected behavior.

    Allan
    AC, Aug 7, 2006
    #3
  4. Davy

    -berlin.de Guest

    AC <clarke@n_o_s_p_a_m_hyperformix.com> wrote in comp.lang.perl.misc:
    >
    > <-berlin.de> wrote in message
    > news:...
    > > Davy <> wrote in comp.lang.perl.misc:
    > >> Hi all,
    > >>
    > >> I am a C++ programmer and new to Perl.
    > >> Is there some equal idea like Public/Private Data/Funcion in Perl OO?
    > >> I found the tutorial provided by Perldoc show all the Data is public??

    > >
    > > Right about methods (and subs in general). There is only the convention
    > > to let the names of private methods begin with an underscore (_private).
    > > That doesn't stop anyone from using them, but at least they'll know they
    > > are doing something wrong.
    > >
    > > Data stored in lexical variables are inaccessible from outside their scope
    > > so they are truly private. This is not true for package variables.
    > >
    > > Anno

    >
    > You should check out Damian Conway'a "Perl Best Practices". It covers these
    > issues exactly. You can use "inside out" objects to hide data


    Right. There is no possible access to the data of an inside-out object,
    except through the accessors you provide.

    > and you can
    > use anonymous code refs to achieve private methods. Its been a while since I
    > read the book, so I'm not sure about how to get protected behavior.


    I don't have the book to hand (don't books have a way of being where you
    aren't?), but I don't see how anonymous code refs could ever participate
    in inheritance. For inheritance, methods *must* be named and can't be
    strictly private.

    Anno
    -berlin.de, Aug 7, 2006
    #4
  5. Davy

    Uri Guttman Guest

    >>>>> "D" == Davy <> writes:

    D> I am a C++ programmer and new to Perl.

    we know you are new to perl already so stop saying it.

    and you should really read a good book on perl before you continue your
    education via usenet. it will be much more efficient for both you and
    us. learn.perl.org has a good free book and the perl docs are very good
    too. read a bunch first before you continue posting again and you will
    feel much better. :)

    uri

    --
    Uri Guttman ------ -------- http://www.stemsystems.com
    --Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
    Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
    Uri Guttman, Aug 7, 2006
    #5
  6. Hi,

    I have a book called "Writing Perl Modules for CPAN" and it's my favourite
    book ever! See if you can get a copy. In it is detailed a way to make
    something private and enforcing it by using an anonymous sub.

    This book will probably introduce you to Perl from the right direction,
    since it makes sense for you to find out what Perl does and doesn't do in
    comparison to C++ as soon as possible.

    Anyway, the solution is you have your new sub return an anonymous sub that
    then sets up the internal data using the my keyword... spot the problem
    here??? Yes! Suddenly EVERYTHING is private (well, all of the variables
    anyway) so that you need accessors/mutators for everything... which are
    difficult to roll automatically in this case.

    I guess the short answer is: it's possible, but don't bother!

    J

    Reference:
    Sam Tregar, Writing Perl Modules for CPAN, Apress/Springer NY 2002
    (Try www.apress.com or www.springer-ny.com)


    On Mon, 07 Aug 2006 10:50:34 +0000, anno4000 wrote:

    > Davy <> wrote in comp.lang.perl.misc:
    >> Hi all,
    >>
    >> I am a C++ programmer and new to Perl.
    >> Is there some equal idea like Public/Private Data/Funcion in Perl OO?
    >> I found the tutorial provided by Perldoc show all the Data is public??

    >
    > Right about methods (and subs in general). There is only the convention
    > to let the names of private methods begin with an underscore (_private).
    > That doesn't stop anyone from using them, but at least they'll know they
    > are doing something wrong.
    >
    > Data stored in lexical variables are inaccessible from outside their scope
    > so they are truly private. This is not true for package variables.
    >
    > Anno
    Jimi-Carlo Bukowski-Wills, Aug 8, 2006
    #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. Charles A. Lackman
    Replies:
    1
    Views:
    1,340
    smith
    Dec 8, 2004
  2. SpamProof
    Replies:
    0
    Views:
    544
    SpamProof
    Oct 21, 2003
  3. Replies:
    1
    Views:
    377
    Alvin Bruney - ASP.NET MVP
    Sep 28, 2005
  4. qazmlp
    Replies:
    19
    Views:
    786
    Daniel T.
    Feb 4, 2004
  5. DaveLessnau
    Replies:
    3
    Views:
    424
    Howard
    May 16, 2005
Loading...

Share This Page