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

  • Home
  • SEARCH
  • 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 8966817
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T17:06:15+00:00 2026-06-15T17:06:15+00:00

I’m pretty new to perl, and I’m getting stuck on a homework problem. I

  • 0

I’m pretty new to perl, and I’m getting stuck on a homework problem. I have an object with a class variable that counts the number of instances created. Then I have a subclass with an instance variable.

My first question is, how do I make the class variable hidden from the user? I tried using closures but couldn’t figure out how to make inheritance work with that. And the fact that it’s a class variable made it worse because the code that increments it executed twice and it said I had two instances when I had one. Not exactly sure why it happened but it makes sense. I tried using scalars but the variable again wasn’t incrementing correctly. Haven’t tried “inside-out objects” yet and I’m not sure I want to, it seems way over my head. I’m getting the feeling that encapsulating class variables is different than encapsulating instance variables, but I can’t find anything that explains how to do it.

My second questions is, as I mentioned, I can’t get encapsulation to work with inheritance. With closures when you call the super constructor from the subclass you get a reference to the subroutine right, so there’s no way (that I know of) to add the instance variables to that.

Here’s my base class:

#!/usr/bin/perl -w
use strict;
package Base;

my $count = 1;

sub new {
    my $class = shift;
    my $self = {
        _Count => $count # not hidden
    };
    $count++; # increment count
    bless $self, $class;
    return $self;
}

sub Count { # getter
    my $self = shift;
    return $self->{_Count};
}
1;

Here’s my subclass:

#!/usr/bin/perl -w
use strict;
package Sub;
use Base;
our @ISA = qw(Base);

sub new {
    my $class = shift;
    my $self = $class->SUPER::New();
    $self->{_Name} = undef; # not hidden
    return $self;
}

sub Name { #getter/setter
    my($self, $name) = @_;
    $self->{_Name} = $name if defined($name);
    return $self->{_Name};
}
1;
  • 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-06-15T17:06:16+00:00Added an answer on June 15, 2026 at 5:06 pm

    If you are using bare Perl 5 (rather than employing an OO framework), the usual way to do class variables is as a lexical visible only to the accessor:

    {
        my $count = 0;
    
        sub Count {
            my ($self, $new_count) = @_;
    
            if (defined $new_count) { # NB only works if undef is not a legit value
                $count = $new_count;
            }
    
            return $count;
         }
    }
    

    $count is only visible in the enclosing block; not even other methods on the same class can see it. But anyone can manipulate it with either $base_obj->Count or Base->Count, and any such manipulation will affect the shared variable.

    You can also employ closure to provide really-hidden instance variables. This is not worth doing unless you are fulfilling the arbitrary rules of a homework assignment.

    package Base;
    
    sub new {
        my ($class, $name) = @_;
        die "Need name!" unless defined $name;
    
        my $age;
    
        return bless sub {
            my ($attribute, @args) = @_;
    
            if ($attribute eq 'name') {
                if (@args) {
                    die "Attempt to set read-only attribute!";
                }
                return $name;
            }
    
            if ($attribute eq 'age') {
                if (@args) {
                     ($age) = @args;
                }
                return $age;
            }
    
            die "Unknown attribute $attribute";
        } => $class;
    }
    
    sub name {
        my ($self, @args) = @_;
    
        return $self->(name => @args);
    }
    
    sub age {
        my ($self, @args) = @_;
    
        return $self->(age => @args);
    }
    

    What happens here is that the blessed sub returned by new closes over two lexicals, $name and $age. When new returns, those lexicals go out of scope and the only way to access them from that point forward is through the closure. The closure can inspect its arguments to permit or deny access to the values it holds. So long as it never returns a reference, it can be sure that it has the only direct access to those variables.

    This works with inheritance, too, without too much added subtlety:

    package Derived;
    use base 'Base';
    sub new {
        my ($class, $name, $color) = @_;
    
        my $base_instance = $class->SUPER::new($name);
    
        return bless sub {
            my ($attribute, @args) = @_;
    
            if ($attribute eq 'color') {
                if (@args) {
                     ($color) = @args;
                }
                return $color;
            }
    
            # base class handles anything we don't, possibly by dying
            return $base_instance->($attribute, @args);
        } => $class;
    }
    

    This emulates what languages with distinct storage for base- and derived-class instance data do, either handling the request locally or passing it on to the base class instance, which has been added to the closure. Deeper inheritance trees will result in closures that close over closures that close over closures, each of them optionally also closing over instance variables needed by that particular class.

    This is a pretty big mess to produce and really hard to inspect and debug, which is why I’m going to emphasize one more time that you should never do this. But it is very useful to understand, to which end I refer you to SICP.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a small JavaScript validation script that validates inputs based on Regex. I
I have a French site that I want to parse, but am running into
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have been unable to fix a problem with Java Unicode and encoding. The
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and

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.