How to make a hash of objects in perl

I would like to be able to store objects in a hash structure so that I can work with the name of the object as a variable. Can someone help me make a sub new {...} which creates a new object as a hash element? I'm not quite sure how to do this or how to reference and / or use an object when it is stored like this. I just want to be able to use and refer to the name of the object for other routines.

See my comment in How can I get the name of an object in Perl? why do I want to do this.

thank

+3
source share
2 answers

. ? , , .

, .

, , , -:

my %hash = (
    some_name => Class->new( ... ),
    other_name => Class->new( ... ).
    );

, , ​​ Intermediate Perl, , .

+5

, . , ?

, perl.

my %hash = ( );
$hash{'foo'} = new Foo(...);
$hash{'bar'} = new Bar(...);

, , , "foo", Foo, "bar" - Bar, .

$hash{'foo'}->foo_method();
$hash{'bar'}->bar_method();

, . , .

+2

Source: https://habr.com/ru/post/1762553/


All Articles