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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T01:58:47+00:00 2026-05-18T01:58:47+00:00

Sample code: m1.pm my $a; my $b; sub init { $a = shift; $b

  • 0

Sample code:

m1.pm

my $a;
my $b;
sub init {
    $a = shift;
    $b = shift;
}

sub printab {
    print "a = -$a-\n";
    print "b = -$b-\n";
}

1;

m2.pm

my $a;
my $b;
sub init {
    $a = shift;
    $b = shift;
}

1;

test.pl

use strict;
use warnings;

use m1;
use m2;

init('hello', 'world');
printab();

Run:

$ perl test.pl
a = --
b = --
$

What happens is that the init('hello', 'world') call is mapped to m2.pm and initializes the variables ($a and $b) there.

This kind of makes sense, but what I do not understand is why those values are not available in test.pl.

  • Is there something fundamentally wrong that I am trying to do here? What is the correct way to use two modules with same named subroutines and variables?

  • How exactly does a Perl use work? It would help if someone could contrast it with C’s #include directive.

  • 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-18T01:58:48+00:00Added an answer on May 18, 2026 at 1:58 am

    First, do read perldoc perlmod.

    You do not declare a namespace in either module, so everything is in the main namespace. Declare package m1; in m1.pm and package m2; in m2.pm.

    At the very least, you should implement an import method (or inherit the one Exporter provides) so that programs that use modules can decide what to import from where.

    It also seems to me that you are exploring around the edges of OO.

    Further:

    • Avoid using $a and $b as variable names because it is easy to confuse them with the package variables $a and $b used by sort.

    • Don’t use lower case module names: They are reserved for pragmata.

    A minimal implementation (all in one file for testing convenience) looks like this:

    package My::M1;
    
    use strict; use warnings;
    
    sub new { my $class = shift; bless { @_ } => $class }
    
    sub a {
        my $self = shift;
        my ($v) = @_;
        $self->{a} = $v if @_;
        return $self->{a};
    }
    
    sub b {
        my $self = shift;
        my ($v) = @_;
        $self->{b} = $v if @_;
        return $self->{b};
    }
    
    package My::M2;
    
    use strict; use warnings;
    use base 'My::M1';
    
    sub printtab {
        my $self = shift;
        for my $x (qw(a b)) {
            printf "%s = -%s-\n", $x, $self->$x;
        }
    }
    
    package main;
    
    my $m = My::M2->new(a => 'hello', 'b' => 'world');
    $m->printtab;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Sample code: <!DOCTYPE html> <html> <head> <title>test</title> <script language=javascript type=text/javascript> function init() { var
I have a fairly simple block of code. Sub Run(Name) on error resume next
i've a huge problem. Take a look to this sample code private sub FUNCTION1()
Take this sample code: Class Foo ReadOnly name As String Public Sub New(name As
In my code jsc.tools is an object containing objects. Each sub-object contains a init()
Sample Code: var Day = Ember.Object.extend({ date:null, activities:null, // is set to an Em.ArrayProxy
Sample code: public class Service1 { public int Service1() { .... } } public
Sample code <asp:Repeater> <ItemTemplate> <asp:ListView DataSource=<%# Container.DataItem.Items %> ... /> <asp:DataPager .... /> </ItemTemplate>
Our Sample Code EditText txtData = (EditText) findViewById(R.id.txtData); Button btnReadSDFile = (Button) findViewById(R.id.btnReadSDFile); btnReadSDFile.setOnClickListener(new
In sample code, I have seen this: typedef enum Ename { Bob, Mary, John}

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.