Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: How can I create private object variables?

by jeffa (Bishop)
on Mar 18, 2001 at 01:33 UTC ( [id://65201]=note: print w/replies, xml ) Need Help??


in reply to How can I create private object variables?

Use closures. Check out perltoot for more info on how this code works.

{ package Foo; sub new { my $pkg = shift; my %data = ( bar => undef, ); bless sub { my $field = shift; @_ and $data{$field} = shift; $data{$field} }, $pkg } sub bar { my $self = shift; # the blessed closure $self->( 'bar', @_ ) } }
And use:
my $foo = new Foo; $foo->bar(42); # set it print $foo->bar; # get it

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://65201]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (8)
As of 2024-04-19 12:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found