Re: Private methods within a Perl Object module

From: Anno Siegel (anno4000_at_lublin.zrz.tu-berlin.de)
Date: 02/23/04


Date: 22 Feb 2004 23:17:24 GMT

Johnny J <johnj@nospam.here.com> wrote in comp.lang.perl.misc:
> I'm doing object oriented programming w/ Perl and created a Perl module
> for a class. What I am trying to do within the module itself is
> implement private methods or subroutines that can only be used by
> (possibly public) methods within the module; I do not want them
> accessible outside the module.
>
> Here's how I'm trying to do it in the module. For flexibility, I wanted
> some of my public methods to invoke the private subroutines via a hash
> table to subroutine refs (which are the private subs). For what I am
> trying to do, this scheme adds much flexibility and manageability, as
> opposed to calling the private subroutines explicitly in the methods by
> name.

I'm not sure I understand entirely what you want to achieve, but there's
enough wrong with your code to comment on.

> -------------------------------------------------
> my %PRIVMETHS = ( 'MOVE' => $prv_MOVE,
> 'WRITE' => $prv_WRITE,
> 'READ' => $prv_READ,
> 'DELETE' => $prv_DELETE, );

You are using the package global variables $prv_MOVE etc to set up the
hash. These are undefined at this point, as far as anyone can see, so
that's what's going into the hash.

You seem to be running without strict, otherwise your program would
have died at this point.

Quite generally, this kind of setup, which is going to be used by other
subs, should go into a BEGIN block, so it is ready when the subs are
ready. It may often not matter, but sometimes it does.

> ##### Private Subroutines (not access. to public) ########
> my $prv_MOVE = sub {
> ....
> }

Here you are setting the lexical variables $prv_MOVE etc. These are
different from the package globals you used above. But even if you
were using the same set of variables, what makes you think setting them
here would retro-actively change the values in your hash? Programs work
sequentially.

> my $prv_WRITE = sub {
> ....
> }
>
> my $prv_READ = sub {
> ....
> }
>
> my $prv_DELETE = sub {
> ....
> }
>
> ##### Public Methods for Object Class ########
> sub action {
> my $self = shift;
> # Method called as $obj->action(<action>, <value>), where <action> is
> # any one of MOVE, WRITE, READ, DELETE, ...
>
> ...
> $PRIVMETHS{$action}->($value);
> ...
> }
> -------------------------------------------------
>
> Unfortunately, I have not been able to get this to work. I have tried
> so many other convolutions to get this to work, even using evals, but no
> go. Any help would be greatly appreciated!

Well, the call should work if $action contains the right value. You just
never assigned anything useful to $PRIVMETHS{ $action}.

Judging from your attempt, you need to learn some elementary Perl, and
probably some programming concepts too, before you try to write your own
OO method dispatcher.

Anno



Relevant Pages

  • Re: Can You Figure Out This - Looping
    ... perl programming problems!) ... Use the urls as the keys to a hash. ... If you have trouble programming this, ...
    (comp.lang.perl.misc)
  • Re: trying to understand a hash - understanding has occured
    ... The first two structures above are shown in my perl ... I desire) the depth of knowledge in any programming language to be ... I comprehend the hash of hashes structure a bit better than before, ... from left to right (first complete legal term dereferences before any ...
    (comp.lang.perl.misc)
  • Noob! Help required and would be really appreciated!
    ... I'm a noob to perl and programming in general (Have done basic C ... On finding the keyword "New Order", it has to start building a hash. ... ordrExePrc: +0000000000350 ...
    (comp.lang.perl.misc)
  • Re: Class::Struct: Cant access struct field elements
    ... Learning Perl, and have been referencing Programming Perl, The Perl ... are called anonymous hashes and arrays or refernces to build up data ... For example, a hash of arrays: ...
    (perl.beginners)
  • Re: Native language versions
    ... : For a general purpose programming language: Probably not. ... Even though this is not Perl, but TeX, I know ... instead of the English commands (TeX ...
    (comp.lang.perl.misc)

Loading