Re: Private methods within a Perl Object module
From: Anno Siegel (anno4000_at_lublin.zrz.tu-berlin.de)
Date: 02/23/04
- Next message: Anno Siegel: "Re: Caching DBI 'prepare' statements for re-use with an Apache server"
- Previous message: Angel: "Re: UNIX Find on Windows"
- In reply to: Johnny J: "Private methods within a Perl Object module"
- Next in thread: Uri Guttman: "Re: Private methods within a Perl Object module"
- Reply: Uri Guttman: "Re: Private methods within a Perl Object module"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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
- Next message: Anno Siegel: "Re: Caching DBI 'prepare' statements for re-use with an Apache server"
- Previous message: Angel: "Re: UNIX Find on Windows"
- In reply to: Johnny J: "Private methods within a Perl Object module"
- Next in thread: Uri Guttman: "Re: Private methods within a Perl Object module"
- Reply: Uri Guttman: "Re: Private methods within a Perl Object module"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|