Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6604599
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T19:10:43+00:00 2026-05-25T19:10:43+00:00

Partially related to this question but different, as this is about constructor calls… I

  • 0

Partially related to this question but different, as this is about constructor calls…

I would like to create an array of a fixed number of objects.

I could do this:

my @objects;
push( @objects, new MyPackage::MyObject() );
push( @objects, new MyPackage::MyObject() );
push( @objects, new MyPackage::MyObject() );
# ...

That’s several kinds of ugly. Making it a loop is only marginally better.

Isn’t there a way to create an array of (constructor-initialized) objects in Perl?

Afterthought question:

These “objects” I want to create are actually SWIG-generated wrappers for C structs, i.e. data structures without “behaviour” (other than the SWIG-generated get and set functions). I just want to pass the array as a parameter to the C function, which will fill the structures for me; do I need to call constructors at all, or is there a shortcut to having the get functions for reading the struct contents afterwards? (Yes, I am awfully new to OOPerl…)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T19:10:43+00:00Added an answer on May 25, 2026 at 7:10 pm

    There Is More Than One Concise Way To Do It:

    my @objects = map { new MyPackage::MyObject() } 1..$N;
    
    my @objects = ();
    push @objects, new MyPackage::MyObject() for 1..$N;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This question in partially related to my previous post on this subject. I would
I 'answered' this in a related question - but it is more of an
this question is partially related to my last question . I have a generic
I believe this to be related partially to short-circuiting logic, but I couldn't find
My question is partially inspired by this article written by Eric Lippert: http://blogs.msdn.com/ericlippert/archive/2009/10/19/what-is-this-thing-you-call-thread-safe.aspx Using
Partially related to an earlier question of mine , I have a system in
I got a CSS question related to this fiddle: http://jsfiddle.net/r584e/ Here the relevant screenshot
This question os partially a Drupal question and partially a SQL question. I have
Yep. This question came up looking at Code Igniter but it is not really
This is related to a question a year and change ago . I put

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.