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 7593931
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T21:17:40+00:00 2026-05-30T21:17:40+00:00

I have a module with the following subroutines: package module; sub new { my

  • 0

I have a module with the following subroutines:

package module;

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

sub add_row{

    @newrow = [1,1,1];
    push @_, @newrow;

};

@matricies is an array of array references. I created objects out of the array references using

my @object= map{module->new($_)} @matrices;

and lets say I want to add a row to one of the objects using:

@object[0]->add_row();

I think there’s something wrong with the add_row subroutine dealing with the use of @ and $. Any ideas?

  • 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-30T21:17:41+00:00Added an answer on May 30, 2026 at 9:17 pm

    Yes, you are adding an array ref yet your variable is an array.

    The first thing you need to do (after using strict and warnings) is to read the following documentation on sigils in Perl (aside from good Perl book):

    • https://stackoverflow.com/a/2732643/119280 – brian d. foy’s excellent summary

    • The rest of the answers to the same question

    • This SO answer

    The best summary I can give you on the syntax of accessing data structures in Perl is (quoting from my older comment)

    • the sygil represents the amount of data from the data structure that you are retrieving ($ of 1 element, @ for a list of elements, % for entire hash)

    • whereas the brace style represent what your data structure is (square for array, curly for hash).


    Now, for your code:

    @newrow = [1,1,1];
    push @_, @newrow;
    

    should be

    my $newrow = [1,1,1];
    push @_, $newrow;
    

    You have another problem in how to access your rows from the object:

    sub add_row {
        my ($self, $newrow) = @_;
        $newrow ||= [1,1,1]; # In case it wasn't passed, default to this?
        push @{$self}, $newrow;
    };
    

    You also have the same problem with @object[0]->add_row(); as with newrow – you are using array sigil to address 1 element

    $object[0]->add_row(); # will add default row of [1,1,1]
    $object[0]->add_row([2,3,4]);
    

    UPDATE: here’s a complete code:

    Module’s add_row() (your constructor is fine):

    sub add_row {
        my ($self, $newrow) = @_;
        $newrow ||= [1,1,1]; # In case it wasn't passed, defauly to this?
        push @{$self}, $newrow;
    };
    

    Test driver:

    use a1;
    my @objects = (a1->new([[5,6,7],[8,9,10]]));
    $objects[0]->add_row();
    $objects[0]->add_row([3,4,5]);
    use Data::Dumper; print Data::Dumper->Dump([\@objects]);
    

    Results:

    $VAR1 = [
          bless( [
                   [
                     5,
                     6,
                     7
                   ],
                   [
                     8,
                     9,
                     10
                   ],
                   [
                     1,
                     1,
                     1
                   ],
                   [
                     3,
                     4,
                     5
                   ]
                 ], 'a1' )
        ];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following mapping: <hibernate-mapping package=server.modules.stats.data> <class name=User table=user> <id name=id> <generator class=native></generator>
I have the following module public class LowerCaseRequest : IHttpModule { public void Init(HttpApplication
Supposing I have the following: module A class B # ... end # ...
I have following code: module CarHelper def call_helpline puts Calling helpline... end end class
Let's say I have a module mod_x like the following: class X: pass x=X()
I have the following module/class: module Pigeons class FedEx attr_accessor :signature_name def initialize(account) self.account
I want the following module to be included in a class I have: module
I have the following code in an Autofac Module that is used in my
i have a module (a single .py file, actually), with a class called HashedDir.
I have a Javascript module the following Javascript: EntryController = function$entry(args) { MainView(); $('#target').click(function()

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.