2

I have a module with a new constructor:

package myClass;

sub new
{
    my $class = shift;
    my $arrayreference = shift;
    bless $arrayreference, $class;
    return $arrayreference;
};

I want to do something like:

foreach $ref (@arrayref)
{
     $array1 = myClass->new($ref);
}

$array1 is being rewritten each time, but I want each element in the array to have a distinct object name (ex. $array1, $array2, $array3 etc.)

5 Answers 5

5

If you are working with a plural data structure (an array), then you need to store the result into a plural container (or multiple scalar containers). The idomatic way to do this is to use the map function:

my @object_array = map {myClass->new($_)} @source_array;

If you know that @source_array contains a fixed number of items, and you want scalars for each object:

my ($foo, $bar, $baz) = map {myClass->new($_)} @source_with_3_items;
Sign up to request clarification or add additional context in comments.

2 Comments

So my @object_array = map {myClass->new($_)} @source_array; will be an array of my objects? How would I call a function of a certain object?
You would access each element by index in the array: $object_array[2]->some_method(...)
2

I think you should use some hash or array to contain the objects.

foreach $ref (@arrayref)
{
     push @array, myClass->new($ref);
     $hash{$key++} = myClass->new($ref);
}

thus you can access them with $array[42] or $hash{42}.

Comments

2

There is essentially no name difference between $array[1] and $array1. There is a programmatic difference in that $array[1] can be "pieced together" and, under modern Perl environments $array1 can't. Thus I can write $array[$x] for any valid $x and get an item with a "virtual name" of $array.$x.

my @objects = map { MyClass->new( $_ ); } @data_array;

Thus, if you just want to append a number, you probably just want to collect your objects in an array. However, if you want a more complex naming scheme, one or more levels of hashes is probably a good way to go.

If you had a way to derive the name from the object data once formed, and had a method called name, you could do this:

my %object_map 
    = map { my $o = MyClass->new( $_ ); ( $o->name => $o ); } @data_array
    ;

Comments

2

Are you are trying to do it in place?

my @objects = (
    { ...args for 1st object... },
    { ...args for 2nd object... },
    ...
);

$_ = Class->new($_) for @objects;

However, you should avoid reusing variables like that.

my @object_data = (
    { ...args for 1st object... },
    { ...args for 2nd object... },
    ...
);

my @objects = map Class->new($_), @object_data;

Comments

1

I agree with Ade YU and Eric Strom, and have +1'd their answers: you should use one of their approaches. But what you ask is technically possible, using symbolic references, so for completeness' sake:

foreach my $i (0 .. $#arrayref)
{
    no strict refs;
    my $varname = 'array' . ($i + 1);
    ${$varname} = myClass->new($arrayref[$i]);
}

1 Comment

Ya. Good answer. Exactly what the asker needs. Hope the asker to know it's not good practice.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.