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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T21:51:08+00:00 2026-05-11T21:51:08+00:00

I created a module Foo::Prototype with the global variables $A and $B. I want

  • 0

I created a module Foo::Prototype with the global variables $A and $B. I want the package Foo::Bar that uses Foo::Prototype as a base to import the global variable $A and $B. I could not figure how to do that.

I understand that using global variables is not a good practice in general, but in this case I want to use them.

The code looks like this:

package Foo:Prototype;
my ($A, $B);
our @EXPORT = qw($A $B);

sub new {
    [...]
    $A = 1;
    $B = 2;
}

1;

package Foo:Bar;
use base Foo:Prototype qw($A $B);

sub test {
    print $A, "\n";
    print $B, "\n";
}

1;


# test.pl
Foo:Bar->new();
Foo:Bar->test();

Edit:

I want to make writing sub classes of Foo::Prototype as compact as possible for other people. Instead of having to write $self->{A}->foo(), I’d rather let people write $A->foo().

  • 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-11T21:51:08+00:00Added an answer on May 11, 2026 at 9:51 pm

    Well, there are a few of issues:

    1. As brian points out, your problem can probably be solved better without using global variables. If you describe what you are trying to achieve rather than how, we may be able to provide better answers.

    2. If you are going to export stuff, you either need a sub import or you need to inherit from Exporter. See perldoc Exporter.

    3. It is not clear where you want the call to new to occur.

    4. As Greg points out in a comment below, variables declared with my at package scope cannot be exported. Therefore, I declared $A and $B using our.

    Here is something that “works” but you are going to have to do some reading and thinking before deciding if this is the way you want to go.

    T.pm:

    package T;
    use strict;
    use warnings;
    
    use base 'Exporter';
    
    our ($A, $B);
    our @EXPORT = qw($A $B);
    
    sub new {
        $A = 1;
        $B = 2;
    }
    
    "EOF T.pm"
    

    U.pm:

    package U;
    
    use strict;
    use warnings;
    
    use base 'T';
    use T;
    
    sub test {
        my $self = shift;
        print "$_\n" for $A, $B;
    }
    
    "EOF U.pm"
    

    t.pl:

    #!/usr/perl/bin
    use strict;
    use warnings; 
    
    use U;
    
    U->new;
    U->test;
    
    C:\Temp> t.pl
    1 
    2
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I created module for admin specific operations. I don't want to write the same
I've got (working) application done with ttk. It uses self-created module for showing a
If I have Foo::Bar written in Ruby, and I want to add a method
I'm defining a module Foo and I am instantiating it within another module Bar
I've created a new node type that is identical to a page called foo,
I created module in netBean platform, then I created window component there, and I
I created a module to add some Extension Methods to a Class. This Class
i have created a module with this among others this function in it: <?php
I just created a module location.rb inside /lib folder with following contents: module Location
I have a DotNetNuke Module created in DNN 4.9.2. It runs fine on my

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.