package SomeClass; use Class::Std::Utils; { my %name_of; sub new { my ($class, $name) = @_; my $new_object = bless \do{my $anon_scalar}, $class; $name_of{ident $new_object} = $name; return $new_object; } sub get_name { my ($self) = @_; return $name_of{ident $self}; } }
And the usage:package SomeClass; { # All I need: my $data; sub new { my ($class, $name) = @_; my $scalref; my $new = bless \$scalref, $class; my $addr = norm($new); $data->{$addr} = { name => $name, obj => $new, }; $new } sub get { my $self = shift; my $arg = shift; $self = norm($self); $data->{$self}{$arg}; } sub set { my $self = shift; my ($key, $val) = @_; $self = norm($self); $data->{$self}{$key} = $val; } sub norm { my $address = ($_[0] =~ /\(?x(\w+)/)[0] } }
#!/usr/bin/perl use strict; use SomeClass; my$_1 = SomeClass->new('ok, creation of obj 1'); my$_2 = SomeClass->new('ok, creation of obj 2'); print $_1->get('name'), $/; print $_2->get('name'), $/; $_1->set('name', 'object _1 new attr value'); $_2->set('name', 'object _2 new attr value'); print $_1->get('name'), $/; print $_2->get('name'), $/; print $_2->{'name'}, $/;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Inside-out classes structure
by japhy (Canon) on Oct 02, 2005 at 02:37 UTC | |
by sh1tn (Priest) on Oct 02, 2005 at 03:11 UTC | |
by rir (Vicar) on Oct 02, 2005 at 04:04 UTC | |
|
Re: Inside-out classes structure
by rir (Vicar) on Oct 02, 2005 at 03:57 UTC | |
by Zaxo (Archbishop) on Oct 02, 2005 at 06:59 UTC | |
by xdg (Monsignor) on Oct 02, 2005 at 13:59 UTC | |
by rir (Vicar) on Oct 02, 2005 at 20:51 UTC | |
by xdg (Monsignor) on Oct 03, 2005 at 02:54 UTC | |
|
Re: Inside-out classes structure
by xdg (Monsignor) on Oct 02, 2005 at 03:36 UTC | |
|
Re: Inside-out classes structure
by Anonymous Monk on Oct 03, 2005 at 10:11 UTC | |
| A reply falls below the community's threshold of quality. You may see it by logging in. |