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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T21:33:55+00:00 2026-06-17T21:33:55+00:00

I have a base class called A : package A; use strict; use warnings;

  • 0

I have a base class called A:

package A;
use strict;
use warnings;
use v5.12;

sub import {
    my $caller = caller;
say 'import called from ' . $caller;
    if (not $caller->can('has')) {
        no strict 'refs';
say "--> import has to $caller";
        *{"${caller}::has"} = sub { _attr($caller, @_) };
    }
}

sub _attr {
    my ($class, $name, $default) = @_;
    no strict 'refs';
    *{"${class}::$name"} = sub {
        my ($self, $value) = @_;
        $self->{$name} = $value if $value;
        return $self->{$name} // $default;
    };
}

1;

And have a child class B:

package B;
use strict;
use warnings;
use lib './';
use base 'A';
use v5.12;

#use C;
A->import();

has 'ans' => 42;
say ans();

1;

If I’m trying to run this code, I’ve got an syntax error
but if I make these changes has 'ans' => 42' to has('ans' => 42); then all works great.

Or if I’ve create class C:

package C;
use strict;
use warnings;
use lib '.';
use base 'A';

1;

then I do not need to modify this code has 'ans'... to has(... because all is works.

So, I’ve can’t understand why this happens.
How I can import subroutine has in a child modules?

  • 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-17T21:33:56+00:00Added an answer on June 17, 2026 at 9:33 pm

    To import has from A into any other module you simply need to put use A into that module.

    Replace this line:

    A->import();
    

    with:

    use A;
    

    and it will do what you want.

    Manually calling ->import doesn’t do what you want because it happens at run time, and the import needs to happen at compile time in order for perl to know that has is a subroutine name before it parses the line that uses it. use is a compile-time command, so has effect before the has line gets parsed.

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

Sidebar

Related Questions

I have a base class called Geometry from which there exists a subclass Sphere
I have a base class called BaseClass . Many classes derived from the BaseClass
I have a base class called Object. PhysicsObject inherits from Object. Ball inherits from
I have a base class called Panel, where some information about a window is
I have a base class called Component. I also have 2 interfaces I2DComponent, and
Please Consider this scenario: We have a base class called clsMain : class clsMain
Let me explain you my situation. I have a base class called Shape, and
I have an abstract base class called Party. There are several concrete subclasses (Company,
I have a class Contact (base class),a class called Customer and a class called
I have a base and derived class called Node which form elements of a

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.