[ale] Private members in perl
Chris Fowler
cfowler at outpostsentinel.com
Sun Aug 1 17:16:50 EDT 2004
Here is one possible solution. Not sure if it a good one but found in
google and did understand it :)
--- Object ---
package OBJ;
use strict;
sub AUTOLOAD {
}
sub new {
my $type = shift;
my $class = ref $type || $type;
my $self = { NAME => undef };
my $closure = sub {
my $field = shift;
@_ and $self->{$field} = shift;
$self->{$field};
};
bless $closure, $class; # Tag object with pkg name
return $closure; # Return object
}
sub get_name {
my $self = shift;
&{$self}('NAME');
}
sub set_name {
my $self = shift;
&{$self}('NAME', @_);
}
-- Program ---
use OBJ;
my $o = OBJ->new();
$o->set_name('CHRIS');
print $o->get_name()."\n";
On Sun, 2004-08-01 at 16:55, Chris Fowler wrote:
> I'm trying to figure a way to implement private members in perl objects
> but all the text I'm reading has me confused
>
> --- Object---
>
>
> {
> package OBJ;
> use strict;
>
>
> sub AUTOLOAD {
> my $self = shift;
> print "AUTOLOAD\n";
> }
>
>
> sub new {
> shift;
>
> #
> # Make sure the type specified is
> # supported
> my $con = {
> 'S' => 1,
> };
> bless $con, 'OBJ'; # Tag object with pkg name
> return $con; # Return object
> }
>
> }
>
>
> return 1;
> --- Program ----
> #!/opt/SAM/perl/bin/perl
>
>
> use OBJ;
>
> my $o = OBJ->new();
> print $o->{'S'} ;
>
>
> Can someone tell me how to make the 'S' private and not accessible?
>
> Thanks,
> Chris
>
>
> _______________________________________________
> Ale mailing list
> Ale at ale.org
> http://www.ale.org/mailman/listinfo/ale
More information about the Ale
mailing list