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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T00:24:40+00:00 2026-05-26T00:24:40+00:00

OK. I have a problem trying to inherit constants set in a parent class

  • 0

OK. I have a problem trying to inherit constants set in a parent class for any of the child classes.

#!/usr/bin/perl
use strict;
use warnings;

package Car;
use Exporter qw( import );
our @EXPORT_OK = ( 'WHEELS', 'WINGS' );

use constant WHEELS => 4;
use constant WINGS  => 0;

sub new {
    my ( $class, %args ) = @_;
    my $self = {
        doors  => $args{doors},
        colour => $args{colour},
        wheels => WHEELS,
        wings  => WINGS,
    };
    bless $self, $class;
    return $self;
}

package Car::Sports;
use base qw( Car );

sub new {
    my ( $class, %args ) = @_;
    my $self = {
        doors  => $args{doors},
        engine => $args{engine},
        wheels => WHEELS,
        wings  => WINGS,
    };
    bless $self, $class;
    return $self;
}

package main;
my $obj = Car->new( doors => 4, colour => "red" );
print Dumper $obj;

my $obj2 = Car::Sports->new( doors => 5, engine => "V8" );

print Dumper $obj2;
__END__

The error is:

Bareword "WHEELS" not allowed while "strict subs" in use at ./t.pl line 30.
Bareword "WINGS" not allowed while "strict subs" in use at ./t.pl line 30.
Execution of ./t.pl aborted due to compilation errors.

Now, I haven’t come here to post without doing some research. I understand that one option would be to use Car qw( WHEELS WINGS) in Car::Sports. However, if I do that I get the following error, because the classes are all inline in the same file:

Can't locate Car.pm in @INC 

For a variety of reasons, I need to keep my packages in one file. Is there a way around this? As constants are basically just subs, why do I have to import them when the same would not be true for a normal method?

Finally, I also know I can do this:

package Car::Sports;
use base qw( Car );

sub new {
    my ( $class, %args ) = @_;
    my $self = {
        doors  => $args{doors},
        engine => $args{engine},
        wheels => Car::WHEELS,
        wings  => Car::WINGS,
    };
    bless $self, $class;
    return $self;
}

And it’s fine… But I have a number of classes and want to make the inheritance of constants more generic that having to name the parent class explicitly (and sometimes it’s not just the parent class, but the grandparent).

Many thanks in advance for any pointers!

Cheers

  • 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-26T00:24:41+00:00Added an answer on May 26, 2026 at 12:24 am

    One workaround is to include the line

    package Car::Sports;
    use base qw( Car );
    Car->import(qw(WHEELS WINGS));
    

    AND use the sigils in the Car::Sports constructor:

    ...
    wheels => &WHEELS,
    wings  => &WINGS,
    ...
    

    Your Car class isn’t defining its @EXPORTS_OK list until run-time. The sigils are required because the Car::Sports constructor is parsed at compile-time, and the compiler doesn’t know there should be WHEELS and WINGS symbols in the Car::Sports namespace.


    The only way to avoid the sigils is to define Car‘s exports at compile-time:

    package Car;
    our @EXPORT_OK;
    BEGIN {@EXPORT_OK = qw(WHEELS WINGS)} # set at compile not run time
    ...
    
    package Car::Sports;
    use base qw(Car);
    BEGIN {Car->import('WHEELS','WINGS')} # import before c'tor is parsed
    

    You could also avoid these machinations by defining the Car class in its own Car.pm file. Then you would just say

    use Car qw(WHEELS WINGS);
    

    and everything in the Car.pm file would be parsed at compile time, AND the Exporter::import method (triggered by a call to Car::import) would automatically get run and import the desired symbols to your current namespace.

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

Sidebar

Related Questions

Ok I have this problem I'm trying to use Jquery to load a partial
Here's the coding problem I am trying to solve... I have a base class,
I have a problem when trying to execute this update statement (below) using C#
Hey all, i have a problem with trying to get 2 forms to close
I have a problem I'm trying to solve involving interfaceing a C++ program with
I have run into a problem trying to modify a form I myself have
Greetings, I have problem with errorPlacement, I'm trying to place the error message next
I have a problem when I'm trying to delete an image file. I always
I have this problem I've been trying to tackle for a while. I have
I have big problem when I am trying to deploy my app over clickonce.

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.