Not quite right Rob :) Rob Hanson wrote: > > > But I'm wondering if there is another way > > (like the Java "private variable") to say > > "all class variables declared here are unique > > to each instance"? > > It sounds like you are trying to link how OO-Perl works with OO-Java... and > that is only going to make your head spin. What you really need to do is > understand what bless really does, and that OO-Perl isn't really OO in the > usual sense (but it works like it is). > > In Perl an "object" is really just a scalar variable that has been blessed. > Blessing adds an attribute to the scalar variable which is supposed to > represent the "type" of scalar. The only requirement is that the bless > scalar must be a reference. > > my $x = "foo"; > my $y = bless \$x, "bar"; > print $y; > # PRINTS: bar=SCALAR(0xa01ed1c) > > Here $y is a reference to a scalar (hence the "SCALAR(0xa01ed1c)"), and it > has been tagged as type "bar". > > That is all bless does. > > Now the next part is the magic that Perl does... > > $x->do_stuff; > > Here $x does what looks like a method call. Perl will look at the "type" of > $x, which is "foo", then try to execute do_stuff() in the "foo" package.
The type of $x is 'bar' - you just said so above. Its value is 'foo', but usually you will want a lot more than just a single scalar value in your object, so they are more usually hashes as you say below. > The first argument of this call will always be $x, like this: > > foo:do_stuff($x); THis should be bar::do_stuff($x) > This call does exactly the same thing as above. > > Now when you talk about instance variables, realize that there is no true > object instance in Perl. It is just a reference tagged with a "type"... the > rest is all smoke and mirrors. > > So to emmulate instance variables you can use a hash reference instead of a > scalar reference, like this: > > my %x = (field1 => 'val1', field2 => 'val2'); > my $y = bless \%x, "bar"; > > Now $y is just a hash reference, so this stuff still works: > > print $y->{field1}; > print $y->{field2}; > > ...And $y is also blessed, meaning you can emulate calling methods like I > did earlier. > > Now you can still emulate private variables, but again, it's more smoke and > mirrors. Without getting into the details (look at 'tie' for more info) you > can just use the Tie::SecureHash module. It emulated public, protected, and > private variables. > > http://search.cpan.org/author/DCONWAY/Tie-SecureHash-1.03/SecureHash.pm It would be a Good Idea to get simple objects working before considering private variables. Cheers, Rob -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]