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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T08:55:38+00:00 2026-05-14T08:55:38+00:00

I am just beginner with Perl, so if it sounds stupid – sorry for

  • 0

I am just beginner with Perl, so if it sounds stupid – sorry for that 🙂

My problem is – I am trying to write a class, which has an empty array, defined in constructor of a class. So I am doing this like this:

package MyClass;

use strict;

sub new {
    my ($C) = @_;
    my $self = {
        items => ()
    };
    bless $self, ref $C || $C;
}

sub get {
    return $_[0]->{items};
}

1;

Later I am testing my class with simple script:

use strict;
use Data::Dumper;
use MyClass;

my $o = MyClass->new();
my @items = $o->get();

print "length = ", scalar(@items), "\n", Dumper(@items);

And while running the script I get following:

$ perl my_test.pl 
length = 1
$VAR1 = undef;

Why am I doing wrong what causes that I get my items array filled with undef?

Maybe someone could show me example how the class would need to be defined so I would not get any default values in my array?

  • 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-14T08:55:39+00:00Added an answer on May 14, 2026 at 8:55 am

    The anonymous array reference constructor is [] not () which is used to group statements into lists. The () in this case flattens to an empty list and perl sees my $self = { item => };. If you were running with use warnings; you would have gotten a message about that.

    Also, in your get subroutine you will probably want to dereference your field to return the list instead of a reference to the array:

    sub get {
        return @{ $_[0]->{items} };
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi I am a C++ beginner just encountered a problem I don't know how
I am just a new beginner in MySQL and I wanted to write the
This may sound like a stupid question but I'm a beginner not just to
I was just trying to learn the syntax of the beginner things, and how
I am just a beginner in php. I am trying to print the array
I am just a beginner in C++. I am trying to construct some header
Complete programming beginner just trying to clarify something, I was creating keys for a
$hash_map{$key}->{$value1} = 1; I'm just a beginner at perl and I need help in
Good morning folks, I'm just that beginner in json, I have a formated data
I'm just a beginner, What's wrong with my code, I'm trying to experiment on

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.