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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T22:56:42+00:00 2026-06-13T22:56:42+00:00

I have the following. package A; sub new { my ($class) = @_; my

  • 0

I have the following.

package A;

sub new {
  my ($class) = @_;
  my $self = { };
  bless $self, $class;
  return($self);
}

sub run() {
  die "Task: ",__PACKAGE__,  "requires a run method";
}

package B;
use A;
our @ISA = qw(A);
sub new {
  my ($class) = @_;
  my $self = { };
  bless $self, $class;
  return($self);
}

package C;
use A;
my @Tasks;

sub new {
  my ($class) = @_;
  my $self = { };
  bless $self, $class;
  return($self);
}

sub add{
   my($self,$tempTask) = @_ ;
   push(@Tasks,$tempTask);
   $arraysize = @Tasks;
}

sub execute{
    foreach my $obj (@Tasks)
    {
            $obj->run();
    }
}
1;

Script

#!/usr/local/bin/perl
use strict;
use C;
use B;

my $tb = new C();
my $task = new B();
$tb->add($task);
$tb->execute();

Package B doesn’t have a run method so it defaults to the Package A run method which is what I want. At this point I want it to print out the name of Package B (there will be many different packages inheriting Package A, but it doesnt.

Currently it prints out Package A using the __PACKAGE__ variable.

Any help?

  • 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-13T22:56:43+00:00Added an answer on June 13, 2026 at 10:56 pm

    An object is a blessed reference. __PACKAGE__ will always equal the name of the current package. But ref( $object ) will give you the name of the class of object. There is also Scalar::Util::blessed, which will not give you false positives for non-blessed references.

    use Scalar::Util qw<blessed>;
    
    my $obj   = bless {}, 'A';
    my $class = ref( {} );       # HASH
    $class    = blessed( {} );   # ''
    $class    = ref( $obj );     # A
    $class    = blessed( $obj ); # A
    

    So in your particular case:

    sub run() {
        die "Task: " . ref( shift ) . "requires a run method";
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a module with the following subroutines: package module; sub new { my
I have the following code :- package A; sub new{ //constructor for A }
I have this simple base class (Module): package XMSP::File; use parent 'IO::File'; sub new
Suppose I have following code package memoryleak; public class MemoryLeak { public static int
In Scala IDE I have the following code: package pkg.one object S { class
I have the following simple Java code: package testj; import java.util.*; public class Query<T>
I have the following (simplified) Hibernate entities: @Entity @Table(name = package) public class Package
I have following package structure and classes. package X Class A private string fieldX;
I am new to java and i have written the following: package mypackage; public
I have the following class: package x.y.z; public class MyClass{ public void someMethod(SomeObject object){

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.