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

  • Home
  • SEARCH
  • 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

Does anyone have sample code to copy open (in-use and locked by another program)
In my code jsc.tools is an object containing objects. Each sub-object contains a init()
Sample code that shows how to create threads using MFC declares the thread function
Searching for some sample code for converting a point in WGS84 coordinate system to
Here is my sample code: from xml.dom.minidom import * def make_xml(): doc = Document()
Here is a sample code to retrieve data from a database using the yield
Here is my sample code. It is meant to be an iterative procedure for
Does anyone have some sample code showing how to POST to a URL using
I'm following the sample code in CFNetwork Programming Guide , specifically the section on
Can you show sample code for reading a drive label or volume name in

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.