[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[no subject]



--- 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
&gt; <a  rel="nofollow" href="http://www.ale.org/mailman/listinfo/ale";>http://www.ale.org/mailman/listinfo/ale</a>


</pre>
<!--X-Body-of-Message-End-->
<!--X-MsgBody-End-->
<!--X-Follow-Ups-->
<hr>
<!--X-Follow-Ups-End-->
<!--X-References-->
<ul><li><strong>References</strong>:
<ul>
<li><strong><a name="00037" href="msg00037.html">[ale] Private members in perl</a></strong>
<ul><li><em>From:</em> cfowler at outpostsentinel.com (Chris Fowler)</li></ul></li>
</ul></li></ul>
<!--X-References-End-->
<!--X-BotPNI-->
<ul>
<li>Prev by Date:
<strong><a href="msg00037.html">[ale] Private members in perl</a></strong>
</li>
<li>Next by Date:
<strong><a href="msg00039.html">[ale] Colocation and Atlanta</a></strong>
</li>
<li>Previous by thread:
<strong><a href="msg00037.html">[ale] Private members in perl</a></strong>
</li>
<li>Next by thread:
<strong><a href="msg00043.html">[ale] Private members in perl</a></strong>
</li>
<li>Index(es):
<ul>
<li><a href="maillist.html#00038"><strong>Date</strong></a></li>
<li><a href="threads.html#00038"><strong>Thread</strong></a></li>
</ul>
</li>
</ul>

<!--X-BotPNI-End-->
<!--X-User-Footer-->
<!--X-User-Footer-End-->
</body>
</html>