于 2012-1-14 11:25, Parag Kalra 写道:
use FooBar;

my $obj = FooBar->new;
....
....
Do something
....
...
$obj->{'new_key'} = 'some_value'

Now I am not sure if that is the correct way of inserting a new data
structure into an already bless reference

I don't think you should modify the blessed object's attribute directly.
You could setup the get/set methods for the instance variables in the class, for example,

Foo.pm:
package Foo;
use strict;

sub new {
    my $class = shift;
    bless { bar=>1 }, $class;
}

sub bar {
    my $self = shift;
    my $new_v = shift;
    if (defined $new_v) {
        $self->{bar} = $new_v;
    } else {
        $self->{bar};
    }
}

1;

foo.pl:
use Foo;
use strict;

my $foo = Foo->new;
print $foo->bar,"\n";
$foo->bar(2);
print $foo->bar,"\n";

The result after running:
$ perl foo.pl
1
2

HTH.


--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to