For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > May 2004 > public/private variables, accessor functions question









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author public/private variables, accessor functions question
Graeme McLaren

2004-05-20, 10:32 am

Hi, I'm starting to look at OO in PERL, I've written a working class but I
don't know how to implement private and public variables and accessor
functions, any idea how to do that?

Here's wot I have so far:
########################################
#######
#!/usr/bin/perl -w

use strict;
use CGI;
my $q = new CGI;

my $count = 0;

# this class takes no constructor parameters
package TRACKER::GraemeTestClass;
sub new{
my $classname = shift; # we know our class name
$count++; # remember how many objects
bless {}, $classname; # and bless an anonymous hash
}
sub count{
my $self = shift; # this is the object itself
return $count;
}

sub GetNameFromForm{
my $self = shift;
$self->{name}=$q->param("name");
}

sub PrintName{
my $self=shift;
return $self->{name};
}

1;
####################################


Cheers in advance for any tips,

Graeme :)

________________________________________
_________________________
Use MSN Messenger to send music and pics to your friends
http://www.msn.co.uk/messenger

James Edward Gray II

2004-05-20, 11:32 am

On May 20, 2004, at 8:32 AM, Graeme McLaren wrote:

> Hi, I'm starting to look at OO in PERL, I've written a working class
> but I don't know how to implement private and public variables and
> accessor functions, any idea how to do that?


Perl takes a very lax view on the whole security issue of objects.

From that point of view, the first step is to ask yourself if you
really NEED public methods/data? As you say, you have a working class
now. What's wrong with it?

That view aside, you do have some options. They vary in levels of
paranoia and effectiveness. Here's an example of a simple private
method:

my $private_method = sub { ... };

# and later, a call...

$self->$private_method( ... );

As far as data goes, convention is that private Perl instance data
starts with an _. There are no rules in place to enforce this, but you
could certainly code some, if you really must go that far...

Hope that helps.

James

P.S. Move your package line up, just under the shebang line.

P.P.S. Do you know that all objects are using the same query? This
could be a feature or a bug, but worth mentioning.

Wiggins D Anconia

2004-05-20, 11:32 am

> Hi, I'm starting to look at OO in PERL, I've written a working class
but I
> don't know how to implement private and public variables and accessor
> functions, any idea how to do that?
>


'Perl' or 'perl' never PERL.

perldoc perlboot
perldoc perltoot
perldoc perltooc
perldoc perlbot

Lots of help in the above, and probably better answers to your questions
since they are pretty general.

> Here's wot I have so far:
> ########################################
#######
> #!/usr/bin/perl -w
>
> use strict;
> use CGI;
> my $q = new CGI;
>
> my $count = 0;
>
> # this class takes no constructor parameters
> package TRACKER::GraemeTestClass;
> sub new{
> my $classname = shift; # we know our class name
> $count++; # remember how many objects
> bless {}, $classname; # and bless an anonymous hash
> }
> sub count{
> my $self = shift; # this is the object itself


Actually this looks more like a class method, so "object itself" is a
little unclear. $self is probably the Class rather than an instance
(though in this case it doesn't matter, isn't Perl grand :-))

> return $count;
> }
>
> sub GetNameFromForm{
> my $self = shift;
> $self->{name}=$q->param("name");
> }
>
> sub PrintName{
> my $self=shift;
> return $self->{name};
> }
>


This looks like an accessor to me, are you having a problem?

>
> Cheers in advance for any tips,
>


Are you having a problem in particular? Do you need private/public
variable distinction, etc.? Unless you have something specific I would
start going through the docs mentioned above, additionally if you are a
book person, Learning Perl Objects, References, and Modules (Schwartz
and Phoenix, O'Reilly) is excellent as is the slightly dated Object
Oriented Perl (Conway, Manning).

Good luck,

http://danconia.org

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com