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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T11:58:51+00:00 2026-05-13T11:58:51+00:00

I am new to object oriented Perl and i have to access member variable

  • 0

I am new to object oriented Perl and i have to access member variable of same object in another subrutine of same object. Sample code is here :

use Class::Struct;

struct Breed =>
{
    name  => '$',
    cross => '$',
};

struct Cat =>
[
    name     => '$',
    kittens  => '@',
    markings => '%',
    breed    => 'Breed',
    breed2 => '$',

];

my $cat = Cat->new( name     => 'Socks',
                    kittens  => ['Monica', 'Kenneth'],
                    markings => { socks=>1, blaze=>"white" },
                    breed    => { name=>'short-hair', cross=>1 },
                    ** //breed2 => sub { return $cat->breed->name;}**

                  );

print "Once a cat called ", $cat->name, "\n";
**print "(which was a ", $cat->breed->name, ")\n";**
print "had two kittens: ", join(' and ', @{$cat->kittens}), "\n";

But i am not sure how to use that $cat->breed->name in subroutine for breed2 ? Can some one help me with this.

  • 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-13T11:58:51+00:00Added an answer on May 13, 2026 at 11:58 am

    The problem in breed2 is that you are trying to refer to a variable that you haven’t defined yet. It looks like it is the same name, but it’s not the object you are creating. It’s a bit of a chicken-and-egg problem.

    I’m not so sure that you want an anonymous subroutine like that in that slot anyway. Are you
    just trying to shorten $cat->breed->name to $cat->breed2? You can start with undef in breed2 and change its value right after the constructor since you’ll have the reference to the object then. However, even if you put a subroutine there, you have to dereference it:

    my $cat = Cat->new( name     => 'Socks',
                        kittens  => ['Monica', 'Kenneth'],
                        markings => { socks=>1, blaze=>"white" },
                        breed    => { name=>'short-hair', cross=>1 },
                        breed2   => undef,
    
                      );
    $cat->breed2( sub { $cat->breed->name } );
    
    print "Once a cat called ", $cat->name, "\n";
    print "(which was a ", $cat->breed2->(), ")\n";
    print "had two kittens: ", join(' and ', @{$cat->kittens}), "\n";
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having trouble figuring out how to structure Perl modules in an object oriented
Just a quick question here: If I choose the object oriented style to interact
The only object oriented programming experience I have is from C#, so PHP is
I am from c# object oriented background and which to work similar priniciples in
I am trying to learn object oriented programming in the context of Java. I
A co-worker of mine is taking an Introduction to Object Oriented Programming class and
I am coding program on c# (C#, & object oriented programming newbie) I am
Right now, I get my Models in a nice object-oriented form. In order to
my question is the inverse of this one . In particular, I've dozens of
I'm not thinking too clearly right now and possibly overlooking something simple. I've been

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.